Skip to content

Commit

Permalink
feat: add service-info resource to obtain Shuttle service info (#1129)
Browse files Browse the repository at this point in the history
* feat: add service-info resource to obtain Shuttle service info

---------

Co-authored-by: jonaro00 <[email protected]>
Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2023
1 parent 38f42bd commit dbb9adb
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ commands:
shuttle-shared-db = { path = "$PWD/resources/shared-db" }
shuttle-secrets = { path = "$PWD/resources/secrets" }
shuttle-static-folder = { path = "$PWD/resources/static-folder" }
shuttle-service-info = { path = "$PWD/resources/service-info" }
shuttle-turso = { path = "$PWD/resources/turso" }
shuttle-axum = { path = "$PWD/services/shuttle-axum" }
Expand Down Expand Up @@ -620,6 +621,7 @@ workflows:
- resources/persist
- resources/secrets
- resources/static-folder
- resources/service-info
- resources/turso
- services/shuttle-actix-web
- services/shuttle-axum
Expand Down Expand Up @@ -834,6 +836,7 @@ workflows:
"resources/secrets",
"resources/shared-db",
"resources/static-folder",
"resources/service-info",
"resources/turso",
]
name: publish-<< matrix.path >>
Expand Down
1 change: 1 addition & 0 deletions DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ shuttle-persist = { path = "[base]/shuttle/resources/persist" }
shuttle-shared-db = { path = "[base]/shuttle/resources/shared-db" }
shuttle-secrets = { path = "[base]/shuttle/resources/secrets" }
shuttle-static-folder = { path = "[base]/shuttle/resources/static-folder" }
shuttle-service-info = { path = "[base]/shuttle/resources/service-info" }
shuttle-turso = { path = "[base]/shuttle/resources/turso" }

shuttle-axum = { path = "[base]/shuttle/services/shuttle-axum" }
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,9 @@ publish: publish-resources publish-cargo-shuttle

publish-resources: publish-resources/aws-rds \
publish-resources/persist \
publish-resources/shared-db
publish-resources/static-folder
publish-resources/shared-db \
publish-resources/static-folder \
publish-resources/service-info

publish-cargo-shuttle: publish-resources/secrets
cd cargo-shuttle; cargo publish
Expand Down
1 change: 1 addition & 0 deletions common/src/models/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn get_resources_table(resources: &Vec<Response>, service_name: &str) -> Str
Type::StaticFolder => "Static Folder",
Type::Persist => "Persist",
Type::Turso => "Turso",
Type::ServiceInfo => "Service Info",
Type::Custom => "Custom",
};

Expand Down
2 changes: 2 additions & 0 deletions common/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum Type {
StaticFolder,
Persist,
Turso,
ServiceInfo,
Custom,
}

Expand Down Expand Up @@ -82,6 +83,7 @@ impl Display for Type {
Type::StaticFolder => write!(f, "static_folder"),
Type::Persist => write!(f, "persist"),
Type::Turso => write!(f, "turso"),
Type::ServiceInfo => write!(f, "service_info"),
Type::Custom => write!(f, "custom"),
}
}
Expand Down
1 change: 1 addition & 0 deletions deployer/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if [[ $PROD != "true" ]]; then
shuttle-shared-db = { path = "/usr/src/shuttle/resources/shared-db" }
shuttle-secrets = { path = "/usr/src/shuttle/resources/secrets" }
shuttle-static-folder = { path = "/usr/src/shuttle/resources/static-folder" }
shuttle-service-info = { path = "/usr/src/shuttle/resources/service-info" }
shuttle-turso = { path = "/usr/src/shuttle/resources/turso" }
shuttle-actix-web = { path = "/usr/src/shuttle/services/shuttle-actix-web" }
Expand Down
6 changes: 6 additions & 0 deletions deployer/src/persistence/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub enum Type {
StaticFolder,
Persist,
Turso,
ServiceInfo,
Custom,
}

