Initial commit
This commit is contained in:
commit
3ac67f0462
9 changed files with 120 additions and 0 deletions
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…
Add table
Add a link
Reference in a new issue