diff --git a/.github/workflows/general.yml b/.github/workflows/general.yml index e417859..2bef1bc 100644 --- a/.github/workflows/general.yml +++ b/.github/workflows/general.yml @@ -1,6 +1,13 @@ name: Rust -on: [push, pull_request] +on: + # NB: this differs from the book's project! + # These settings allow us to run this specific CI pipeline for PRs against + # this specific branch (a.k.a. book chapter). + pull_request: + types: [ opened, synchronize, reopened ] + branches: + - root-chapter-03-part0 env: CARGO_TERM_COLOR: always @@ -9,11 +16,6 @@ jobs: test: name: Test runs-on: ubuntu-latest - services: - postgres: - image: postgres - ports: - - 5432:5432 steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 @@ -21,11 +23,6 @@ jobs: profile: minimal toolchain: stable override: true - - name: Migrate database - run: | - sudo apt-get install libpq-dev -y - cargo install --version=0.1.0-beta.1 sqlx-cli --no-default-features --features postgres - SKIP_DOCKER=true cd chapter03-1 && ./scripts/init_db.sh - uses: actions-rs/cargo@v1 with: command: test @@ -49,11 +46,6 @@ jobs: clippy: name: Clippy runs-on: ubuntu-latest - services: - postgres: - image: postgres - ports: - - 5432:5432 steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 @@ -62,11 +54,6 @@ jobs: toolchain: stable override: true - run: rustup component add clippy - - name: Migrate database - run: | - sudo apt-get install libpq-dev -y - cargo install --version=0.1.0-beta.1 sqlx-cli --no-default-features --features postgres - SKIP_DOCKER=true cd chapter03-1 && ./scripts/init_db.sh - uses: actions-rs/cargo@v1 with: command: clippy @@ -75,11 +62,6 @@ jobs: coverage: name: Code coverage runs-on: ubuntu-latest - services: - postgres: - image: postgres - ports: - - 5432:5432 steps: - name: Checkout repository uses: actions/checkout@v2 @@ -89,13 +71,6 @@ jobs: with: toolchain: stable override: true - - - name: Migrate database - run: | - sudo apt-get install libpq-dev -y - cargo install --version=0.1.0-beta.1 sqlx-cli --no-default-features --features postgres - SKIP_DOCKER=true cd chapter03-1 && ./scripts/init_db.sh - - name: Run cargo-tarpaulin uses: actions-rs/tarpaulin@v0.1 with: diff --git a/Cargo.lock b/Cargo.lock index 9bf71a9..7b1de10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,7 +79,7 @@ dependencies = [ "serde_urlencoded", "sha-1", "slab", - "time 0.2.22", + "time", ] [[package]] @@ -245,7 +245,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "socket2", - "time 0.2.22", + "time", "tinyvec 1.0.1", "url", ] @@ -276,12 +276,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" -[[package]] -name = "ahash" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217" - [[package]] name = "aho-corasick" version = "0.7.14" @@ -291,27 +285,12 @@ dependencies = [ "memchr", ] -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi 0.3.9", -] - [[package]] name = "arc-swap" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" -[[package]] -name = "arrayvec" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" - [[package]] name = "async-trait" version = "0.1.41" @@ -323,15 +302,6 @@ dependencies = [ "syn", ] -[[package]] -name = "atoi" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0afb7287b68575f5ca0e5c7e40191cbd4be59d325781f46faa603e176eaef47" -dependencies = [ - "num-traits", -] - [[package]] name = "autocfg" version = "1.0.1" @@ -422,12 +392,6 @@ dependencies = [ "libc", ] -[[package]] -name = "build_const" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39" - [[package]] name = "bumpalo" version = "3.4.0" @@ -473,89 +437,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chapter03-0" -version = "0.1.0" -dependencies = [ - "actix-rt", - "actix-web", - "reqwest", - "tokio", -] - -[[package]] -name = "chapter03-1" -version = "0.1.0" -dependencies = [ - "actix-rt", - "actix-web", - "chrono", - "config", - "reqwest", - "serde", - "sqlx", - "tokio", - "uuid", -] - -[[package]] -name = "chapter04" -version = "0.1.0" -dependencies = [ - "actix-rt", - "actix-web", - "chrono", - "config", - "lazy_static", - "reqwest", - "serde", - "sqlx", - "tokio", - "tracing", - "tracing-actix-web", - "tracing-bunyan-formatter", - "tracing-futures", - "tracing-log", - "tracing-subscriber", - "uuid", -] - -[[package]] -name = "chapter05" -version = "0.1.0" -dependencies = [ - "actix-rt", - "actix-web", - "chrono", - "config", - "lazy_static", - "reqwest", - "serde", - "serde-aux", - "sqlx", - "tokio", - "tracing", - "tracing-actix-web", - "tracing-bunyan-formatter", - "tracing-futures", - "tracing-log", - "tracing-subscriber", - "uuid", -] - -[[package]] -name = "chrono" -version = "0.4.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" -dependencies = [ - "libc", - "num-integer", - "num-traits", - "time 0.1.44", - "winapi 0.3.9", -] - [[package]] name = "cloudabi" version = "0.1.0" @@ -565,18 +446,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "config" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b076e143e1d9538dde65da30f8481c2a6c44040edb8e02b9bf1351edb92ce3" -dependencies = [ - "lazy_static", - "nom", - "serde", - "yaml-rust", -] - [[package]] name = "const_fn" version = "0.4.2" @@ -590,7 +459,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1373a16a4937bc34efec7b391f9c1500c30b8478a701a4f44c9165cc0475a6e0" dependencies = [ "percent-encoding", - "time 0.2.22", + "time", "version_check", ] @@ -622,15 +491,6 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" -[[package]] -name = "crc" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb" -dependencies = [ - "build_const", -] - [[package]] name = "crc32fast" version = "1.2.0" @@ -640,48 +500,6 @@ dependencies = [ "cfg-if 0.1.10", ] -[[package]] -name = "crossbeam-channel" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87" -dependencies = [ - "crossbeam-utils", - "maybe-uninit", -] - -[[package]] -name = "crossbeam-queue" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" -dependencies = [ - "cfg-if 0.1.10", - "crossbeam-utils", - "maybe-uninit", -] - -[[package]] -name = "crossbeam-utils" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" -dependencies = [ - "autocfg", - "cfg-if 0.1.10", - "lazy_static", -] - -[[package]] -name = "crypto-mac" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" -dependencies = [ - "generic-array", - "subtle", -] - [[package]] name = "derive_more" version = "0.99.11" @@ -708,12 +526,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" -[[package]] -name = "dotenv" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" - [[package]] name = "dtoa" version = "0.4.6" @@ -725,9 +537,6 @@ name = "either" version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" -dependencies = [ - "serde", -] [[package]] name = "encoding_rs" @@ -807,7 +616,6 @@ checksum = "5d8e3078b7b2a8a671cb7a3d17b4760e4181ea243227776ba83fd043b4ca034e" dependencies = [ "futures-channel", "futures-core", - "futures-executor", "futures-io", "futures-sink", "futures-task", @@ -830,17 +638,6 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d674eaa0056896d5ada519900dbf97ead2e46a7b6621e8160d79e2f2e1e2784b" -[[package]] -name = "futures-executor" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc709ca1da6f66143b8c9bec8e6260181869893714e9b5a490b169b0414144ab" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - [[package]] name = "futures-io" version = "0.3.6" @@ -913,16 +710,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "gethostname" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e692e296bfac1d2533ef168d0b60ff5897b8b70a4009276834014dd8924cc028" -dependencies = [ - "libc", - "winapi 0.3.9", -] - [[package]] name = "getrandom" version = "0.1.15" @@ -931,7 +718,7 @@ checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ "cfg-if 0.1.10", "libc", - "wasi 0.9.0+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -959,16 +746,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "hashbrown" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b62f79061a0bc2e046024cb7ba44b08419ed238ecbd9adbd787434b9e8c25" -dependencies = [ - "ahash", - "autocfg", -] - [[package]] name = "hashbrown" version = "0.9.1" @@ -993,22 +770,6 @@ dependencies = [ "libc", ] -[[package]] -name = "hex" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" - -[[package]] -name = "hmac" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "126888268dcc288495a26bf004b38c5fdbb31682f992c84ceb046a1f0fe38840" -dependencies = [ - "crypto-mac", - "digest", -] - [[package]] name = "hostname" version = "0.3.1" @@ -1108,7 +869,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" dependencies = [ "autocfg", - "hashbrown 0.9.1", + "hashbrown", ] [[package]] @@ -1184,19 +945,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "lexical-core" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616" -dependencies = [ - "arrayvec", - "bitflags", - "cfg-if 0.1.10", - "ryu", - "static_assertions", -] - [[package]] name = "libc" version = "0.2.79" @@ -1236,50 +984,18 @@ dependencies = [ "linked-hash-map", ] -[[package]] -name = "maplit" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" - [[package]] name = "match_cfg" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" -[[package]] -name = "matchers" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" -dependencies = [ - "regex-automata", -] - [[package]] name = "matches" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - -[[package]] -name = "md-5" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15" -dependencies = [ - "block-buffer", - "digest", - "opaque-debug", -] - [[package]] name = "memchr" version = "2.3.3" @@ -1383,36 +1099,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "nom" -version = "5.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" -dependencies = [ - "lexical-core", - "memchr", - "version_check", -] - -[[package]] -name = "num-integer" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" -dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" -dependencies = [ - "autocfg", -] - [[package]] name = "num_cpus" version = "1.13.0" @@ -1645,16 +1331,6 @@ dependencies = [ "thread_local", ] -[[package]] -name = "regex-automata" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" -dependencies = [ - "byteorder", - "regex-syntax", -] - [[package]] name = "regex-syntax" version = "0.6.20" @@ -1695,7 +1371,6 @@ dependencies = [ "percent-encoding", "pin-project-lite", "serde", - "serde_json", "serde_urlencoded", "tokio", "tokio-tls", @@ -1800,17 +1475,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-aux" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fb1c0e382599b35cf66986b74182d3787605bd4c3087b4091ee305a692f071" -dependencies = [ - "chrono", - "serde", - "serde_json", -] - [[package]] name = "serde_derive" version = "1.0.117" @@ -1828,7 +1492,6 @@ version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" dependencies = [ - "indexmap", "itoa", "ryu", "serde", @@ -1865,28 +1528,6 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" -[[package]] -name = "sha2" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2933378ddfeda7ea26f48c555bdad8bb446bf8a3d17832dc83e380d444cfb8c1" -dependencies = [ - "block-buffer", - "cfg-if 0.1.10", - "cpuid-bool", - "digest", - "opaque-debug", -] - -[[package]] -name = "sharded-slab" -version = "0.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06d5a3f5166fb5b42a5439f2eee8b9de149e235961e3eb21c5808fc3ea17ff3e" -dependencies = [ - "lazy_static", -] - [[package]] name = "signal-hook-registry" version = "1.2.1" @@ -1921,107 +1562,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "sqlformat" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f699301eec598ffd6c39832cca1416381ea459ac73c506f6ca74c8750fb52969" -dependencies = [ - "lazy_static", - "maplit", - "regex", -] - -[[package]] -name = "sqlx" -version = "0.4.0-beta.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cb7b012f28c74075d6b11172ba1874f4376a255509462eaf2ef25068b31729f" -dependencies = [ - "sqlx-core", - "sqlx-macros", -] - -[[package]] -name = "sqlx-core" -version = "0.4.0-beta.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe2857d90b39b8528948109abc1b8d4c1905d184c87deaf06055f0b21050f13e" -dependencies = [ - "atoi", - "base64", - "bitflags", - "byteorder", - "bytes", - "chrono", - "crc", - "crossbeam-channel", - "crossbeam-queue", - "crossbeam-utils", - "either", - "futures-channel", - "futures-core", - "futures-util", - "hashbrown 0.8.2", - "hex", - "hmac", - "itoa", - "libc", - "log", - "lru-cache", - "md-5", - "memchr", - "once_cell", - "parking_lot", - "percent-encoding", - "rand", - "serde", - "sha-1", - "sha2", - "smallvec", - "sqlformat", - "sqlx-rt", - "stringprep", - "thiserror", - "url", - "uuid", - "whoami", -] - -[[package]] -name = "sqlx-macros" -version = "0.4.0-beta.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d1bc862e5f4484965156c224f7e4c139f2d3c65b6aa0e3ae8c63461831b8da9" -dependencies = [ - "dotenv", - "either", - "futures", - "heck", - "hex", - "proc-macro2", - "quote", - "serde", - "serde_json", - "sha2", - "sqlx-core", - "sqlx-rt", - "syn", - "url", -] - -[[package]] -name = "sqlx-rt" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23f9104f6116b568358f315e9839ae66c4ebbc0e974db5580105f0acfeb4863f" -dependencies = [ - "native-tls", - "once_cell", - "tokio", - "tokio-native-tls", -] - [[package]] name = "standback" version = "0.2.11" @@ -2031,12 +1571,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "stdweb" version = "0.4.20" @@ -2086,22 +1620,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" -[[package]] -name = "stringprep" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ee348cb74b87454fff4b551cbf727025810a004f88aeacae7f85b87f4e9a1c1" -dependencies = [ - "unicode-bidi", - "unicode-normalization", -] - -[[package]] -name = "subtle" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "343f3f510c2915908f155e94f17220b19ccfacf2a64a2a5d8004f2c3e311e7fd" - [[package]] name = "syn" version = "1.0.45" @@ -2165,17 +1683,6 @@ dependencies = [ "num_cpus", ] -[[package]] -name = "time" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" -dependencies = [ - "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi 0.3.9", -] - [[package]] name = "time" version = "0.2.22" @@ -2250,35 +1757,12 @@ dependencies = [ "memchr", "mio", "mio-uds", - "num_cpus", "pin-project-lite", "signal-hook-registry", "slab", - "tokio-macros", "winapi 0.3.9", ] -[[package]] -name = "tokio-macros" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tokio-native-tls" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd608593a919a8e05a7d1fc6df885e40f6a88d3a70a3a7eff23ff27964eda069" -dependencies = [ - "native-tls", - "tokio", -] - [[package]] name = "tokio-tls" version = "0.3.1" @@ -2318,51 +1802,9 @@ dependencies = [ "cfg-if 0.1.10", "log", "pin-project-lite", - "tracing-attributes", "tracing-core", ] -[[package]] -name = "tracing-actix-web" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc36fc2f840643e49d220d07cd7ca81bc31c7f6df25f164d4257971533dab354" -dependencies = [ - "actix-web", - "futures", - "tracing", - "tracing-futures", - "uuid", -] - -[[package]] -name = "tracing-attributes" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tracing-bunyan-formatter" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d8dfd28e9ee6d79937f139ae4f112fa2172018cfdf111c0dac1f3f8c6912053" -dependencies = [ - "chrono", - "gethostname", - "log", - "serde", - "serde_json", - "tracing", - "tracing-core", - "tracing-log", - "tracing-subscriber", -] - [[package]] name = "tracing-core" version = "0.1.17" @@ -2372,59 +1814,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "tracing-futures" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c" -dependencies = [ - "pin-project", - "tracing", -] - -[[package]] -name = "tracing-log" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e0f8c7178e13481ff6765bd169b33e8d554c5d2bbede5e32c356194be02b9b9" -dependencies = [ - "lazy_static", - "log", - "tracing-core", -] - -[[package]] -name = "tracing-serde" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb65ea441fbb84f9f6748fd496cf7f63ec9af5bca94dd86456978d055e8eb28b" -dependencies = [ - "serde", - "tracing-core", -] - -[[package]] -name = "tracing-subscriber" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ef0a5e15477aa303afbfac3a44cba9b6430fdaad52423b1e6c0dbbe28c3eedd" -dependencies = [ - "ansi_term", - "chrono", - "lazy_static", - "matchers", - "regex", - "serde", - "serde_json", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", - "tracing-serde", -] - [[package]] name = "trust-dns-proto" version = "0.19.5" @@ -2527,15 +1916,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "uuid" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" -dependencies = [ - "rand", -] - [[package]] name = "vcpkg" version = "0.2.10" @@ -2564,12 +1944,6 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasm-bindgen" version = "0.2.68" @@ -2648,12 +2022,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "whoami" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7884773ab69074615cb8f8425d0e53f11710786158704fca70f53e71b0e05504" - [[package]] name = "widestring" version = "0.4.3" @@ -2723,10 +2091,11 @@ dependencies = [ ] [[package]] -name = "yaml-rust" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39f0c922f1a334134dc2f7a8b67dc5d25f0735263feec974345ff706bcf20b0d" +name = "zero2prod" +version = "0.1.0" dependencies = [ - "linked-hash-map", + "actix-rt", + "actix-web", + "reqwest", + "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 7b05a59..b242cf2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,21 @@ -[workspace] -members = ["chapter03-0", "chapter03-1", "chapter04", "chapter05"] +[package] +name = "zero2prod" +version = "0.1.0" +authors = ["LukeMathWalker "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +path = "src/lib.rs" + +[[bin]] +path = "src/main.rs" +name = "zero2prod" + +[dependencies] +actix-web = "3.0.0" +actix-rt = "1.1.1" +tokio = "0.2.22" + +[dev-dependencies] +reqwest = "0.10.7" diff --git a/chapter03-0/Cargo.toml b/chapter03-0/Cargo.toml deleted file mode 100644 index 4e5dab3..0000000 --- a/chapter03-0/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "chapter03-0" -version = "0.1.0" -authors = ["LukeMathWalker "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[lib] -path = "src/lib.rs" - -[[bin]] -path = "src/main.rs" -name = "chapter03-0" - -[dependencies] -actix-web = "3.0.0" -actix-rt = "1.1.1" -tokio = "0.2.22" - -[dev-dependencies] -reqwest = "0.10.7" diff --git a/chapter03-1/.env b/chapter03-1/.env deleted file mode 100644 index 88cfb53..0000000 --- a/chapter03-1/.env +++ /dev/null @@ -1 +0,0 @@ -DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter" diff --git a/chapter03-1/Cargo.toml b/chapter03-1/Cargo.toml deleted file mode 100644 index 2164985..0000000 --- a/chapter03-1/Cargo.toml +++ /dev/null @@ -1,26 +0,0 @@ -[package] -name = "chapter03-1" -version = "0.1.0" -authors = ["LukeMathWalker "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[lib] -path = "src/lib.rs" - -[[bin]] -path = "src/main.rs" -name = "chapter03-1" - -[dependencies] -actix-web = "3.0.0" -actix-rt = "1.1.1" -tokio = "0.2.22" -serde = "1.0.115" -config = { version = "0.10.1", default-features = false, features = ["yaml"] } -sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate"] } -uuid = { version = "0.8.1", features = ["v4"] } -chrono = "0.4.15" - -[dev-dependencies] -reqwest = { version = "0.10.7", features = ["json"] } diff --git a/chapter03-1/configuration.yaml b/chapter03-1/configuration.yaml deleted file mode 100644 index 04dc5ef..0000000 --- a/chapter03-1/configuration.yaml +++ /dev/null @@ -1,7 +0,0 @@ -application_port: 8000 -database: - host: "localhost" - port: 5432 - username: "postgres" - password: "password" - database_name: "newsletter" \ No newline at end of file diff --git a/chapter03-1/migrations/20200823135036_create_subscriptions_table.sql b/chapter03-1/migrations/20200823135036_create_subscriptions_table.sql deleted file mode 100644 index 2c0d262..0000000 --- a/chapter03-1/migrations/20200823135036_create_subscriptions_table.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Create Subscriptions Table -CREATE TABLE subscriptions( - id uuid NOT NULL, - PRIMARY KEY (id), - email TEXT NOT NULL UNIQUE, - name TEXT NOT NULL, - subscribed_at timestamptz NOT NULL -); \ No newline at end of file diff --git a/chapter03-1/scripts/init_db.sh b/chapter03-1/scripts/init_db.sh deleted file mode 100755 index cc1df04..0000000 --- a/chapter03-1/scripts/init_db.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash -set -x -set -eo pipefail - -# Check if a custom user has been set, otherwise default to 'postgres' -DB_USER=${POSTGRES_USER:=postgres} -# Check if a custom password has been set, otherwise default to 'password' -DB_PASSWORD="${POSTGRES_PASSWORD:=password}" -# Check if a custom password has been set, otherwise default to 'newsletter' -DB_NAME="${POSTGRES_DB:=newsletter}" -# Check if a custom port has been set, otherwise default to '5432' -DB_PORT="${POSTGRES_PORT:=5432}" - -# Allow to skip Docker if a dockerized Postgres database is already running -if [[ -z "${SKIP_DOCKER}" ]] -then - # Launch postgres using Docker - docker run \ - -e POSTGRES_USER=${DB_USER} \ - -e POSTGRES_PASSWORD=${DB_PASSWORD} \ - -e POSTGRES_DB=${DB_NAME} \ - -p "${DB_PORT}":5432 \ - -d postgres \ - postgres -N 1000 - # ^ Increased maximum number of connections for testing purposes -fi - -# Keep pinging Postgres until it's ready to accept commands -until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do - >&2 echo "Postgres is still unavailable - sleeping" - sleep 1 -done - ->&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" - -export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME} -sqlx database create -sqlx migrate run - ->&2 echo "Postgres has been migrated, ready to go!" diff --git a/chapter03-1/src/configuration.rs b/chapter03-1/src/configuration.rs deleted file mode 100644 index 3ad6da7..0000000 --- a/chapter03-1/src/configuration.rs +++ /dev/null @@ -1,38 +0,0 @@ -#[derive(serde::Deserialize)] -pub struct Settings { - pub database: DatabaseSettings, - pub application_port: u16, -} - -#[derive(serde::Deserialize)] -pub struct DatabaseSettings { - pub username: String, - pub password: String, - pub port: u16, - pub host: String, - pub database_name: String, -} - -impl DatabaseSettings { - pub fn connection_string(&self) -> String { - format!( - "postgres://{}:{}@{}:{}/{}", - self.username, self.password, self.host, self.port, self.database_name - ) - } - - pub fn connection_string_without_db(&self) -> String { - format!( - "postgres://{}:{}@{}:{}", - self.username, self.password, self.host, self.port - ) - } -} - -pub fn get_configuration() -> Result { - let mut settings = config::Config::default(); - - settings.merge(config::File::with_name("configuration"))?; - - settings.try_into() -} diff --git a/chapter03-1/src/lib.rs b/chapter03-1/src/lib.rs deleted file mode 100644 index b477f22..0000000 --- a/chapter03-1/src/lib.rs +++ /dev/null @@ -1,4 +0,0 @@ -#![allow(clippy::toplevel_ref_arg)] -pub mod configuration; -pub mod routes; -pub mod startup; diff --git a/chapter03-1/src/main.rs b/chapter03-1/src/main.rs deleted file mode 100644 index b4f54f4..0000000 --- a/chapter03-1/src/main.rs +++ /dev/null @@ -1,23 +0,0 @@ -use chapter03_1::configuration::get_configuration; -use chapter03_1::startup::run; -use sqlx::postgres::PgPool; -use std::net::TcpListener; - -#[actix_rt::main] -async fn main() -> std::io::Result<()> { - let configuration = get_configuration().expect("Failed to read configuration."); - let connection_pool = PgPool::connect(&configuration.database.connection_string()) - .await - .expect("Failed to connect to Postgres."); - - // Here we choose to bind explicitly to localhost, 127.0.0.1, for security - // reasons. This binding may cause issues in some environments. For example, - // it causes connectivity issues running in WSL2, where you cannot reach the - // server when it is bound to WSL2's localhost interface. As a workaround, - // you can choose to bind to all interfaces, 0.0.0.0, instead, but be aware - // of the security implications when you expose the server on all interfaces. - let address = format!("127.0.0.1:{}", configuration.application_port); - let listener = TcpListener::bind(address)?; - run(listener, connection_pool)?.await?; - Ok(()) -} diff --git a/chapter03-1/src/routes/health_check.rs b/chapter03-1/src/routes/health_check.rs deleted file mode 100644 index d7eb4e0..0000000 --- a/chapter03-1/src/routes/health_check.rs +++ /dev/null @@ -1,5 +0,0 @@ -use actix_web::HttpResponse; - -pub async fn health_check() -> HttpResponse { - HttpResponse::Ok().finish() -} diff --git a/chapter03-1/src/routes/mod.rs b/chapter03-1/src/routes/mod.rs deleted file mode 100644 index 90ffeed..0000000 --- a/chapter03-1/src/routes/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod health_check; -mod subscriptions; - -pub use health_check::*; -pub use subscriptions::*; diff --git a/chapter03-1/src/routes/subscriptions.rs b/chapter03-1/src/routes/subscriptions.rs deleted file mode 100644 index 8db4b58..0000000 --- a/chapter03-1/src/routes/subscriptions.rs +++ /dev/null @@ -1,33 +0,0 @@ -use actix_web::{web, HttpResponse}; -use chrono::Utc; -use sqlx::PgPool; -use uuid::Uuid; - -#[derive(serde::Deserialize)] -pub struct FormData { - email: String, - name: String, -} - -pub async fn subscribe( - form: web::Form, - pool: web::Data, -) -> Result { - sqlx::query!( - r#" - INSERT INTO subscriptions (id, email, name, subscribed_at) - VALUES ($1, $2, $3, $4) - "#, - Uuid::new_v4(), - form.email, - form.name, - Utc::now() - ) - .execute(pool.as_ref()) - .await - .map_err(|e| { - println!("Failed to execute query: {}", e); - HttpResponse::InternalServerError().finish() - })?; - Ok(HttpResponse::Ok().finish()) -} diff --git a/chapter03-1/src/startup.rs b/chapter03-1/src/startup.rs deleted file mode 100644 index 9f35c15..0000000 --- a/chapter03-1/src/startup.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::routes::{health_check, subscribe}; -use actix_web::dev::Server; -use actix_web::web::Data; -use actix_web::{web, App, HttpServer}; -use sqlx::PgPool; -use std::net::TcpListener; - -pub fn run(listener: TcpListener, db_pool: PgPool) -> Result { - let db_pool = Data::new(db_pool); - let server = HttpServer::new(move || { - App::new() - .route("/health_check", web::get().to(health_check)) - .route("/subscriptions", web::post().to(subscribe)) - .app_data(db_pool.clone()) - }) - .listen(listener)? - .run(); - Ok(server) -} diff --git a/chapter03-1/tests/health_check.rs b/chapter03-1/tests/health_check.rs deleted file mode 100644 index 61de9c6..0000000 --- a/chapter03-1/tests/health_check.rs +++ /dev/null @@ -1,129 +0,0 @@ -use chapter03_1::configuration::{get_configuration, DatabaseSettings}; -use chapter03_1::startup::run; -use sqlx::{Connection, Executor, PgConnection, PgPool}; -use std::net::TcpListener; -use uuid::Uuid; - -pub struct TestApp { - pub address: String, - pub db_pool: PgPool, -} - -async fn spawn_app() -> TestApp { - let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); - // We retrieve the port assigned to us by the OS - let port = listener.local_addr().unwrap().port(); - let address = format!("http://127.0.0.1:{}", port); - - let mut configuration = get_configuration().expect("Failed to read configuration."); - configuration.database.database_name = Uuid::new_v4().to_string(); - let connection_pool = configure_database(&configuration.database).await; - - let server = run(listener, connection_pool.clone()).expect("Failed to bind address"); - let _ = tokio::spawn(server); - TestApp { - address, - db_pool: connection_pool, - } -} - -pub async fn configure_database(config: &DatabaseSettings) -> PgPool { - // Create database - let mut connection = PgConnection::connect(&config.connection_string_without_db()) - .await - .expect("Failed to connect to Postgres"); - connection - .execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name)) - .await - .expect("Failed to create database."); - - // Migrate database - let connection_pool = PgPool::connect(&config.connection_string()) - .await - .expect("Failed to connect to Postgres."); - sqlx::migrate!("./migrations") - .run(&connection_pool) - .await - .expect("Failed to migrate the database"); - - connection_pool -} - -#[actix_rt::test] -async fn health_check_works() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - - // Act - let response = client - // Use the returned application address - .get(&format!("{}/health_check", &app.address)) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert!(response.status().is_success()); - assert_eq!(Some(0), response.content_length()); -} - -#[actix_rt::test] -async fn subscribe_returns_a_200_for_valid_form_data() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; - - // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(body) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert_eq!(200, response.status().as_u16()); - - let saved = sqlx::query!("SELECT email, name FROM subscriptions",) - .fetch_one(&app.db_pool) - .await - .expect("Failed to fetch saved subscription."); - - assert_eq!(saved.email, "ursula_le_guin@gmail.com"); - assert_eq!(saved.name, "le guin"); -} - -#[actix_rt::test] -async fn subscribe_returns_a_400_when_data_is_missing() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - let test_cases = vec![ - ("name=le%20guin", "missing the email"), - ("email=ursula_le_guin%40gmail.com", "missing the name"), - ("", "missing both name and email"), - ]; - - for (invalid_body, error_message) in test_cases { - // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(invalid_body) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert_eq!( - 400, - response.status().as_u16(), - // Additional customised error message on test failure - "The API did not fail with 400 Bad Request when the payload was {}.", - error_message - ); - } -} diff --git a/chapter04/.env b/chapter04/.env deleted file mode 100644 index 88cfb53..0000000 --- a/chapter04/.env +++ /dev/null @@ -1 +0,0 @@ -DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter" diff --git a/chapter04/Cargo.toml b/chapter04/Cargo.toml deleted file mode 100644 index 01edb36..0000000 --- a/chapter04/Cargo.toml +++ /dev/null @@ -1,33 +0,0 @@ -[package] -name = "chapter04" -version = "0.1.0" -authors = ["LukeMathWalker "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[lib] -path = "src/lib.rs" - -[[bin]] -path = "src/main.rs" -name = "chapter04" - -[dependencies] -actix-web = "3.0.0" -actix-rt = "1.1.1" -tokio = "0.2.22" -serde = "1.0.115" -config = { version = "0.10.1", default-features = false, features = ["yaml"] } -sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate"] } -uuid = { version = "0.8.1", features = ["v4"] } -chrono = "0.4.15" -tracing = "0.1.19" -tracing-futures = "0.2.4" -tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] } -tracing-bunyan-formatter = "0.1.6" -tracing-log = "0.1.1" -tracing-actix-web = "0.2.0" - -[dev-dependencies] -reqwest = { version = "0.10.7", features = ["json"] } -lazy_static = "1.4.0" diff --git a/chapter04/configuration.yaml b/chapter04/configuration.yaml deleted file mode 100644 index 04dc5ef..0000000 --- a/chapter04/configuration.yaml +++ /dev/null @@ -1,7 +0,0 @@ -application_port: 8000 -database: - host: "localhost" - port: 5432 - username: "postgres" - password: "password" - database_name: "newsletter" \ No newline at end of file diff --git a/chapter04/migrations/20200823135036_create_subscriptions_table.sql b/chapter04/migrations/20200823135036_create_subscriptions_table.sql deleted file mode 100644 index 2c0d262..0000000 --- a/chapter04/migrations/20200823135036_create_subscriptions_table.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Create Subscriptions Table -CREATE TABLE subscriptions( - id uuid NOT NULL, - PRIMARY KEY (id), - email TEXT NOT NULL UNIQUE, - name TEXT NOT NULL, - subscribed_at timestamptz NOT NULL -); \ No newline at end of file diff --git a/chapter04/scripts/init_db.sh b/chapter04/scripts/init_db.sh deleted file mode 100755 index cc1df04..0000000 --- a/chapter04/scripts/init_db.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash -set -x -set -eo pipefail - -# Check if a custom user has been set, otherwise default to 'postgres' -DB_USER=${POSTGRES_USER:=postgres} -# Check if a custom password has been set, otherwise default to 'password' -DB_PASSWORD="${POSTGRES_PASSWORD:=password}" -# Check if a custom password has been set, otherwise default to 'newsletter' -DB_NAME="${POSTGRES_DB:=newsletter}" -# Check if a custom port has been set, otherwise default to '5432' -DB_PORT="${POSTGRES_PORT:=5432}" - -# Allow to skip Docker if a dockerized Postgres database is already running -if [[ -z "${SKIP_DOCKER}" ]] -then - # Launch postgres using Docker - docker run \ - -e POSTGRES_USER=${DB_USER} \ - -e POSTGRES_PASSWORD=${DB_PASSWORD} \ - -e POSTGRES_DB=${DB_NAME} \ - -p "${DB_PORT}":5432 \ - -d postgres \ - postgres -N 1000 - # ^ Increased maximum number of connections for testing purposes -fi - -# Keep pinging Postgres until it's ready to accept commands -until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do - >&2 echo "Postgres is still unavailable - sleeping" - sleep 1 -done - ->&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" - -export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME} -sqlx database create -sqlx migrate run - ->&2 echo "Postgres has been migrated, ready to go!" diff --git a/chapter04/src/configuration.rs b/chapter04/src/configuration.rs deleted file mode 100644 index 3ad6da7..0000000 --- a/chapter04/src/configuration.rs +++ /dev/null @@ -1,38 +0,0 @@ -#[derive(serde::Deserialize)] -pub struct Settings { - pub database: DatabaseSettings, - pub application_port: u16, -} - -#[derive(serde::Deserialize)] -pub struct DatabaseSettings { - pub username: String, - pub password: String, - pub port: u16, - pub host: String, - pub database_name: String, -} - -impl DatabaseSettings { - pub fn connection_string(&self) -> String { - format!( - "postgres://{}:{}@{}:{}/{}", - self.username, self.password, self.host, self.port, self.database_name - ) - } - - pub fn connection_string_without_db(&self) -> String { - format!( - "postgres://{}:{}@{}:{}", - self.username, self.password, self.host, self.port - ) - } -} - -pub fn get_configuration() -> Result { - let mut settings = config::Config::default(); - - settings.merge(config::File::with_name("configuration"))?; - - settings.try_into() -} diff --git a/chapter04/src/lib.rs b/chapter04/src/lib.rs deleted file mode 100644 index 5d8e21e..0000000 --- a/chapter04/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![allow(clippy::toplevel_ref_arg)] -pub mod configuration; -pub mod routes; -pub mod startup; -pub mod telemetry; diff --git a/chapter04/src/main.rs b/chapter04/src/main.rs deleted file mode 100644 index 6d032d2..0000000 --- a/chapter04/src/main.rs +++ /dev/null @@ -1,27 +0,0 @@ -use chapter04::configuration::get_configuration; -use chapter04::startup::run; -use chapter04::telemetry::{get_subscriber, init_subscriber}; -use sqlx::postgres::PgPool; -use std::net::TcpListener; - -#[actix_rt::main] -async fn main() -> std::io::Result<()> { - let subscriber = get_subscriber("zero2prod".into(), "info".into()); - init_subscriber(subscriber); - - let configuration = get_configuration().expect("Failed to read configuration."); - let connection_pool = PgPool::connect(&configuration.database.connection_string()) - .await - .expect("Failed to connect to Postgres."); - - // Here we choose to bind explicitly to localhost, 127.0.0.1, for security - // reasons. This binding may cause issues in some environments. For example, - // it causes connectivity issues running in WSL2, where you cannot reach the - // server when it is bound to WSL2's localhost interface. As a workaround, - // you can choose to bind to all interfaces, 0.0.0.0, instead, but be aware - // of the security implications when you expose the server on all interfaces. - let address = format!("127.0.0.1:{}", configuration.application_port); - let listener = TcpListener::bind(address)?; - run(listener, connection_pool)?.await?; - Ok(()) -} diff --git a/chapter04/src/routes/health_check.rs b/chapter04/src/routes/health_check.rs deleted file mode 100644 index d7eb4e0..0000000 --- a/chapter04/src/routes/health_check.rs +++ /dev/null @@ -1,5 +0,0 @@ -use actix_web::HttpResponse; - -pub async fn health_check() -> HttpResponse { - HttpResponse::Ok().finish() -} diff --git a/chapter04/src/routes/mod.rs b/chapter04/src/routes/mod.rs deleted file mode 100644 index 90ffeed..0000000 --- a/chapter04/src/routes/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod health_check; -mod subscriptions; - -pub use health_check::*; -pub use subscriptions::*; diff --git a/chapter04/src/routes/subscriptions.rs b/chapter04/src/routes/subscriptions.rs deleted file mode 100644 index fd801af..0000000 --- a/chapter04/src/routes/subscriptions.rs +++ /dev/null @@ -1,52 +0,0 @@ -use actix_web::{web, HttpResponse}; -use chrono::Utc; -use sqlx::PgPool; -use uuid::Uuid; - -#[derive(serde::Deserialize)] -pub struct FormData { - email: String, - name: String, -} - -#[tracing::instrument( - name = "Adding a new subscriber", - skip(form, pool), - fields( - email = %form.email, - name = %form.name - ) -)] -pub async fn subscribe( - form: web::Form, - pool: web::Data, -) -> Result { - insert_subscriber(&pool, &form) - .await - .map_err(|_| HttpResponse::InternalServerError().finish())?; - Ok(HttpResponse::Ok().finish()) -} - -#[tracing::instrument( - name = "Saving new subscriber details in the database", - skip(form, pool) -)] -pub async fn insert_subscriber(pool: &PgPool, form: &FormData) -> Result<(), sqlx::Error> { - sqlx::query!( - r#" - INSERT INTO subscriptions (id, email, name, subscribed_at) - VALUES ($1, $2, $3, $4) - "#, - Uuid::new_v4(), - form.email, - form.name, - Utc::now() - ) - .execute(pool) - .await - .map_err(|e| { - tracing::error!("Failed to execute query: {:?}", e); - e - })?; - Ok(()) -} diff --git a/chapter04/src/startup.rs b/chapter04/src/startup.rs deleted file mode 100644 index d091772..0000000 --- a/chapter04/src/startup.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::routes::{health_check, subscribe}; -use actix_web::dev::Server; -use actix_web::web::Data; -use actix_web::{web, App, HttpServer}; -use sqlx::PgPool; -use std::net::TcpListener; -use tracing_actix_web::TracingLogger; - -pub fn run(listener: TcpListener, db_pool: PgPool) -> Result { - let db_pool = Data::new(db_pool); - let server = HttpServer::new(move || { - App::new() - .wrap(TracingLogger) - .route("/health_check", web::get().to(health_check)) - .route("/subscriptions", web::post().to(subscribe)) - .app_data(db_pool.clone()) - }) - .listen(listener)? - .run(); - Ok(server) -} diff --git a/chapter04/src/telemetry.rs b/chapter04/src/telemetry.rs deleted file mode 100644 index 27168fb..0000000 --- a/chapter04/src/telemetry.rs +++ /dev/null @@ -1,29 +0,0 @@ -use tracing::subscriber::set_global_default; -use tracing::Subscriber; -use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; -use tracing_log::LogTracer; -use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; - -/// Compose multiple layers into a `tracing`'s subscriber. -/// -/// # Implementation Notes -/// -/// We are using `impl Subscriber` as return type to avoid having to spell out the actual -/// type of the returned subscriber, which is indeed quite complex. -pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sync + Send { - let env_filter = - EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter)); - let formatting_layer = BunyanFormattingLayer::new(name, std::io::stdout); - Registry::default() - .with(env_filter) - .with(JsonStorageLayer) - .with(formatting_layer) -} - -/// Register a subscriber as global default to process span data. -/// -/// It should only be called once! -pub fn init_subscriber(subscriber: impl Subscriber + Sync + Send) { - LogTracer::init().expect("Failed to set logger"); - set_global_default(subscriber).expect("Failed to set subscriber"); -} diff --git a/chapter04/tests/health_check.rs b/chapter04/tests/health_check.rs deleted file mode 100644 index c0ac230..0000000 --- a/chapter04/tests/health_check.rs +++ /dev/null @@ -1,143 +0,0 @@ -use chapter04::configuration::{get_configuration, DatabaseSettings}; -use chapter04::startup::run; -use chapter04::telemetry::{get_subscriber, init_subscriber}; -use sqlx::{Connection, Executor, PgConnection, PgPool}; -use std::net::TcpListener; -use uuid::Uuid; - -// Ensure that the `tracing` stack is only initialised once using `lazy_static` -lazy_static::lazy_static! { - static ref TRACING: () = { - let filter = if std::env::var("TEST_LOG").is_ok() { "debug" } else { "" }; - let subscriber = get_subscriber("test".into(), filter.into()); - init_subscriber(subscriber); - }; -} - -pub struct TestApp { - pub address: String, - pub db_pool: PgPool, -} - -async fn spawn_app() -> TestApp { - // The first time `initialize` is invoked the code in `TRACING` is executed. - // All other invocations will instead skip execution. - lazy_static::initialize(&TRACING); - - let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); - // We retrieve the port assigned to us by the OS - let port = listener.local_addr().unwrap().port(); - let address = format!("http://127.0.0.1:{}", port); - - let mut configuration = get_configuration().expect("Failed to read configuration."); - configuration.database.database_name = Uuid::new_v4().to_string(); - let connection_pool = configure_database(&configuration.database).await; - - let server = run(listener, connection_pool.clone()).expect("Failed to bind address"); - let _ = tokio::spawn(server); - TestApp { - address, - db_pool: connection_pool, - } -} - -pub async fn configure_database(config: &DatabaseSettings) -> PgPool { - // Create database - let mut connection = PgConnection::connect(&config.connection_string_without_db()) - .await - .expect("Failed to connect to Postgres"); - connection - .execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name)) - .await - .expect("Failed to create database."); - - // Migrate database - let connection_pool = PgPool::connect(&config.connection_string()) - .await - .expect("Failed to connect to Postgres."); - sqlx::migrate!("./migrations") - .run(&connection_pool) - .await - .expect("Failed to migrate the database"); - - connection_pool -} - -#[actix_rt::test] -async fn health_check_works() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - - // Act - let response = client - // Use the returned application address - .get(&format!("{}/health_check", &app.address)) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert!(response.status().is_success()); - assert_eq!(Some(0), response.content_length()); -} - -#[actix_rt::test] -async fn subscribe_returns_a_200_for_valid_form_data() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; - - // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(body) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert_eq!(200, response.status().as_u16()); - - let saved = sqlx::query!("SELECT email, name FROM subscriptions",) - .fetch_one(&app.db_pool) - .await - .expect("Failed to fetch saved subscription."); - - assert_eq!(saved.email, "ursula_le_guin@gmail.com"); - assert_eq!(saved.name, "le guin"); -} - -#[actix_rt::test] -async fn subscribe_returns_a_400_when_data_is_missing() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - let test_cases = vec![ - ("name=le%20guin", "missing the email"), - ("email=ursula_le_guin%40gmail.com", "missing the name"), - ("", "missing both name and email"), - ]; - - for (invalid_body, error_message) in test_cases { - // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(invalid_body) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert_eq!( - 400, - response.status().as_u16(), - // Additional customised error message on test failure - "The API did not fail with 400 Bad Request when the payload was {}.", - error_message - ); - } -} diff --git a/chapter05/.dockerignore b/chapter05/.dockerignore deleted file mode 100644 index 6c23612..0000000 --- a/chapter05/.dockerignore +++ /dev/null @@ -1,9 +0,0 @@ -.env -.dockerignore -spec.yaml -target/ -deploy/ -tests/ -Dockerfile -scripts/ -migrations/ \ No newline at end of file diff --git a/chapter05/.env b/chapter05/.env deleted file mode 100644 index 88cfb53..0000000 --- a/chapter05/.env +++ /dev/null @@ -1 +0,0 @@ -DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter" diff --git a/chapter05/Cargo.toml b/chapter05/Cargo.toml deleted file mode 100644 index 62cea9a..0000000 --- a/chapter05/Cargo.toml +++ /dev/null @@ -1,34 +0,0 @@ -[package] -name = "chapter05" -version = "0.1.0" -authors = ["LukeMathWalker "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[lib] -path = "src/lib.rs" - -[[bin]] -path = "src/main.rs" -name = "chapter05" - -[dependencies] -actix-web = "3.0.0" -actix-rt = "1.1.1" -tokio = "0.2.22" -serde = "1.0.115" -config = { version = "0.10.1", default-features = false, features = ["yaml"] } -sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] } -uuid = { version = "0.8.1", features = ["v4"] } -chrono = "0.4.15" -tracing = "0.1.19" -tracing-futures = "0.2.4" -tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] } -tracing-bunyan-formatter = "0.1.6" -tracing-log = "0.1.1" -tracing-actix-web = "0.2.0" -serde-aux = "1.0.1" - -[dev-dependencies] -reqwest = { version = "0.10.7", features = ["json"] } -lazy_static = "1.4.0" diff --git a/chapter05/Dockerfile b/chapter05/Dockerfile deleted file mode 100644 index 9b72852..0000000 --- a/chapter05/Dockerfile +++ /dev/null @@ -1,38 +0,0 @@ -FROM rust:1.47 AS planner -WORKDIR app -# We only pay the installation cost once, -# it will be cached from the second build onwards -# To ensure a reproducible build consider pinning -# the cargo-chef version with `--version X.X.X` -RUN cargo install cargo-chef -COPY . . -# Compute a lock-like file for our project -RUN cargo chef prepare --recipe-path recipe.json - -FROM rust:1.47 AS cacher -WORKDIR app -RUN cargo install cargo-chef -COPY --from=planner /app/recipe.json recipe.json -# Build our project dependencies, not our application! -RUN cargo chef cook --release --recipe-path recipe.json - -FROM rust:1.47 AS builder -WORKDIR app -# Copy over the cached dependencies -COPY --from=cacher /app/target target -COPY --from=cacher /usr/local/cargo /usr/local/cargo -COPY . . -# Build our application, leveraging the cached deps! -ENV SQLX_OFFLINE true -RUN cargo build --release --bin chapter05 - -FROM debian:buster-slim AS runtime -WORKDIR app -RUN apt-get update -y \ - && apt-get install -y --no-install-recommends openssl \ - # Clean up - && apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* -COPY --from=builder /app/target/release/chapter05 zero2prod -COPY configuration configuration -ENV APP_ENVIRONMENT production -ENTRYPOINT ["./zero2prod"] diff --git a/chapter05/configuration/base.yaml b/chapter05/configuration/base.yaml deleted file mode 100644 index e42d958..0000000 --- a/chapter05/configuration/base.yaml +++ /dev/null @@ -1,10 +0,0 @@ -application: - port: 8000 - host: 0.0.0.0 -database: - host: "localhost" - port: 5432 - username: "postgres" - password: "password" - database_name: "newsletter" - require_ssl: false \ No newline at end of file diff --git a/chapter05/configuration/local.yaml b/chapter05/configuration/local.yaml deleted file mode 100644 index 8fd67fa..0000000 --- a/chapter05/configuration/local.yaml +++ /dev/null @@ -1,4 +0,0 @@ -application: - host: 127.0.0.1 -database: - require_ssl: false diff --git a/chapter05/configuration/production.yaml b/chapter05/configuration/production.yaml deleted file mode 100644 index f3ac210..0000000 --- a/chapter05/configuration/production.yaml +++ /dev/null @@ -1,4 +0,0 @@ -application: - host: 0.0.0.0 -database: - require_ssl: true \ No newline at end of file diff --git a/chapter05/migrations/20200823135036_create_subscriptions_table.sql b/chapter05/migrations/20200823135036_create_subscriptions_table.sql deleted file mode 100644 index 2c0d262..0000000 --- a/chapter05/migrations/20200823135036_create_subscriptions_table.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Create Subscriptions Table -CREATE TABLE subscriptions( - id uuid NOT NULL, - PRIMARY KEY (id), - email TEXT NOT NULL UNIQUE, - name TEXT NOT NULL, - subscribed_at timestamptz NOT NULL -); \ No newline at end of file diff --git a/chapter05/scripts/init_db.sh b/chapter05/scripts/init_db.sh deleted file mode 100755 index cc1df04..0000000 --- a/chapter05/scripts/init_db.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash -set -x -set -eo pipefail - -# Check if a custom user has been set, otherwise default to 'postgres' -DB_USER=${POSTGRES_USER:=postgres} -# Check if a custom password has been set, otherwise default to 'password' -DB_PASSWORD="${POSTGRES_PASSWORD:=password}" -# Check if a custom password has been set, otherwise default to 'newsletter' -DB_NAME="${POSTGRES_DB:=newsletter}" -# Check if a custom port has been set, otherwise default to '5432' -DB_PORT="${POSTGRES_PORT:=5432}" - -# Allow to skip Docker if a dockerized Postgres database is already running -if [[ -z "${SKIP_DOCKER}" ]] -then - # Launch postgres using Docker - docker run \ - -e POSTGRES_USER=${DB_USER} \ - -e POSTGRES_PASSWORD=${DB_PASSWORD} \ - -e POSTGRES_DB=${DB_NAME} \ - -p "${DB_PORT}":5432 \ - -d postgres \ - postgres -N 1000 - # ^ Increased maximum number of connections for testing purposes -fi - -# Keep pinging Postgres until it's ready to accept commands -until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do - >&2 echo "Postgres is still unavailable - sleeping" - sleep 1 -done - ->&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" - -export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME} -sqlx database create -sqlx migrate run - ->&2 echo "Postgres has been migrated, ready to go!" diff --git a/chapter05/spec.yaml b/chapter05/spec.yaml deleted file mode 100644 index c2c0870..0000000 --- a/chapter05/spec.yaml +++ /dev/null @@ -1,54 +0,0 @@ -name: zero2prod -# See https://www.digitalocean.com/docs/app-platform/#regional-availability for the available options -# You can get region slugs from https://www.digitalocean.com/docs/platform/availability-matrix/ -# `fra` stands for Frankfurt (Germany - EU) -region: fra -services: - - name: zero2prod - # Relative to the repository root - dockerfile_path: chapter05/Dockerfile - source_dir: chapter05 - github: - branch: ch-05 - deploy_on_push: true - repo: LukeMathWalker/zero-to-production - # Active probe used by DigitalOcean's to ensure our application is healthy - health_check: - # The path to our health check endpoint! It turned out to be useful in the end! - http_path: /health_check - # The port the application will be listening on for incoming requests - # It should match what we specify in our configuration.yaml file! - http_port: 8000 - # For production workloads we'd go for at least two! - instance_count: 1 - # Let's keep the bill lean for now... - instance_size_slug: basic-xxs - # All incoming requests should be routed to our app - routes: - - path: / - envs: - - key: APP_DATABASE__USERNAME - scope: RUN_TIME - value: ${newsletter.USERNAME} - - key: APP_DATABASE__PASSWORD - scope: RUN_TIME - value: ${newsletter.PASSWORD} - - key: APP_DATABASE__HOST - scope: RUN_TIME - value: ${newsletter.HOSTNAME} - - key: APP_DATABASE__PORT - scope: RUN_TIME - value: ${newsletter.PORT} - - key: APP_DATABASE__DATABASE_NAME - scope: RUN_TIME - value: ${newsletter.DATABASE} -databases: - # PG = Postgres - - engine: PG - # Database name - name: newsletter - # Again, let's keep the bill lean - num_nodes: 1 - size: db-s-dev-database - # Postgres version - using the latest here - version: "12" \ No newline at end of file diff --git a/chapter05/sqlx-data.json b/chapter05/sqlx-data.json deleted file mode 100644 index 230520f..0000000 --- a/chapter05/sqlx-data.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "db": "PostgreSQL", - "793f0df728d217c204123f12e4eafd6439db2d49d0cb506618ae9e780c7e0558": { - "query": "\n INSERT INTO subscriptions (id, email, name, subscribed_at)\n VALUES ($1, $2, $3, $4)\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Uuid", - "Text", - "Text", - "Timestamptz" - ] - }, - "nullable": [] - } - } -} \ No newline at end of file diff --git a/chapter05/src/configuration.rs b/chapter05/src/configuration.rs deleted file mode 100644 index 8281ca0..0000000 --- a/chapter05/src/configuration.rs +++ /dev/null @@ -1,104 +0,0 @@ -use serde_aux::field_attributes::deserialize_number_from_string; -use sqlx::postgres::{PgConnectOptions, PgSslMode}; -use std::convert::{TryFrom, TryInto}; - -#[derive(serde::Deserialize)] -pub struct Settings { - pub database: DatabaseSettings, - pub application: ApplicationSettings, -} - -#[derive(serde::Deserialize)] -pub struct ApplicationSettings { - #[serde(deserialize_with = "deserialize_number_from_string")] - pub port: u16, - pub host: String, -} - -#[derive(serde::Deserialize)] -pub struct DatabaseSettings { - pub username: String, - pub password: String, - #[serde(deserialize_with = "deserialize_number_from_string")] - pub port: u16, - pub host: String, - pub database_name: String, - pub require_ssl: bool, -} - -impl DatabaseSettings { - pub fn without_db(&self) -> PgConnectOptions { - let ssl_mode = if self.require_ssl { - PgSslMode::Require - } else { - PgSslMode::Prefer - }; - PgConnectOptions::new() - .host(&self.host) - .username(&self.username) - .password(&self.password) - .port(self.port) - .ssl_mode(ssl_mode) - } - - pub fn with_db(&self) -> PgConnectOptions { - self.without_db().database(&self.database_name) - } -} - -pub fn get_configuration() -> Result { - let mut settings = config::Config::default(); - let base_path = std::env::current_dir().expect("Failed to determine the current directory"); - let configuration_directory = base_path.join("configuration"); - - // Read the "default" configuration file - settings.merge(config::File::from(configuration_directory.join("base")).required(true))?; - - // Detect the running environment. - // Default to `local` if unspecified. - let environment: Environment = std::env::var("APP_ENVIRONMENT") - .unwrap_or_else(|_| "local".into()) - .try_into() - .expect("Failed to parse APP_ENVIRONMENT."); - - // Layer on the environment-specific values. - settings.merge( - config::File::from(configuration_directory.join(environment.as_str())).required(true), - )?; - - // Add in settings from environment variables (with a prefix of APP and '__' as separator) - // E.g. `APP_APPLICATION__PORT=5001 would set `Settings.application.port` - settings.merge(config::Environment::with_prefix("app").separator("__"))?; - - settings.try_into() -} - -/// The possible runtime environment for our application. -pub enum Environment { - Local, - Production, -} - -impl Environment { - pub fn as_str(&self) -> &'static str { - match self { - Environment::Local => "local", - Environment::Production => "production", - } - } -} - -impl TryFrom for Environment { - type Error = String; - - fn try_from(s: String) -> Result { - match s.to_lowercase().as_str() { - "local" => Ok(Self::Local), - "production" => Ok(Self::Production), - other => Err(format!( - "{} is not a supported environment. Use either `local` or `production`.", - other - )), - } - } -} diff --git a/chapter05/src/lib.rs b/chapter05/src/lib.rs deleted file mode 100644 index 5d8e21e..0000000 --- a/chapter05/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![allow(clippy::toplevel_ref_arg)] -pub mod configuration; -pub mod routes; -pub mod startup; -pub mod telemetry; diff --git a/chapter05/src/main.rs b/chapter05/src/main.rs deleted file mode 100644 index 102e50a..0000000 --- a/chapter05/src/main.rs +++ /dev/null @@ -1,26 +0,0 @@ -use chapter05::configuration::get_configuration; -use chapter05::startup::run; -use chapter05::telemetry::{get_subscriber, init_subscriber}; -use sqlx::postgres::PgPoolOptions; -use std::net::TcpListener; - -#[actix_rt::main] -async fn main() -> std::io::Result<()> { - let subscriber = get_subscriber("zero2prod".into(), "info".into()); - init_subscriber(subscriber); - - let configuration = get_configuration().expect("Failed to read configuration."); - let connection_pool = PgPoolOptions::new() - .connect_timeout(std::time::Duration::from_secs(2)) - .connect_with(configuration.database.with_db()) - .await - .expect("Failed to connect to Postgres."); - - let address = format!( - "{}:{}", - configuration.application.host, configuration.application.port - ); - let listener = TcpListener::bind(address)?; - run(listener, connection_pool)?.await?; - Ok(()) -} diff --git a/chapter05/src/routes/health_check.rs b/chapter05/src/routes/health_check.rs deleted file mode 100644 index d7eb4e0..0000000 --- a/chapter05/src/routes/health_check.rs +++ /dev/null @@ -1,5 +0,0 @@ -use actix_web::HttpResponse; - -pub async fn health_check() -> HttpResponse { - HttpResponse::Ok().finish() -} diff --git a/chapter05/src/routes/mod.rs b/chapter05/src/routes/mod.rs deleted file mode 100644 index 90ffeed..0000000 --- a/chapter05/src/routes/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod health_check; -mod subscriptions; - -pub use health_check::*; -pub use subscriptions::*; diff --git a/chapter05/src/routes/subscriptions.rs b/chapter05/src/routes/subscriptions.rs deleted file mode 100644 index fd801af..0000000 --- a/chapter05/src/routes/subscriptions.rs +++ /dev/null @@ -1,52 +0,0 @@ -use actix_web::{web, HttpResponse}; -use chrono::Utc; -use sqlx::PgPool; -use uuid::Uuid; - -#[derive(serde::Deserialize)] -pub struct FormData { - email: String, - name: String, -} - -#[tracing::instrument( - name = "Adding a new subscriber", - skip(form, pool), - fields( - email = %form.email, - name = %form.name - ) -)] -pub async fn subscribe( - form: web::Form, - pool: web::Data, -) -> Result { - insert_subscriber(&pool, &form) - .await - .map_err(|_| HttpResponse::InternalServerError().finish())?; - Ok(HttpResponse::Ok().finish()) -} - -#[tracing::instrument( - name = "Saving new subscriber details in the database", - skip(form, pool) -)] -pub async fn insert_subscriber(pool: &PgPool, form: &FormData) -> Result<(), sqlx::Error> { - sqlx::query!( - r#" - INSERT INTO subscriptions (id, email, name, subscribed_at) - VALUES ($1, $2, $3, $4) - "#, - Uuid::new_v4(), - form.email, - form.name, - Utc::now() - ) - .execute(pool) - .await - .map_err(|e| { - tracing::error!("Failed to execute query: {:?}", e); - e - })?; - Ok(()) -} diff --git a/chapter05/src/startup.rs b/chapter05/src/startup.rs deleted file mode 100644 index d091772..0000000 --- a/chapter05/src/startup.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::routes::{health_check, subscribe}; -use actix_web::dev::Server; -use actix_web::web::Data; -use actix_web::{web, App, HttpServer}; -use sqlx::PgPool; -use std::net::TcpListener; -use tracing_actix_web::TracingLogger; - -pub fn run(listener: TcpListener, db_pool: PgPool) -> Result { - let db_pool = Data::new(db_pool); - let server = HttpServer::new(move || { - App::new() - .wrap(TracingLogger) - .route("/health_check", web::get().to(health_check)) - .route("/subscriptions", web::post().to(subscribe)) - .app_data(db_pool.clone()) - }) - .listen(listener)? - .run(); - Ok(server) -} diff --git a/chapter05/src/telemetry.rs b/chapter05/src/telemetry.rs deleted file mode 100644 index 27168fb..0000000 --- a/chapter05/src/telemetry.rs +++ /dev/null @@ -1,29 +0,0 @@ -use tracing::subscriber::set_global_default; -use tracing::Subscriber; -use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; -use tracing_log::LogTracer; -use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; - -/// Compose multiple layers into a `tracing`'s subscriber. -/// -/// # Implementation Notes -/// -/// We are using `impl Subscriber` as return type to avoid having to spell out the actual -/// type of the returned subscriber, which is indeed quite complex. -pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sync + Send { - let env_filter = - EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter)); - let formatting_layer = BunyanFormattingLayer::new(name, std::io::stdout); - Registry::default() - .with(env_filter) - .with(JsonStorageLayer) - .with(formatting_layer) -} - -/// Register a subscriber as global default to process span data. -/// -/// It should only be called once! -pub fn init_subscriber(subscriber: impl Subscriber + Sync + Send) { - LogTracer::init().expect("Failed to set logger"); - set_global_default(subscriber).expect("Failed to set subscriber"); -} diff --git a/chapter05/tests/health_check.rs b/chapter05/tests/health_check.rs deleted file mode 100644 index 11db352..0000000 --- a/chapter05/tests/health_check.rs +++ /dev/null @@ -1,143 +0,0 @@ -use chapter05::configuration::{get_configuration, DatabaseSettings}; -use chapter05::startup::run; -use chapter05::telemetry::{get_subscriber, init_subscriber}; -use sqlx::{Connection, Executor, PgConnection, PgPool}; -use std::net::TcpListener; -use uuid::Uuid; - -// Ensure that the `tracing` stack is only initialised once using `lazy_static` -lazy_static::lazy_static! { - static ref TRACING: () = { - let filter = if std::env::var("TEST_LOG").is_ok() { "debug" } else { "" }; - let subscriber = get_subscriber("test".into(), filter.into()); - init_subscriber(subscriber); - }; -} - -pub struct TestApp { - pub address: String, - pub db_pool: PgPool, -} - -async fn spawn_app() -> TestApp { - // The first time `initialize` is invoked the code in `TRACING` is executed. - // All other invocations will instead skip execution. - lazy_static::initialize(&TRACING); - - let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); - // We retrieve the port assigned to us by the OS - let port = listener.local_addr().unwrap().port(); - let address = format!("http://127.0.0.1:{}", port); - - let mut configuration = get_configuration().expect("Failed to read configuration."); - configuration.database.database_name = Uuid::new_v4().to_string(); - let connection_pool = configure_database(&configuration.database).await; - - let server = run(listener, connection_pool.clone()).expect("Failed to bind address"); - let _ = tokio::spawn(server); - TestApp { - address, - db_pool: connection_pool, - } -} - -pub async fn configure_database(config: &DatabaseSettings) -> PgPool { - // Create database - let mut connection = PgConnection::connect_with(&config.without_db()) - .await - .expect("Failed to connect to Postgres"); - connection - .execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name)) - .await - .expect("Failed to create database."); - - // Migrate database - let connection_pool = PgPool::connect_with(config.with_db()) - .await - .expect("Failed to connect to Postgres."); - sqlx::migrate!("./migrations") - .run(&connection_pool) - .await - .expect("Failed to migrate the database"); - - connection_pool -} - -#[actix_rt::test] -async fn health_check_works() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - - // Act - let response = client - // Use the returned application address - .get(&format!("{}/health_check", &app.address)) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert!(response.status().is_success()); - assert_eq!(Some(0), response.content_length()); -} - -#[actix_rt::test] -async fn subscribe_returns_a_200_for_valid_form_data() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; - - // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(body) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert_eq!(200, response.status().as_u16()); - - let saved = sqlx::query!("SELECT email, name FROM subscriptions",) - .fetch_one(&app.db_pool) - .await - .expect("Failed to fetch saved subscription."); - - assert_eq!(saved.email, "ursula_le_guin@gmail.com"); - assert_eq!(saved.name, "le guin"); -} - -#[actix_rt::test] -async fn subscribe_returns_a_400_when_data_is_missing() { - // Arrange - let app = spawn_app().await; - let client = reqwest::Client::new(); - let test_cases = vec![ - ("name=le%20guin", "missing the email"), - ("email=ursula_le_guin%40gmail.com", "missing the name"), - ("", "missing both name and email"), - ]; - - for (invalid_body, error_message) in test_cases { - // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(invalid_body) - .send() - .await - .expect("Failed to execute request."); - - // Assert - assert_eq!( - 400, - response.status().as_u16(), - // Additional customised error message on test failure - "The API did not fail with 400 Bad Request when the payload was {}.", - error_message - ); - } -} diff --git a/chapter03-0/src/lib.rs b/src/lib.rs similarity index 100% rename from chapter03-0/src/lib.rs rename to src/lib.rs diff --git a/chapter03-0/src/main.rs b/src/main.rs similarity index 88% rename from chapter03-0/src/main.rs rename to src/main.rs index cb6021e..25f27db 100644 --- a/chapter03-0/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -use chapter03_0::run; use std::net::TcpListener; +use zero2prod::run; #[actix_rt::main] async fn main() -> std::io::Result<()> { diff --git a/chapter03-0/tests/health_check.rs b/tests/health_check.rs similarity index 97% rename from chapter03-0/tests/health_check.rs rename to tests/health_check.rs index 98596a1..39bbde6 100644 --- a/chapter03-0/tests/health_check.rs +++ b/tests/health_check.rs @@ -1,5 +1,5 @@ -use chapter03_0::run; use std::net::TcpListener; +use zero2prod::run; fn spawn_app() -> String { let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");