-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service-info resource to obtain Shuttle service info (#1129)
* 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
1 parent
38f42bd
commit dbb9adb
Showing
11 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |