diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 924d5fa..e09b8c1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -3,26 +3,27 @@ name: Rust on: push: branches: [ "main" ] + pull_request: + branches: [ "main" ] env: CARGO_TERM_COLOR: always jobs: build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose deploy: - if: startsWith(github.ref, 'refs/tags/v') runs-on: ubuntu-latest steps: - uses: shuttle-hq/deploy-action@main with: deploy-key: ${{ secrets.SHUTTLE_API_KEY }} + secrets: | + DATABASE_URL = '${{ secrets.DATABASE_URL }}' diff --git a/.gitignore b/.gitignore index a9be750..09d1f59 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ test.db .DS_Store Secrets.toml +.env diff --git a/Cargo.lock b/Cargo.lock index fc7b848..627663f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -944,6 +944,7 @@ name = "home-inventory-api" version = "0.1.0" dependencies = [ "actix-web", + "anyhow", "chrono", "diesel", "diesel_migrations", @@ -954,8 +955,10 @@ dependencies = [ "serde", "shuttle-actix-web", "shuttle-runtime", + "shuttle-secrets", "strum 0.25.0", "strum_macros 0.25.3", + "tokio", "uuid", ] @@ -1866,6 +1869,16 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "secrecy" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e" +dependencies = [ + "serde", + "zeroize", +] + [[package]] name = "semver" version = "1.0.20" @@ -1959,9 +1972,9 @@ dependencies = [ [[package]] name = "shuttle-actix-web" -version = "0.35.2" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03ef17f9815b0930611aa50642c613500020e735c5ada1551e37452fa5afa516" +checksum = "e4d384c498c4d8e30b76eb7bbafac7218bc8778bf12a314f1114c84a526a80ff" dependencies = [ "actix-web", "num_cpus", @@ -1970,9 +1983,9 @@ dependencies = [ [[package]] name = "shuttle-codegen" -version = "0.35.2" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0d8979720bfd909ca7d7b188f55b1a266d455a5f4fc687153eae1883ba470b8" +checksum = "032ec76ecb7e3698d7a9e836e3649a88faa13e53f4d13eaa314030761f4ecac9" dependencies = [ "proc-macro-error", "proc-macro2", @@ -1982,9 +1995,9 @@ dependencies = [ [[package]] name = "shuttle-common" -version = "0.35.2" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774cf8a2f4bfa3d81d286a2f90fcd9fdfafc0526470ed575c1245aa9ddc3137f" +checksum = "e8e3ddec998dd953743ac0e47de7ae6d65fcb1ffc45e68a4ceecd2558f8cad1b" dependencies = [ "anyhow", "async-trait", @@ -2026,9 +2039,9 @@ dependencies = [ [[package]] name = "shuttle-proto" -version = "0.35.2" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6a2740bb2148e6aa2ad57568ca9bf317cfd5bb241fc622fa512d9e45b046afa" +checksum = "1ff4e554df686fb01ce42636bf768ba902800934ba4e22f5a5d96082f608d325" dependencies = [ "futures-core", "prost 0.12.3", @@ -2039,9 +2052,9 @@ dependencies = [ [[package]] name = "shuttle-runtime" -version = "0.35.2" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d425ff8765955c524714b98c855350e1441237ee43e058c208d1552405cf3099" +checksum = "cd4e48b0112dc1ba7ae056197810808a27948a7352340506f355da7e4bdc6cb4" dependencies = [ "anyhow", "async-trait", @@ -2063,11 +2076,23 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "shuttle-secrets" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71c9fede35b016a177a7842546d93dd27232dbbb777eafb9bdb39890cc76fb5" +dependencies = [ + "async-trait", + "secrecy", + "serde", + "shuttle-service", +] + [[package]] name = "shuttle-service" -version = "0.35.2" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7341769b2e27c2a6175ee71a041f7d0479c122d809fd8ab06c62864ff8fe362d" +checksum = "809db2a4b8511b07f363942737459bb4ec4f859d9adac7759c52677beabe9af4" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index f2b2f1b..537b287 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] actix-web = "4.4.0" +anyhow = "1.0.66" chrono = "0.4.31" diesel = { version = "2.1.4", features = ["postgres", "r2d2", "chrono", "uuid"] } diesel_migrations = "2.1.0" @@ -18,5 +19,7 @@ env_logger = "0.10" log = "0.4" strum_macros = "0.25.3" strum = { version = "0.25.0", features = ["derive"]} -shuttle-actix-web = "0.35.1" -shuttle-runtime = "0.35.1" +shuttle-actix-web = "0.36.0" +shuttle-runtime = "0.36.0" +shuttle-secrets = "0.36.0" +tokio = "1.35.1" diff --git a/README.md b/README.md index 1e65e23..e4d6f5d 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Built with actix ## Development -#### Compile the current package +#### Compile the current package - ``cargo build`` #### Setup the database and migration @@ -16,7 +16,6 @@ Built with actix - ``cargo run`` - ## How to's **Create new table** diff --git a/src/main.rs b/src/main.rs index b4b3d4b..8c87eea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,51 +1,40 @@ -use actix_web::{middleware::Logger, web::{self}, App, HttpServer}; +use actix_web::{ + web::ServiceConfig, + web::{self}, +}; +use shuttle_actix_web::ShuttleActixWeb; pub mod app; pub mod schema; -use app::api::home::{ - all_homes, - add_home, - find_home, - delete_home -}; - -use app::api::room::{add_room, get_room}; +use app::api::home::{add_home, all_homes, delete_home, find_home}; use app::api::item::{add_item, get_items}; +use app::api::room::{add_room, get_room}; -use app::db::{ - initialize_db_pool, - initial_migration -}; +use app::db::{initial_migration, initialize_db_pool}; -// #[cfg(debug_assertions)] -#[actix_web::main] -async fn main() -> std::io::Result<()> { +#[shuttle_runtime::main] +async fn main() -> ShuttleActixWeb { dotenvy::dotenv().ok(); - std::env::set_var("RUST_LOG", "debug"); - std::env::set_var("RUST_BACKTRACE", "1"); - env_logger::init(); let pool = initialize_db_pool(); - log::info!("starting HTTP server at http://localhost:8080"); initial_migration(); - HttpServer::new(move || { - let logger = Logger::default(); - App::new() - .app_data(web::Data::new(pool.clone())) - .wrap(logger) - .service(all_homes) - .service(add_home) - .service(find_home) - .service(delete_home) - .service(add_room) - .service(get_room) - .service(get_items) - .service(add_item) - }) - .bind(("127.0.0.1", 8080))? - .run() - .await + let config = move |cfg: &mut ServiceConfig| { + cfg.app_data(web::Data::new(pool.clone())); + // Homes + cfg.service(all_homes); + cfg.service(add_home); + cfg.service(find_home); + cfg.service(delete_home); + // Rooms + cfg.service(add_room); + cfg.service(get_room); + // Items + cfg.service(add_item); + cfg.service(get_items); + }; + + Ok(config.into()) }