diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index a483152..58475d8 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -2,16 +2,16 @@ name: CD on: push: - branches: - - main - workflow_dispatch: + branches: [main] jobs: deploy: runs-on: ubuntu-latest + steps: - uses: shuttle-hq/deploy-action@main with: deploy-key: ${{ secrets.SHUTTLE_API_KEY }} + # just to show usage secrets: | - DATABASE_URL = "sqlite://dev.db" + DATABASE_URL = 'sqlite://dev.db' diff --git a/Cargo.lock b/Cargo.lock index 5bb49e7..ee80aed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -664,7 +664,7 @@ dependencies = [ "iana-time-zone", "num-traits", "serde", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -712,11 +712,12 @@ name = "clean-axum" version = "0.1.0" dependencies = [ "api", - "migration", "shuttle-axum", "shuttle-runtime", + "shuttle-shared-db", "tokio", "tracing-subscriber", + "utils", ] [[package]] @@ -919,6 +920,53 @@ dependencies = [ "syn 2.0.68", ] +[[package]] +name = "diesel" +version = "2.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff236accb9a5069572099f0b350a92e9560e8e63a9b8d546162f4a5e03026bb2" +dependencies = [ + "bitflags 2.6.0", + "byteorder", + "diesel_derives", + "itoa", +] + +[[package]] +name = "diesel-async" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acada1517534c92d3f382217b485db8a8638f111b0e3f2a2a8e26165050f77be" +dependencies = [ + "async-trait", + "diesel", + "futures-util", + "scoped-futures", + "tokio", + "tokio-postgres", +] + +[[package]] +name = "diesel_derives" +version = "2.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14701062d6bed917b5c7103bdffaee1e4609279e240488ad24e7bd979ca6866c" +dependencies = [ + "diesel_table_macro_syntax", + "proc-macro2", + "quote", + "syn 2.0.68", +] + +[[package]] +name = "diesel_table_macro_syntax" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" +dependencies = [ + "syn 2.0.68", +] + [[package]] name = "digest" version = "0.10.7" @@ -1043,6 +1091,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + [[package]] name = "fastrand" version = "1.9.0" @@ -2084,7 +2138,7 @@ dependencies = [ "libc", "redox_syscall 0.5.2", "smallvec", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -2108,6 +2162,24 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -2209,6 +2281,35 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "postgres-protocol" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49b6c5ef183cd3ab4ba005f1ca64c21e8bd97ce4699cfea9e8d9a2c4958ca520" +dependencies = [ + "base64 0.21.7", + "byteorder", + "bytes", + "fallible-iterator", + "hmac", + "md-5", + "memchr", + "rand", + "sha2", + "stringprep", +] + +[[package]] +name = "postgres-types" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d2234cdee9408b523530a9b6d2d6b373d1db34f6a8e51dc03ded1828d7fb67c" +dependencies = [ + "bytes", + "fallible-iterator", + "postgres-protocol", +] + [[package]] name = "powerfmt" version = "0.2.0" @@ -2694,7 +2795,7 @@ dependencies = [ "once_cell", "ring", "rustls-pki-types", - "rustls-webpki 0.102.4", + "rustls-webpki 0.102.5", "subtle", "zeroize", ] @@ -2736,9 +2837,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.102.4" +version = "0.102.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff448f7e92e913c4b7d4c6d8e4540a1724b319b4152b8aef6d4cf8339712b33e" +checksum = "f9a6fccd794a42c2c105b513a2f62bc3fd8f3ba57a4593677ceb0bd035164d78" dependencies = [ "ring", "rustls-pki-types", @@ -2766,6 +2867,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scoped-futures" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1473e24c637950c9bd38763220bea91ec3e095a89f672bbd7a10d03e77ba467" +dependencies = [ + "cfg-if", + "pin-utils", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -3140,6 +3251,20 @@ dependencies = [ "thiserror", ] +[[package]] +name = "shuttle-shared-db" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccd902dc026098f2af1df364610fa8591daea3d5b15673339d1decddf0a8f9a6" +dependencies = [ + "async-trait", + "diesel-async", + "serde", + "serde_json", + "shuttle-service", + "sqlx", +] + [[package]] name = "signal-hook" version = "0.3.17" @@ -3186,6 +3311,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "slab" version = "0.4.9" @@ -3722,6 +3853,32 @@ dependencies = [ "syn 2.0.68", ] +[[package]] +name = "tokio-postgres" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d340244b32d920260ae7448cb72b6e238bddc3d4f7603394e7dd46ed8e48f5b8" +dependencies = [ + "async-trait", + "byteorder", + "bytes", + "fallible-iterator", + "futures-channel", + "futures-util", + "log", + "parking_lot", + "percent-encoding", + "phf", + "pin-project-lite", + "postgres-protocol", + "postgres-types", + "rand", + "socket2 0.5.7", + "tokio", + "tokio-util", + "whoami", +] + [[package]] name = "tokio-rustls" version = "0.26.0" @@ -4064,6 +4221,13 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "utils" +version = "0.1.0" +dependencies = [ + "migration", +] + [[package]] name = "utoipa" version = "5.0.0-alpha.0" @@ -4304,6 +4468,7 @@ checksum = "a44ab49fad634e88f55bf8f9bb3abd2f27d7204172a112c7c9987e01c1c94ea9" dependencies = [ "redox_syscall 0.4.1", "wasite", + "web-sys", ] [[package]] @@ -4343,7 +4508,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -4361,7 +4526,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -4381,18 +4546,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -4403,9 +4568,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -4415,9 +4580,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -4427,15 +4592,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -4445,9 +4610,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -4457,9 +4622,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -4469,9 +4634,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -4481,9 +4646,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" @@ -4515,18 +4680,18 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.34" +version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae87e3fcd617500e5d106f0380cf7b77f3c6092aae37191433159dda23cfb087" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.34" +version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 53b30c1..1091bc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,11 @@ license = "MIT" readme = "README.md" [workspace] -members = ["api", "app", "models", "migration"] +members = ["api", "app", "models", "migration", "utils"] [dependencies] api = { path = "api" } -migration = { path = "migration" } +utils = { path = "utils" } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } tokio = { version = "1.38.0", features = ["full"] } @@ -23,8 +23,10 @@ tokio = { version = "1.38.0", features = ["full"] } # shuttle runtime shuttle-axum = { version = "0.46.0", optional = true } shuttle-runtime = { version = "0.46.0", optional = true } - +shuttle-shared-db = { version = "0.46.0", features = [ + "postgres", +], optional = true } [features] default = [] -shuttle = ["shuttle-axum", "shuttle-runtime"] +shuttle = ["shuttle-axum", "shuttle-runtime", "shuttle-shared-db"] diff --git a/README.md b/README.md index 574063f..132ceb2 100644 --- a/README.md +++ b/README.md @@ -57,24 +57,32 @@ open http://localhost:3000/docs open http://localhost:3000/scalar ``` -## Shuttle deployment +## Start Shuttle local server + +```bash +# cargo install cargo-shuttle +cargo shuttle run +``` -Add `Secrets.toml` +Make sure docker engine is running, otherwise: -```toml -DATABASE_URL = "sqlite://dev.db" +```bash +brew install colima docker +colima start +sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock ``` -Then +## Shuttle deployment ```bash -# cargo install cargo-shuttle +cargo shuttle login cargo shuttle deploy ``` ## Benchmark ```bash +# edit .env to use Postgres cargo run --release wrk --latency -t20 -c50 -d10s http://localhost:3000/users\?username\= ``` diff --git a/src/main.rs b/src/main.rs index 841d088..58c0c34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,10 @@ -mod db; -mod utils; - -use db::migrate; -use utils::create_dev_db; +use utils::migrate; #[cfg(not(feature = "shuttle"))] #[tokio::main] async fn main() { + use utils::create_dev_db; + println!("Starting with tokio"); tracing_subscriber::fmt::init(); @@ -26,14 +24,9 @@ async fn main() { #[cfg(feature = "shuttle")] #[shuttle_runtime::main] -async fn main( - #[shuttle_runtime::Secrets] secrets: shuttle_runtime::SecretStore, -) -> shuttle_axum::ShuttleAxum { +async fn main(#[shuttle_shared_db::Postgres] db_url: String) -> shuttle_axum::ShuttleAxum { println!("Starting with shuttle"); - let db_url = secrets.get("DATABASE_URL").expect("secret was not found"); - create_dev_db(&db_url); - let conn = api::setup_db(&db_url).await; migrate(&conn).await.expect("Migration failed!"); diff --git a/utils/Cargo.toml b/utils/Cargo.toml new file mode 100644 index 0000000..319de64 --- /dev/null +++ b/utils/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "utils" +version = "0.1.0" +edition = "2021" +publish = false + + +[dependencies] +migration = { path = "../migration" } diff --git a/utils/README.md b/utils/README.md new file mode 100644 index 0000000..1694935 --- /dev/null +++ b/utils/README.md @@ -0,0 +1,3 @@ +# utils + +Some utility functions to facilitate development. diff --git a/src/db.rs b/utils/src/db.rs similarity index 100% rename from src/db.rs rename to utils/src/db.rs diff --git a/src/utils.rs b/utils/src/file.rs similarity index 83% rename from src/utils.rs rename to utils/src/file.rs index dad8316..c443eb8 100644 --- a/src/utils.rs +++ b/utils/src/file.rs @@ -1,9 +1,9 @@ use std::process::Command; -pub fn touch(file_name: &str) { +fn touch(file_name: &str) { if cfg!(target_os = "windows") { Command::new("cmd") - .args(["/C", &format!("copy nul {}", file_name)]) + .args(["/C", &format!("type nul >> {}", file_name)]) .output() .expect("failed to execute touch"); } else { diff --git a/utils/src/lib.rs b/utils/src/lib.rs new file mode 100644 index 0000000..fb793ed --- /dev/null +++ b/utils/src/lib.rs @@ -0,0 +1,5 @@ +mod db; +mod file; + +pub use db::migrate; +pub use file::create_dev_db;