From ab9040c5d938335323614a5d6b43dad8aee1ffda Mon Sep 17 00:00:00 2001 From: itsscb Date: Mon, 2 Dec 2024 21:32:27 +0100 Subject: [PATCH] challenge: -1 --- .gitignore | 4 +- Cargo.toml | 13 ++++++ flake.lock | 96 +++++++++++++++++++++++++++++++++++++++ flake.nix | 51 +++++++++++++++++++++ src/lib.rs | 52 +++++++++++++++++++++ src/main.rs | 10 ++++ src/routes/hello_bird.rs | 4 ++ src/routes/hello_world.rs | 4 ++ src/routes/minus_one.rs | 15 ++++++ src/routes/mod.rs | 7 +++ 10 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/routes/hello_bird.rs create mode 100644 src/routes/hello_world.rs create mode 100644 src/routes/minus_one.rs create mode 100644 src/routes/mod.rs diff --git a/.gitignore b/.gitignore index d01bd1a..00e8ec6 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,6 @@ Cargo.lock # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea//target +.shuttle* +Secrets*.toml diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cf633a0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "itsscb-shuttlings-cch24" +version = "0.1.0" +edition = "2021" + +[dependencies] +axum = "0.7.4" +shuttle-axum = "0.49.0" +shuttle-runtime = "0.49.0" +tokio = "1.28.2" + +[dev-dependencies] +axum-test = "16.4.0" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..cf0a9ce --- /dev/null +++ b/flake.lock @@ -0,0 +1,96 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1733064805, + "narHash": "sha256-7NbtSLfZO0q7MXPl5hzA0sbVJt6pWxxtGWbaVUDDmjs=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "31d66ae40417bb13765b0ad75dd200400e98de84", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1728538411, + "narHash": "sha256-f0SBJz1eZ2yOuKUr5CA9BHULGXVSn6miBuUWdTyhUhU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b69de56fac8c2b6f8fd27f2eca01dcda8e0a4221", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1733106880, + "narHash": "sha256-aJmAIjZfWfPSWSExwrYBLRgXVvgF5LP1vaeUGOOIQ98=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "e66c0d43abf5bdefb664c3583ca8994983c332ae", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..613f068 --- /dev/null +++ b/flake.nix @@ -0,0 +1,51 @@ +{ + description = "Example Rust development environment for Zero to Nix"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + rust-overlay.url = "github:oxalica/rust-overlay"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + rustToolchain = pkgs.rust-bin.nightly.latest.default.override { + extensions = [ "rust-src" "rust-analyzer" "clippy" "rustfmt" ]; + targets = [ "x86_64-unknown-linux-gnu" ]; + }; + in + { + devShells.default = pkgs.mkShell { + buildInputs = with pkgs; [ + rustToolchain + clippy + + + cargo-shuttle + cargo-edit + cargo-binstall + bacon + + openssl + pkg-config + ]; + + shellHook = '' + export PATH=${rustToolchain}/bin:$PATH + export RUSTC_VERSION=$(rustc --version) + export RUST_SRC_PATH="${rustToolchain}/lib/rustlib/src/rust/library" + export OPENSSL_DIR="${pkgs.openssl.dev}" + export OPENSSL_LIB_DIR="${pkgs.openssl.out}/lib" + export OPENSSL_INCLUDE_DIR="${pkgs.openssl.dev}/include" + ''; + + packages = pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]); + }; + } + ); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..3a68250 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,52 @@ +mod routes; + +pub use routes::hello_world; +use routes::{hello_bird, minus_one}; + +pub fn router() -> axum::Router { + use axum::routing::get; + use axum::Router; + + Router::new() + .route("/hello_world", get(hello_world)) + .route("/-1/seek", get(minus_one)) + .route("/", get(hello_bird)) +} + +#[cfg(test)] +mod test { + use super::*; + use axum::http::StatusCode; + use axum_test::TestServer; + + fn test_server() -> TestServer { + TestServer::new(router()).unwrap() + } + + #[tokio::test] + async fn test_hello_world() { + let server = test_server(); + + let response = server.get("/hello_world").await; + response.assert_status_ok(); + response.assert_text("Hello, world!"); + } + + #[tokio::test] + async fn test_hello_bird() { + let server = test_server(); + + let response = server.get("/").await; + response.assert_status_ok(); + response.assert_text("Hello, bird!"); + } + + #[tokio::test] + async fn test_minus_one() { + let server = test_server(); + + let response = server.get("/-1/seek").await; + response.assert_header("location", "https://www.youtube.com/watch?v=9Gc4QTqslN4"); + response.assert_status(StatusCode::FOUND); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f81558f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,10 @@ +use axum::{routing::get, Router}; +use itsscb_shuttlings_cch24::hello_world; + +#[shuttle_runtime::main] +#[allow(clippy::unused_async)] +async fn main() -> shuttle_axum::ShuttleAxum { + let router = Router::new().route("/", get(hello_world)); + + Ok(router.into()) +} diff --git a/src/routes/hello_bird.rs b/src/routes/hello_bird.rs new file mode 100644 index 0000000..ba28735 --- /dev/null +++ b/src/routes/hello_bird.rs @@ -0,0 +1,4 @@ +#![allow(dead_code, clippy::unused_async)] +pub async fn hello_bird() -> &'static str { + "Hello, bird!" +} diff --git a/src/routes/hello_world.rs b/src/routes/hello_world.rs new file mode 100644 index 0000000..561c178 --- /dev/null +++ b/src/routes/hello_world.rs @@ -0,0 +1,4 @@ +#![allow(dead_code, clippy::unused_async)] +pub async fn hello_world() -> &'static str { + "Hello, world!" +} diff --git a/src/routes/minus_one.rs b/src/routes/minus_one.rs new file mode 100644 index 0000000..00f4f32 --- /dev/null +++ b/src/routes/minus_one.rs @@ -0,0 +1,15 @@ +#![allow(dead_code, clippy::unused_async)] + +use axum::{ + http::{header, StatusCode}, + response::{AppendHeaders, IntoResponse}, +}; +pub async fn minus_one() -> impl IntoResponse { + ( + StatusCode::FOUND, + AppendHeaders([( + header::LOCATION, + "https://www.youtube.com/watch?v=9Gc4QTqslN4", + )]), + ) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..86a21fc --- /dev/null +++ b/src/routes/mod.rs @@ -0,0 +1,7 @@ +mod hello_bird; +mod hello_world; +mod minus_one; + +pub use hello_bird::hello_bird; +pub use hello_world::hello_world; +pub use minus_one::minus_one;