From 7cf399638b96e7c78a06b68b0b2a5a980f7b3d32 Mon Sep 17 00:00:00 2001 From: jonaro00 <54029719+jonaro00@users.noreply.github.com> Date: Fri, 15 Mar 2024 10:09:53 +0100 Subject: [PATCH] chore: move secrets & metadata to runtime (#153) * chore: move secrets & metadata to runtime * fix serenity * fix: revert config::Source --- actix-web/clerk/backend/Cargo.toml | 1 - actix-web/clerk/backend/src/main.rs | 4 ++-- axum/metadata/Cargo.toml | 1 - axum/metadata/src/main.rs | 4 ++-- fullstack-templates/saas/backend/Cargo.toml | 1 - fullstack-templates/saas/backend/src/main.rs | 4 ++-- loco/hello-world/Cargo.toml | 15 ++++++--------- loco/hello-world/src/bin/shuttle.rs | 6 +++--- other/standalone-binary/Cargo.toml | 1 - other/standalone-binary/src/bin/shuttle.rs | 4 ++-- poise/hello-world/Cargo.toml | 1 - poise/hello-world/src/main.rs | 4 ++-- rocket/secrets/Cargo.toml | 1 - rocket/secrets/Secrets.toml.example | 2 +- rocket/secrets/src/main.rs | 12 ++++-------- serenity/hello-world/Cargo.toml | 1 - serenity/hello-world/src/main.rs | 12 ++++-------- serenity/postgres/Cargo.toml | 1 - serenity/postgres/src/main.rs | 4 ++-- 19 files changed, 30 insertions(+), 49 deletions(-) diff --git a/actix-web/clerk/backend/Cargo.toml b/actix-web/clerk/backend/Cargo.toml index 689d182c..2566b849 100644 --- a/actix-web/clerk/backend/Cargo.toml +++ b/actix-web/clerk/backend/Cargo.toml @@ -12,4 +12,3 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" shuttle-actix-web = "0.41.0" shuttle-runtime = "0.41.0" -shuttle-secrets = "0.41.0" diff --git a/actix-web/clerk/backend/src/main.rs b/actix-web/clerk/backend/src/main.rs index 627a5a9a..f08d3108 100644 --- a/actix-web/clerk/backend/src/main.rs +++ b/actix-web/clerk/backend/src/main.rs @@ -12,7 +12,7 @@ use clerk_rs::{ }; use serde::{Deserialize, Serialize}; use shuttle_actix_web::ShuttleActixWeb; -use shuttle_secrets::SecretStore; +use shuttle_runtime::SecretStore; struct AppState { client: Clerk, @@ -70,7 +70,7 @@ async fn get_user(state: web::Data, req: HttpRequest) -> impl Responde #[shuttle_runtime::main] async fn main( - #[shuttle_secrets::Secrets] secrets: SecretStore, + #[shuttle_runtime::Secrets] secrets: SecretStore, ) -> ShuttleActixWeb { let app_config = move |cfg: &mut ServiceConfig| { let clerk_secret_key = secrets diff --git a/axum/metadata/Cargo.toml b/axum/metadata/Cargo.toml index 7aa01c64..1e1f95c6 100644 --- a/axum/metadata/Cargo.toml +++ b/axum/metadata/Cargo.toml @@ -7,5 +7,4 @@ edition = "2021" axum = "0.7.3" shuttle-axum = "0.41.0" shuttle-runtime = "0.41.0" -shuttle-metadata = "0.41.0" tokio = "1.28.2" diff --git a/axum/metadata/src/main.rs b/axum/metadata/src/main.rs index f619aac4..77561fdd 100644 --- a/axum/metadata/src/main.rs +++ b/axum/metadata/src/main.rs @@ -1,9 +1,9 @@ use axum::{routing::get, Router}; -use shuttle_metadata::Metadata; +use shuttle_runtime::DeploymentMetadata; #[shuttle_runtime::main] async fn axum( - #[shuttle_metadata::ShuttleMetadata] metadata: Metadata, + #[shuttle_runtime::Metadata] metadata: DeploymentMetadata, ) -> shuttle_axum::ShuttleAxum { let router = Router::new().route("/", get(format!("{:?}", metadata))); diff --git a/fullstack-templates/saas/backend/Cargo.toml b/fullstack-templates/saas/backend/Cargo.toml index 18f9de85..b7a348b9 100644 --- a/fullstack-templates/saas/backend/Cargo.toml +++ b/fullstack-templates/saas/backend/Cargo.toml @@ -18,7 +18,6 @@ reqwest = "0.11.16" serde = { version = "1.0.160", features = ["derive"] } shuttle-axum = "0.41.0" shuttle-runtime = "0.41.0" -shuttle-secrets = "0.41.0" shuttle-shared-db = { version = "0.41.0", features = ["postgres", "sqlx"] } sqlx = { version = "0.7.1", features = ["time"] } time = { version = "0.3.20", features = ["serde"] } diff --git a/fullstack-templates/saas/backend/src/main.rs b/fullstack-templates/saas/backend/src/main.rs index 5608c175..6aa46ce2 100644 --- a/fullstack-templates/saas/backend/src/main.rs +++ b/fullstack-templates/saas/backend/src/main.rs @@ -35,7 +35,7 @@ impl FromRef for Key { #[shuttle_runtime::main] async fn axum( #[shuttle_shared_db::Postgres] postgres: PgPool, - #[shuttle_secrets::Secrets] secrets: shuttle_secrets::SecretStore, + #[shuttle_runtime::Secrets] secrets: shuttle_runtime::SecretStore, ) -> shuttle_axum::ShuttleAxum { sqlx::migrate!() .run(&postgres) @@ -64,7 +64,7 @@ async fn axum( Ok(router.into()) } -fn grab_secrets(secrets: shuttle_secrets::SecretStore) -> (String, String, String, String, String) { +fn grab_secrets(secrets: shuttle_runtime::SecretStore) -> (String, String, String, String, String) { let stripe_key = secrets .get("STRIPE_KEY") .unwrap_or_else(|| "None".to_string()); diff --git a/loco/hello-world/Cargo.toml b/loco/hello-world/Cargo.toml index 721d2b85..d5bfefce 100644 --- a/loco/hello-world/Cargo.toml +++ b/loco/hello-world/Cargo.toml @@ -8,19 +8,16 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -shuttle-axum = "0.38.0" -shuttle-metadata = "0.38.0" -shuttle-runtime = { version = "0.38.0", default-features = false } - +async-trait = "0.1.74" +axum = "0.7.1" +eyre = "*" loco-rs = { version = "0.3.1", default-features = false, features = ["cli"] } +shuttle-axum = "0.41.0" +shuttle-runtime = { version = "0.41.0", default-features = false } serde = "*" serde_json = "*" -eyre = "*" -tokio = { version = "1.33.0", default-features = false } -async-trait = "0.1.74" - -axum = "0.7.1" +tokio = "1.33.0" tracing = "0.1.40" tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] } diff --git a/loco/hello-world/src/bin/shuttle.rs b/loco/hello-world/src/bin/shuttle.rs index f611a79a..c68f027e 100644 --- a/loco/hello-world/src/bin/shuttle.rs +++ b/loco/hello-world/src/bin/shuttle.rs @@ -4,11 +4,11 @@ use loco_rs::environment::Environment; #[shuttle_runtime::main] async fn main( - #[shuttle_metadata::ShuttleMetadata] meta: shuttle_metadata::Metadata, + #[shuttle_runtime::Metadata] meta: shuttle_runtime::DeploymentMetadata, ) -> shuttle_axum::ShuttleAxum { let environment = match meta.env { - shuttle_metadata::Environment::Local => Environment::Development, - shuttle_metadata::Environment::Deployment => Environment::Production, + shuttle_runtime::Environment::Local => Environment::Development, + shuttle_runtime::Environment::Deployment => Environment::Production, }; let boot_result = create_app::(StartMode::ServerOnly, &environment) .await diff --git a/other/standalone-binary/Cargo.toml b/other/standalone-binary/Cargo.toml index 94665841..08ad5488 100644 --- a/other/standalone-binary/Cargo.toml +++ b/other/standalone-binary/Cargo.toml @@ -17,5 +17,4 @@ axum = "0.7.3" dotenvy = "0.15.7" shuttle-axum = "0.41.0" shuttle-runtime = "0.41.0" -shuttle-secrets = "0.41.0" tokio = "1.28.2" diff --git a/other/standalone-binary/src/bin/shuttle.rs b/other/standalone-binary/src/bin/shuttle.rs index 84fd00e8..99133ef6 100644 --- a/other/standalone-binary/src/bin/shuttle.rs +++ b/other/standalone-binary/src/bin/shuttle.rs @@ -1,10 +1,10 @@ use shuttle_axum::ShuttleAxum; -use shuttle_secrets::SecretStore; +use shuttle_runtime::SecretStore; use multi_binary::build_router; #[shuttle_runtime::main] -async fn axum(#[shuttle_secrets::Secrets] secret_store: SecretStore) -> ShuttleAxum { +async fn axum(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleAxum { // Get all resources 'the Shuttle way' let my_secret = secret_store.get("SOME_API_KEY").unwrap(); diff --git a/poise/hello-world/Cargo.toml b/poise/hello-world/Cargo.toml index 03024cb5..a78d445e 100644 --- a/poise/hello-world/Cargo.toml +++ b/poise/hello-world/Cargo.toml @@ -8,7 +8,6 @@ publish = false anyhow = "1.0.68" poise = "0.6.1" shuttle-runtime = "0.41.0" -shuttle-secrets = "0.41.0" # Since poise is a serenity command framework, it can run on Shuttle with shuttle-serenity shuttle-serenity = "0.41.0" tracing = "0.1.37" diff --git a/poise/hello-world/src/main.rs b/poise/hello-world/src/main.rs index 64abe991..29b9bf99 100644 --- a/poise/hello-world/src/main.rs +++ b/poise/hello-world/src/main.rs @@ -1,6 +1,6 @@ use anyhow::Context as _; use poise::serenity_prelude::{ClientBuilder, GatewayIntents}; -use shuttle_secrets::SecretStore; +use shuttle_runtime::SecretStore; use shuttle_serenity::ShuttleSerenity; struct Data {} // User data, which is stored and accessible in all command invocations @@ -15,7 +15,7 @@ async fn hello(ctx: Context<'_>) -> Result<(), Error> { } #[shuttle_runtime::main] -async fn main(#[shuttle_secrets::Secrets] secret_store: SecretStore) -> ShuttleSerenity { +async fn main(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleSerenity { // Get the discord token set in `Secrets.toml` let discord_token = secret_store .get("DISCORD_TOKEN") diff --git a/rocket/secrets/Cargo.toml b/rocket/secrets/Cargo.toml index 86126335..f138c9d7 100644 --- a/rocket/secrets/Cargo.toml +++ b/rocket/secrets/Cargo.toml @@ -8,5 +8,4 @@ anyhow = "1.0.66" rocket = "0.5.0" shuttle-rocket = "0.41.0" shuttle-runtime = "0.41.0" -shuttle-secrets = "0.41.0" tokio = "1.26.0" diff --git a/rocket/secrets/Secrets.toml.example b/rocket/secrets/Secrets.toml.example index 455ab4c8..ceedf199 100644 --- a/rocket/secrets/Secrets.toml.example +++ b/rocket/secrets/Secrets.toml.example @@ -1 +1 @@ -MY_API_KEY = 'the contents of my API key' \ No newline at end of file +MY_API_KEY = 'the contents of my API key' diff --git a/rocket/secrets/src/main.rs b/rocket/secrets/src/main.rs index 879d38ce..c6e6b18e 100644 --- a/rocket/secrets/src/main.rs +++ b/rocket/secrets/src/main.rs @@ -1,10 +1,10 @@ #[macro_use] extern crate rocket; -use anyhow::anyhow; +use anyhow::Context; use rocket::response::status::BadRequest; use rocket::State; -use shuttle_secrets::SecretStore; +use shuttle_runtime::SecretStore; #[get("/secret")] async fn secret(state: &State) -> Result> { @@ -17,14 +17,10 @@ struct MyState { #[shuttle_runtime::main] async fn rocket( - #[shuttle_secrets::Secrets] secret_store: SecretStore, + #[shuttle_runtime::Secrets] secrets: SecretStore, ) -> shuttle_rocket::ShuttleRocket { // get secret defined in `Secrets.toml` file. - let secret = if let Some(secret) = secret_store.get("MY_API_KEY") { - secret - } else { - return Err(anyhow!("secret was not found").into()); - }; + let secret = secrets.get("MY_API_KEY").context("secret was not found")?; let state = MyState { secret }; let rocket = rocket::build().mount("/", routes![secret]).manage(state); diff --git a/serenity/hello-world/Cargo.toml b/serenity/hello-world/Cargo.toml index 7a12eba0..017ebac0 100644 --- a/serenity/hello-world/Cargo.toml +++ b/serenity/hello-world/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" anyhow = "1.0.66" serenity = { version = "0.12.0", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] } shuttle-runtime = "0.41.0" -shuttle-secrets = "0.41.0" shuttle-serenity = "0.41.0" tokio = "1.26.0" tracing = "0.1.37" diff --git a/serenity/hello-world/src/main.rs b/serenity/hello-world/src/main.rs index 2b01b6a8..cb59ff47 100644 --- a/serenity/hello-world/src/main.rs +++ b/serenity/hello-world/src/main.rs @@ -1,9 +1,9 @@ -use anyhow::anyhow; +use anyhow::Context as _; use serenity::async_trait; use serenity::model::channel::Message; use serenity::model::gateway::Ready; use serenity::prelude::*; -use shuttle_secrets::SecretStore; +use shuttle_runtime::SecretStore; use tracing::{error, info}; struct Bot; @@ -25,14 +25,10 @@ impl EventHandler for Bot { #[shuttle_runtime::main] async fn serenity( - #[shuttle_secrets::Secrets] secret_store: SecretStore, + #[shuttle_runtime::Secrets] secrets: SecretStore, ) -> shuttle_serenity::ShuttleSerenity { // Get the discord token set in `Secrets.toml` - let token = if let Some(token) = secret_store.get("DISCORD_TOKEN") { - token - } else { - return Err(anyhow!("'DISCORD_TOKEN' was not found").into()); - }; + let token = secrets.get("DISCORD_TOKEN").context("'DISCORD_TOKEN' was not found")?; // Set gateway intents, which decides what events the bot will be notified about let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT; diff --git a/serenity/postgres/Cargo.toml b/serenity/postgres/Cargo.toml index ab5f31df..0581ba46 100644 --- a/serenity/postgres/Cargo.toml +++ b/serenity/postgres/Cargo.toml @@ -8,7 +8,6 @@ anyhow = "1.0.66" serde = "1.0.148" serenity = { version = "0.12.0", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] } shuttle-runtime = "0.41.0" -shuttle-secrets = "0.41.0" shuttle-serenity = "0.41.0" shuttle-shared-db = { version = "0.41.0", features = ["postgres", "sqlx"] } sqlx = "0.7.1" diff --git a/serenity/postgres/src/main.rs b/serenity/postgres/src/main.rs index d908ebff..c001b979 100644 --- a/serenity/postgres/src/main.rs +++ b/serenity/postgres/src/main.rs @@ -7,7 +7,7 @@ use serenity::model::application::{CommandDataOptionValue, CommandOptionType, In use serenity::model::gateway::Ready; use serenity::model::id::GuildId; use serenity::prelude::*; -use shuttle_secrets::SecretStore; +use shuttle_runtime::SecretStore; use sqlx::{Executor, PgPool}; use tracing::{error, info}; @@ -127,7 +127,7 @@ impl EventHandler for Bot { #[shuttle_runtime::main] async fn serenity( #[shuttle_shared_db::Postgres] pool: PgPool, - #[shuttle_secrets::Secrets] secret_store: SecretStore, + #[shuttle_runtime::Secrets] secret_store: SecretStore, ) -> shuttle_serenity::ShuttleSerenity { // Get the discord token set in `Secrets.toml` let token = secret_store