diff --git a/loco-gen/src/lib.rs b/loco-gen/src/lib.rs index fc26b8f67..676b4c8d5 100644 --- a/loco-gen/src/lib.rs +++ b/loco-gen/src/lib.rs @@ -31,7 +31,7 @@ pub struct GenerateResults { rrgen: Vec, local_templates: Vec, } -const DEPLOYMENT_SHUTTLE_RUNTIME_VERSION: &str = "0.46.0"; +const DEPLOYMENT_SHUTTLE_RUNTIME_VERSION: &str = "0.51.0"; const DEPLOYMENT_OPTIONS: &[(&str, DeploymentKind)] = &[ ("Docker", DeploymentKind::Docker), diff --git a/loco-gen/src/templates/deployment/shuttle/config.t b/loco-gen/src/templates/deployment/shuttle/config.t index d5de71eef..0c20a9db1 100644 --- a/loco-gen/src/templates/deployment/shuttle/config.t +++ b/loco-gen/src/templates/deployment/shuttle/config.t @@ -2,4 +2,12 @@ to: "Shuttle.toml" skip_exists: true message: "Shuttle.toml file created successfully" --- -name = "{{pkg_name}}" +[deploy] +include = [ + "config/production.yaml" +] + +[build] +assets = [ + "config/production.yaml" +] diff --git a/loco-gen/src/templates/deployment/shuttle/shuttle.t b/loco-gen/src/templates/deployment/shuttle/shuttle.t index cfba68518..ffaeec485 100644 --- a/loco-gen/src/templates/deployment/shuttle/shuttle.t +++ b/loco-gen/src/templates/deployment/shuttle/shuttle.t @@ -39,7 +39,12 @@ async fn main( shuttle_runtime::Environment::Local => Environment::Development, shuttle_runtime::Environment::Deployment => Environment::Production, }; - let boot_result = create_app::(StartMode::ServerOnly, &environment) + + let config = environment + .load() + .expect("Failed to load configuration from the environment"); + + let boot_result = create_app::(StartMode::ServerOnly, &environment, config) .await .unwrap(); diff --git a/loco-gen/tests/templates/deployment.rs b/loco-gen/tests/templates/deployment.rs index 7dea78da5..8fc9f64ca 100644 --- a/loco-gen/tests/templates/deployment.rs +++ b/loco-gen/tests/templates/deployment.rs @@ -99,3 +99,73 @@ fn can_generate_nginx() { .expect("nginx config missing") ); } + +#[test] +fn can_generate_shuttle() { + let mut settings = insta::Settings::clone_current(); + settings.set_prepend_module_to_snapshot(false); + settings.set_snapshot_suffix("deployment"); + let _guard = settings.bind_to_scope(); + + let component = Component::Deployment { + kind: DeploymentKind::Shuttle, + fallback_file: None, + asset_folder: None, + host: "localhost".to_string(), + port: 8080, + }; + + let tree_fs = tree_fs::TreeBuilder::default() + .drop(true) + .add( + ".cargo/config.toml", + r#"[alias] +loco = "run --" +loco-tool = "run --" + +playground = "run --example playground" +"#, + ) + .add( + "Cargo.toml", + r" +[dependencies] + +[dev-dependencies] + +", + ) + .create() + .unwrap(); + let rrgen = RRgen::with_working_dir(&tree_fs.root); + + let gen_result = generate( + &rrgen, + component, + &AppInfo { + app_name: "tester".to_string(), + }, + ) + .expect("Generation failed"); + + assert_eq!( + collect_messages(&gen_result), + r"* Shuttle.toml file created successfully +* Shuttle deployment ready do use +" + ); + assert_snapshot!( + "generate[shuttle.rs]", + fs::read_to_string(tree_fs.root.join("src").join("bin").join("shuttle.rs")) + .expect("shuttle rs missing") + ); + assert_snapshot!( + "inject[.config_toml]", + fs::read_to_string(tree_fs.root.join(".cargo").join("config.toml")) + .expect(".cargo/config.toml not exists") + ); + assert_snapshot!( + "inject[cargo_toml]", + fs::read_to_string(tree_fs.root.join("Cargo.toml")).expect("cargo.toml not exists") + ); +} diff --git a/loco-gen/tests/templates/snapshots/generate[shuttle.rs]@deployment.snap b/loco-gen/tests/templates/snapshots/generate[shuttle.rs]@deployment.snap new file mode 100644 index 000000000..7fc23a3ee --- /dev/null +++ b/loco-gen/tests/templates/snapshots/generate[shuttle.rs]@deployment.snap @@ -0,0 +1,32 @@ +--- +source: loco-gen/tests/templates/deployment.rs +expression: "fs::read_to_string(tree_fs.root.join(\"src\").join(\"bin\").join(\"shuttle.rs\")).expect(\"shuttle rs missing\")" +--- +use loco_rs::boot::{create_app, StartMode}; +use loco_rs::environment::Environment; +use tester::app::App; +use migration::Migrator; +use shuttle_runtime::DeploymentMetadata; + +#[shuttle_runtime::main] +async fn main( + #[shuttle_shared_db::Postgres] conn_str: String, + #[shuttle_runtime::Metadata] meta: DeploymentMetadata, +) -> shuttle_axum::ShuttleAxum { + std::env::set_var("DATABASE_URL", conn_str); + let environment = match meta.env { + shuttle_runtime::Environment::Local => Environment::Development, + shuttle_runtime::Environment::Deployment => Environment::Production, + }; + + let config = environment + .load() + .expect("Failed to load configuration from the environment"); + + let boot_result = create_app::(StartMode::ServerOnly, &environment, config) + .await + .unwrap(); + + let router = boot_result.router.unwrap(); + Ok(router.into()) +} diff --git a/loco-gen/tests/templates/snapshots/inject[.config_toml]@deployment.snap b/loco-gen/tests/templates/snapshots/inject[.config_toml]@deployment.snap new file mode 100644 index 000000000..34d100122 --- /dev/null +++ b/loco-gen/tests/templates/snapshots/inject[.config_toml]@deployment.snap @@ -0,0 +1,9 @@ +--- +source: loco-gen/tests/templates/deployment.rs +expression: "fs::read_to_string(tree_fs.root.join(\".cargo\").join(\"config.toml\")).expect(\".cargo/config.toml not exists\")" +--- +[alias] +loco = "run --bin tester-cli --" +loco-tool = "run --" + +playground = "run --example playground" diff --git a/loco-gen/tests/templates/snapshots/inject[cargo_toml]@deployment.snap b/loco-gen/tests/templates/snapshots/inject[cargo_toml]@deployment.snap new file mode 100644 index 000000000..f974b26b4 --- /dev/null +++ b/loco-gen/tests/templates/snapshots/inject[cargo_toml]@deployment.snap @@ -0,0 +1,15 @@ +--- +source: loco-gen/tests/templates/deployment.rs +expression: "fs::read_to_string(tree_fs.root.join(\"Cargo.toml\")).expect(\"cargo.toml not exists\")" +--- +[dependencies] +shuttle-axum = "0.51.0" +shuttle-runtime = { version = "0.51.0", default-features = false } +shuttle-shared-db = { version = "0.51.0", features = ["postgres"] } + + +[[bin]] +name = "tester" +path = "src/bin/shuttle.rs" + +[dev-dependencies]