Expand All @@ -77,6 +78,7 @@ impl From<Type> for shuttle_common::resource::Type {
Type::StaticFolder => Self::StaticFolder,
Type::Persist => Self::Persist,
Type::Turso => Self::Turso,
Type::ServiceInfo => Self::ServiceInfo,
Type::Custom => Self::Custom,
}
}
Expand All @@ -90,6 +92,7 @@ impl From<shuttle_common::resource::Type> for Type {
shuttle_common::resource::Type::StaticFolder => Self::StaticFolder,
shuttle_common::resource::Type::Persist => Self::Persist,
shuttle_common::resource::Type::Turso => Self::Turso,
shuttle_common::resource::Type::ServiceInfo => Self::ServiceInfo,
shuttle_common::resource::Type::Custom => Self::Custom,
}
}
Expand All @@ -103,6 +106,7 @@ impl Display for Type {
Type::StaticFolder => write!(f, "static_folder"),
Type::Persist => write!(f, "persist"),
Type::Turso => write!(f, "turso"),
Type::ServiceInfo => write!(f, "service_info"),
Type::Custom => write!(f, "custom"),
}
}
Expand All @@ -123,6 +127,7 @@ impl FromStr for Type {
"static_folder" => Ok(Self::StaticFolder),
"persist" => Ok(Self::Persist),
"turso" => Ok(Self::Turso),
"service_info" => Ok(Self::ServiceInfo),
"custom" => Ok(Self::Custom),
_ => Err(format!("'{s}' is an unknown resource type")),
}
Expand Down Expand Up @@ -173,6 +178,7 @@ mod tests {
Type::StaticFolder,
Type::Persist,
Type::Turso,
Type::ServiceInfo,
Type::Custom,
];

Expand Down
5 changes: 5 additions & 0 deletions e2e/tests/integration/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ shuttle-persist = {{ path = "{}" }}
shuttle-shared-db = {{ path = "{}" }}
shuttle-secrets = {{ path = "{}" }}
shuttle-static-folder = {{ path = "{}" }}
shuttle-service-info = {{ path = "{}" }}
shuttle-axum = {{ path = "{}" }}
shuttle-actix-web = {{ path = "{}" }}
Expand All @@ -69,6 +70,10 @@ shuttle-warp = {{ path = "{}" }}"#,
.join("resources")
.join("static-folder")
.display(),
WORKSPACE_ROOT
.join("resources")
.join("service-info")
.display(),
WORKSPACE_ROOT
.join("services")
.join("shuttle-axum")
Expand Down
12 changes: 12 additions & 0 deletions resources/service-info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "shuttle-service-info"
version = "0.24.0"
edition = "2021"
license = "Apache-2.0"
description = "Plugin to get Shuttle service information"
keywords = ["shuttle-service", "service-info"]

[dependencies]
async-trait = "0.1.56"
serde = { version = "1.0.0", features = ["derive"] }
shuttle-service = { path = "../../service", version = "0.24.0" }
22 changes: 22 additions & 0 deletions resources/service-info/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Shuttle Service Info

This plugin allows applications to obtain certain information about their runtime environment.

## Usage

Add `shuttle-service-info` to the dependencies for your service.

You can get this resource using the `shuttle_service_info::ShuttleServiceInfo` attribute to get a `ServiceInfo`. This struct will contain information such as the Shuttle service name.

```rust
#[shuttle_runtime::main]
async fn app(
#[shuttle_service_info::ShuttleServiceInfo] service_info: shuttle_service_info::ServiceInfo,
) -> __ { ... }
```

#### Example projects that use `shuttle-service-info`

| Framework | Link |
| --------- | ------------------------------------------------------------------------------------------ |
| Axum | [axum example](https://github.com/shuttle-hq/shuttle-examples/tree/main/axum/service-info) |
45 changes: 45 additions & 0 deletions resources/service-info/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use shuttle_service::{error::Error, Factory, ResourceBuilder, Type};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ServiceInfo {
/// The Shuttle service name.
service_name: String,
}

impl ServiceInfo {
/// Get the Shuttle service name.
pub fn service_name(&self) -> &str {
&self.service_name
}
}

pub struct ShuttleServiceInfo;

#[async_trait]
impl ResourceBuilder<ServiceInfo> for ShuttleServiceInfo {
fn new() -> Self {
Self
}

const TYPE: Type = Type::ServiceInfo;

type Config = ();

type Output = ServiceInfo;

fn config(&self) -> &Self::Config {
&()
}

async fn output(self, factory: &mut dyn Factory) -> Result<Self::Output, Error> {
Ok(ServiceInfo {
service_name: factory.get_service_name().to_string(),
})
}

async fn build(build_data: &Self::Output) -> Result<ServiceInfo, Error> {
Ok(build_data.clone())
}
}

0 comments on commit dbb9adb

Please sign in to comment.