From 3ac67f0462dd38e1a77ba9a7f8d5549c7618650d Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Tue, 20 Aug 2024 08:05:01 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 46 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++++++ src/entities/mod.rs | 1 + src/entities/request.rs | 13 +++++++++++ src/lib.rs | 18 +++++++++++++++ src/router.rs | 26 +++++++++++++++++++++ src/utilities/mod.rs | 1 + src/utilities/responses.rs | 7 ++++++ 9 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/entities/mod.rs create mode 100644 src/entities/request.rs create mode 100644 src/lib.rs create mode 100644 src/router.rs create mode 100644 src/utilities/mod.rs create mode 100644 src/utilities/responses.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..01814dc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "rust-framework" +version = "0.0.1" +dependencies = [ + "tiny_http", +] + +[[package]] +name = "tiny_http" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82" +dependencies = [ + "ascii", + "chunked_transfer", + "httpdate", + "log", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c68b35d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "rust-framework" +version = "0.0.1" +edition = "2021" + +[dependencies] +tiny_http = "0.12.0" diff --git a/src/entities/mod.rs b/src/entities/mod.rs new file mode 100644 index 0000000..be9378d --- /dev/null +++ b/src/entities/mod.rs @@ -0,0 +1 @@ +pub mod request; diff --git a/src/entities/request.rs b/src/entities/request.rs new file mode 100644 index 0000000..3713c95 --- /dev/null +++ b/src/entities/request.rs @@ -0,0 +1,13 @@ +use tiny_http::Request; + + +pub trait Url { + fn get_url_without_parameters(&self) -> String; +} + +impl Url for Request { + fn get_url_without_parameters(&self) -> String { + self.url().split("?").next().unwrap().to_string() + } +} + diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c1d2959 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,18 @@ +pub mod router; +pub mod entities; +pub mod utilities; + +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/src/router.rs b/src/router.rs new file mode 100644 index 0000000..941c9dd --- /dev/null +++ b/src/router.rs @@ -0,0 +1,26 @@ +use std::collections::HashMap; + +use tiny_http::Request; + +use crate::{entities::request::Url, utilities::responses::respond_not_found}; + + +pub struct Router { + routes: HashMap, +} + +impl Router { + pub fn new() -> Self { + let routes = HashMap::new(); + + Router { routes } + } + + pub async fn route(&self, request: Request) { + match self.routes.get(&request.get_url_without_parameters()) { + Some(handler) => handler(request), + None => respond_not_found(request), + } + } +} + diff --git a/src/utilities/mod.rs b/src/utilities/mod.rs new file mode 100644 index 0000000..b396b03 --- /dev/null +++ b/src/utilities/mod.rs @@ -0,0 +1 @@ +pub mod responses; diff --git a/src/utilities/responses.rs b/src/utilities/responses.rs new file mode 100644 index 0000000..b828ee7 --- /dev/null +++ b/src/utilities/responses.rs @@ -0,0 +1,7 @@ +use tiny_http::{Request, Response}; + +pub fn respond_not_found(request: Request) { + let response = Response::from_string("Not Found").with_status_code(404); + + let _ = request.respond(response); +}