challenge: -1

This commit is contained in:
itsscb 2024-12-02 21:32:27 +01:00
parent 5dde7a99f2
commit ab9040c5d9
10 changed files with 255 additions and 1 deletions

4
.gitignore vendored

@ -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/
#.idea//target
.shuttle*
Secrets*.toml

13
Cargo.toml Normal file

@ -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"

96
flake.lock generated Normal file

@ -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
}

51
flake.nix Normal file

@ -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 ]);
};
}
);
}

52
src/lib.rs Normal file

@ -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);
}
}

10
src/main.rs Normal file

@ -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())
}

4
src/routes/hello_bird.rs Normal file

@ -0,0 +1,4 @@
#![allow(dead_code, clippy::unused_async)]
pub async fn hello_bird() -> &'static str {
"Hello, bird!"
}

@ -0,0 +1,4 @@
#![allow(dead_code, clippy::unused_async)]
pub async fn hello_world() -> &'static str {
"Hello, world!"
}

15
src/routes/minus_one.rs Normal file

@ -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",
)]),
)
}

7
src/routes/mod.rs Normal file

@ -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;