From f0abdf73066ef12eade1d52df1b4a9a35f7a3c70 Mon Sep 17 00:00:00 2001 From: Dario A Lencina-Talarico Date: Fri, 21 Jul 2023 15:21:17 -0400 Subject: [PATCH] Retrofit template with all the videocall stuff (#23) * update ci, cache crates, go fast * save * update docker images * add help commands * config.toml * justin tv and twitch * clippy * fmt * remove cache --------- Co-authored-by: Dario Lencina --- .github/workflows/backend.yaml | 35 +++++++++++++ .github/workflows/cargo-audit.yaml | 22 -------- .github/workflows/cargo-lint.yaml | 32 ------------ .github/workflows/cargo-test.yaml | 42 ---------------- .github/workflows/frontend.yaml | 32 ++++++++++++ Makefile | 16 +++++- actix-api/.cargo/config.toml | 2 + actix-api/Cargo.lock | 80 ++++++++++++++++++++++-------- actix-api/Cargo.toml | 4 +- actix-api/src/auth/mod.rs | 6 ++- actix-api/src/main.rs | 6 +-- docker/Dockerfile.actix | 7 ++- docker/Dockerfile.yew | 10 ++-- docker/docker-compose.yaml | 7 ++- yew-ui/.cargo/config.toml | 5 ++ 15 files changed, 174 insertions(+), 132 deletions(-) create mode 100644 .github/workflows/backend.yaml delete mode 100644 .github/workflows/cargo-audit.yaml delete mode 100644 .github/workflows/cargo-lint.yaml delete mode 100644 .github/workflows/cargo-test.yaml create mode 100644 .github/workflows/frontend.yaml create mode 100644 actix-api/.cargo/config.toml create mode 100644 yew-ui/.cargo/config.toml diff --git a/.github/workflows/backend.yaml b/.github/workflows/backend.yaml new file mode 100644 index 0000000..810f529 --- /dev/null +++ b/.github/workflows/backend.yaml @@ -0,0 +1,35 @@ +# Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md + +on: + pull_request: + paths: + - 'actix-api/**' + - 'types/**' + - Dockerfile.actix + +name: check backend + +env: + ACTIX_PORT: 8080 + TRUNK_SERVE_PORT: 80 + TRUNK_SERVE_HOST: localhost + OAUTH_CLIENT_ID: OAUTH_CLIENT_ID + OAUTH_AUTH_URL: OAUTH_AUTH_URL + OAUTH_TOKEN_URL: OAUTH_TOKEN_URL + OAUTH_CLIENT_SECRET: OAUTH_CLIENT_SECRET + OAUTH_REDIRECT_URL: http://localhost:${ACTIX_PORT:-8080}/login/callback + RUST_LOG: info + PG_URL: postgres://postgres:docker@postgres:5432/actix-api-db?sslmode=disable + +jobs: + test: + name: cargo test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@1.66 + with: + components: clippy, rustfmt + - run: cd actix-api && cargo clippy -- --deny warnings + - run: cd actix-api && cargo fmt --check + - run: cd actix-api && cargo test \ No newline at end of file diff --git a/.github/workflows/cargo-audit.yaml b/.github/workflows/cargo-audit.yaml deleted file mode 100644 index bed568d..0000000 --- a/.github/workflows/cargo-audit.yaml +++ /dev/null @@ -1,22 +0,0 @@ -name: Security Audit -on: - schedule: - - cron: '0 0 * * *' - push: - paths: - - '**/Cargo.toml' - - '**/Cargo.lock' -jobs: - security_audit: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - uses: security-union/audit-check@6 - with: - token: ${{ secrets.GITHUB_TOKEN }} - cargo-lock-path: ./actix-api - - uses: security-union/audit-check@7 - with: - token: ${{ secrets.GITHUB_TOKEN }} - cargo-lock-path: ./yew-ui - \ No newline at end of file diff --git a/.github/workflows/cargo-lint.yaml b/.github/workflows/cargo-lint.yaml deleted file mode 100644 index a5bab30..0000000 --- a/.github/workflows/cargo-lint.yaml +++ /dev/null @@ -1,32 +0,0 @@ -on: [pull_request] - -name: Lints - -env: - ACTIX_PORT: 8080 - TRUNK_SERVE_PORT: 80 - TRUNK_SERVE_HOST: localhost - OAUTH_CLIENT_ID: blah - OAUTH_AUTH_URL: blah - OAUTH_TOKEN_URL: blah - OAUTH_CLIENT_SECRET: blah - OAUTH_REDIRECT_URL: http://localhost:8080/login/callback - RUST_LOG: info - PG_URL: postgres://postgres:docker@postgres:5432/actix-api-db?sslmode=disable - -jobs: - fmt: - name: Rust Fmt - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - components: rustfmt - - uses: actions-rs/cargo@v1 - with: - command: fmt - args: --manifest-path ./actix-api/Cargo.toml --all -- --check diff --git a/.github/workflows/cargo-test.yaml b/.github/workflows/cargo-test.yaml deleted file mode 100644 index 78a63dd..0000000 --- a/.github/workflows/cargo-test.yaml +++ /dev/null @@ -1,42 +0,0 @@ -# Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md - -on: [pull_request] - -name: cargo test - -env: - ACTIX_PORT: 8080 - TRUNK_SERVE_PORT: 80 - TRUNK_SERVE_HOST: localhost - OAUTH_CLIENT_ID: blah - OAUTH_AUTH_URL: blah - OAUTH_TOKEN_URL: blah - OAUTH_CLIENT_SECRET: blah - OAUTH_REDIRECT_URL: http://localhost:8080/login/callback - RUST_LOG: info - PG_URL: postgres://postgres:docker@postgres:5432/actix-api-db?sslmode=disable - -jobs: - - test: - name: Test Suite - runs-on: ubuntu-latest - steps: - - name: Checkout sources - uses: actions/checkout@v2 - with: - working-directory: ./actix-api - - - name: Install stable toolchain - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - - name: Run cargo test - uses: actions-rs/cargo@v1 - continue-on-error: false # WARNING: only for this example, remove it! - with: - command: test - args: --manifest-path ./actix-api/Cargo.toml \ No newline at end of file diff --git a/.github/workflows/frontend.yaml b/.github/workflows/frontend.yaml new file mode 100644 index 0000000..53e7cb1 --- /dev/null +++ b/.github/workflows/frontend.yaml @@ -0,0 +1,32 @@ +# Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md + +on: + pull_request: + paths: + - 'yew-ui/**' + - 'types/**' + - Dockerfile.yew + - nginx.conf + + +name: check frontend + +env: + ACTIX_PORT: {ACTIX_PORT:-8080} + TRUNK_SERVE_PORT: ${TRUNK_SERVE_PORT:-80} + ENABLE_OAUTH: true + LOGIN_URL: http://localhost:${ACTIX_PORT:-8080}/login + +jobs: + test: + name: cargo test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@1.66 + with: + components: clippy, rustfmt + # - run: cd yew-ui && cargo clippy -- --deny warnings + - run: rustup target add wasm32-unknown-unknown + - run: cd yew-ui && cargo fmt --check + - run: cd yew-ui && cargo test \ No newline at end of file diff --git a/Makefile b/Makefile index 74d4955..b46b7d8 100644 --- a/Makefile +++ b/Makefile @@ -10,4 +10,18 @@ build: docker compose -f docker/docker-compose.yaml build connect_to_db: - docker compose -f docker/docker-compose.yaml run postgres bash -c "psql -h postgres -d actix-api-db -U postgres" \ No newline at end of file + docker compose -f docker/docker-compose.yaml run postgres bash -c "psql -h postgres -d actix-api-db -U postgres" + + +fmt: + docker compose -f docker/docker-compose.yaml run yew-ui bash -c "cd app/yew-ui && cargo fmt" + docker compose -f docker/docker-compose.yaml run actix-api bash -c "cd app/actix-api && cargo fmt && cd ../types && cargo fmt" + +clippy-fix: + docker compose -f docker/docker-compose.yaml run yew-ui bash -c "cd app/yew-ui && cargo clippy --fix" + docker compose -f docker/docker-compose.yaml run actix-api bash -c "cd app/actix-api && cargo clippy --fix && cd ../types && cargo clippy --fix" + +check: + # The ui does not support clippy yet + docker compose -f docker/docker-compose.yaml run yew-ui bash -c "cd app/yew-ui && cargo clippy --all -- --deny warnings && cargo fmt --check" + docker compose -f docker/docker-compose.yaml run actix-api bash -c "cd app/actix-api && cargo clippy --all -- --deny warnings && cargo fmt --check" \ No newline at end of file diff --git a/actix-api/.cargo/config.toml b/actix-api/.cargo/config.toml new file mode 100644 index 0000000..656e08b --- /dev/null +++ b/actix-api/.cargo/config.toml @@ -0,0 +1,2 @@ +[net] +git-fetch-with-cli = true \ No newline at end of file diff --git a/actix-api/Cargo.lock b/actix-api/Cargo.lock index 50a5bf9..4773d8c 100644 --- a/actix-api/Cargo.lock +++ b/actix-api/Cargo.lock @@ -81,16 +81,16 @@ dependencies = [ [[package]] name = "actix-http" -version = "3.2.2" +version = "3.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c83abf9903e1f0ad9973cc4f7b9767fd5a03a583f51a5b7a339e07987cd2724" +checksum = "c2079246596c18b4a33e274ae10c0e50613f4d32a4198e09c7b93771013fed74" dependencies = [ "actix-codec", "actix-rt", "actix-service", "actix-utils", - "ahash", - "base64 0.13.1", + "ahash 0.8.3", + "base64 0.21.0", "bitflags", "brotli", "bytes", @@ -112,6 +112,8 @@ dependencies = [ "rand", "sha1", "smallvec", + "tokio", + "tokio-util", "tracing", "zstd", ] @@ -190,9 +192,9 @@ dependencies = [ [[package]] name = "actix-web" -version = "4.2.1" +version = "4.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d48f7b6534e06c7bfc72ee91db7917d4af6afe23e7d223b51e68fffbb21e96b9" +checksum = "cd3cb42f9566ab176e1ef0b8b3a896529062b4efc6be0123046095914c4c1c96" dependencies = [ "actix-codec", "actix-http", @@ -203,7 +205,7 @@ dependencies = [ "actix-service", "actix-utils", "actix-web-codegen", - "ahash", + "ahash 0.7.6", "bytes", "bytestring", "cfg-if", @@ -231,9 +233,9 @@ dependencies = [ [[package]] name = "actix-web-codegen" -version = "4.1.0" +version = "4.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fa9362663c8643d67b2d5eafba49e4cb2c8a053a29ed00a0bea121f17c76b13" +checksum = "2262160a7ae29e3415554a3f1fc04c764b1540c116aa524683208078b7a75bc9" dependencies = [ "actix-router", "proc-macro2", @@ -269,6 +271,18 @@ dependencies = [ "version_check", ] +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if", + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.20" @@ -1101,7 +1115,7 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -1253,7 +1267,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -1565,7 +1579,7 @@ version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "713cfb06c7059f3588fb8044c0fad1d09e3c01d225e25b9220dbfdcf16dbb1b3" dependencies = [ - "windows-sys", + "windows-sys 0.42.0", ] [[package]] @@ -1874,9 +1888,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.24.1" +version = "1.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d9f76183f91ecfb55e1d7d5602bd1d979e38a3a522fe900241cf195624d67ae" +checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" dependencies = [ "autocfg", "bytes", @@ -1888,7 +1902,7 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "socket2", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -2211,6 +2225,30 @@ dependencies = [ "windows_x86_64_msvc", ] +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.1" @@ -2264,18 +2302,18 @@ dependencies = [ [[package]] name = "zstd" -version = "0.11.2+zstd.1.5.2" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4" +checksum = "1a27595e173641171fc74a1232b7b1c7a7cb6e18222c11e9dfb9888fa424c53c" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "5.0.2+zstd.1.5.2" +version = "6.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db" +checksum = "ee98ffd0b48ee95e6c5168188e44a54550b1564d9d530ee21d5f0eaed1069581" dependencies = [ "libc", "zstd-sys", @@ -2283,9 +2321,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "2.0.5+zstd.1.5.2" +version = "2.0.8+zstd.1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edc50ffce891ad571e9f9afe5039c4837bede781ac4bb13052ed7ae695518596" +checksum = "5556e6ee25d32df2586c098bbfa278803692a20d0ab9565e049480d52707ec8c" dependencies = [ "cc", "libc", diff --git a/actix-api/Cargo.toml b/actix-api/Cargo.toml index 4efe387..0371c6b 100644 --- a/actix-api/Cargo.toml +++ b/actix-api/Cargo.toml @@ -9,8 +9,8 @@ description = "Actix-web backend" [dependencies] actix = "0.13.0" -actix-cors = "0.6.1" -actix-web = "4.1.0" +actix-cors = "0.6.4" +actix-web = "4.3.1" types = { path= "../types"} serde = "1.0.140" reqwest = { version = "0.11.11", features = ["json"]} diff --git a/actix-api/src/auth/mod.rs b/actix-api/src/auth/mod.rs index 75a190d..8dad1d1 100644 --- a/actix-api/src/auth/mod.rs +++ b/actix-api/src/auth/mod.rs @@ -131,8 +131,10 @@ pub async fn request_token( ]; let response = client.post(oauth_token_url).form(¶ms).send().await?; let oauth_response: OAuthResponse = response.json().await?; - let claims: Vec<&str> = oauth_response.id_token.split(".").collect(); - let claims_chunk = claims.get(1).ok_or(anyhow!("Unable to parse jwt token"))?; + let claims: Vec<&str> = oauth_response.id_token.split('.').collect(); + let claims_chunk = claims + .get(1) + .ok_or_else(|| anyhow!("Unable to parse jwt token"))?; let decoded_claims = DecodedJwtPartClaims::from_jwt_part_claims(claims_chunk)?; Ok((oauth_response, decoded_claims.deserialize()?)) } diff --git a/actix-api/src/main.rs b/actix-api/src/main.rs index 0f69f34..4cbe9b7 100644 --- a/actix-api/src/main.rs +++ b/actix-api/src/main.rs @@ -28,7 +28,7 @@ const SCOPE: &str = "email%20profile%20openid"; const ACTIX_PORT: &str = std::env!("ACTIX_PORT"); const UI_PORT: &str = std::env!("TRUNK_SERVE_PORT"); const UI_HOST: &str = std::env!("TRUNK_SERVE_HOST"); -const AFTER_LOGIN_URL: &'static str = concat!("http://localhost:", std::env!("TRUNK_SERVE_PORT")); +const AFTER_LOGIN_URL: &str = concat!("http://localhost:", std::env!("TRUNK_SERVE_PORT")); pub mod auth; pub mod db; @@ -48,7 +48,7 @@ async fn login(pool: web::Data) -> Result { // 2. Generate and Store OAuth Request. let (csrf_token, pkce_challenge) = { let pool = pool2.clone(); - web::block(move || generate_and_store_oauth_request(pool.clone())).await? + web::block(move || generate_and_store_oauth_request(pool)).await? } .map_err(|e| { log::error!("{:?}", e); @@ -168,7 +168,7 @@ async fn main() -> std::io::Result<()> { let pool = get_pool(); App::new() - .app_data(web::Data::new(pool.clone())) + .app_data(web::Data::new(pool)) .wrap(cors) .service(greet) .service(handle_google_oauth_callback) diff --git a/docker/Dockerfile.actix b/docker/Dockerfile.actix index 0d0b92c..0334305 100644 --- a/docker/Dockerfile.actix +++ b/docker/Dockerfile.actix @@ -1,4 +1,7 @@ -FROM rust:1.62-slim-bullseye +FROM rust:1.70-slim-bullseye -RUN apt-get --yes update && apt-get --yes install pkg-config libssl-dev +RUN apt-get --yes update && apt-get --yes install curl git pkg-config libssl-dev +RUN curl https://github.com/amacneil/dbmate/releases/download/v2.4.0/dbmate-linux-amd64 -L -o /usr/bin/dbmate && chmod +x /usr/bin/dbmate RUN cargo install cargo-watch +RUN rustup component add clippy-preview +RUN rustup component add rustfmt \ No newline at end of file diff --git a/docker/Dockerfile.yew b/docker/Dockerfile.yew index e5e40c2..a15c9ef 100644 --- a/docker/Dockerfile.yew +++ b/docker/Dockerfile.yew @@ -1,6 +1,8 @@ -FROM rust:1.62-slim-bullseye +FROM rust:1.70-slim-bullseye -RUN apt-get --yes update && apt-get --yes install pkg-config libssl-dev -RUN cargo install trunk wasm-bindgen-cli -RUN rustup target add wasm32-unknown-unknown +RUN apt-get --yes update && apt-get --yes install curl git pkg-config libssl-dev +RUN curl https://github.com/amacneil/dbmate/releases/download/v2.4.0/dbmate-linux-amd64 -L -o /usr/bin/dbmate && chmod +x /usr/bin/dbmate +RUN cargo install cargo-watch +RUN rustup component add clippy-preview +RUN rustup component add rustfmt diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 0e9dff8..0f304a5 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -7,6 +7,7 @@ services: source: ../ target: /app - /app/yew-ui/target + - yew-ui-cargo-registry-cache:/usr/local/cargo/registry build: dockerfile: ../docker/Dockerfile.yew command: bash -c "cd app/yew-ui && trunk serve --address 0.0.0.0 --port ${TRUNK_SERVE_PORT:-80}" @@ -40,6 +41,7 @@ services: source: ../ target: /app - /app/actix-api/target + - actix-web-cargo-registry-cache:/usr/local/cargo/registry depends_on: - postgres @@ -59,4 +61,7 @@ services: POSTGRES_PASSWORD: "docker" PGPASSWORD: "docker" ports: - - 5432 \ No newline at end of file + - 5432 +volumes: + actix-web-cargo-registry-cache: + yew-ui-cargo-registry-cache: \ No newline at end of file diff --git a/yew-ui/.cargo/config.toml b/yew-ui/.cargo/config.toml new file mode 100644 index 0000000..f62806a --- /dev/null +++ b/yew-ui/.cargo/config.toml @@ -0,0 +1,5 @@ +[net] +git-fetch-with-cli = true + +[target.wasm32-unknown-unknown] +runner = 'wasm-bindgen-test-runner' \ No newline at end of file