Initial commit
This commit is contained in:
commit
3ac67f0462
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
target/
|
46
Cargo.lock
generated
Normal file
46
Cargo.lock
generated
Normal file
@ -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",
|
||||
]
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "rust-framework"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
tiny_http = "0.12.0"
|
1
src/entities/mod.rs
Normal file
1
src/entities/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod request;
|
13
src/entities/request.rs
Normal file
13
src/entities/request.rs
Normal file
@ -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()
|
||||
}
|
||||
}
|
||||
|
18
src/lib.rs
Normal file
18
src/lib.rs
Normal file
@ -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);
|
||||
}
|
||||
}
|
26
src/router.rs
Normal file
26
src/router.rs
Normal file
@ -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<String, fn(Request)>,
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
1
src/utilities/mod.rs
Normal file
1
src/utilities/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod responses;
|
7
src/utilities/responses.rs
Normal file
7
src/utilities/responses.rs
Normal file
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user