From 677eb8a01a8f0cdc047872dadb9c1c7233328b88 Mon Sep 17 00:00:00 2001 From: Darius Auding <Darius.auding@gmx.de> Date: Sun, 28 May 2023 18:04:45 +0200 Subject: [PATCH] Add Ranking --- core/http/src/handling/methods.rs | 2 +- core/http/src/handling/request.rs | 12 ++++++------ core/http/src/setup.rs | 9 ++++----- site/src/main.rs | 22 ++++++++++++++++++++-- site/static/hi | 1 + 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 site/static/hi diff --git a/core/http/src/handling/methods.rs b/core/http/src/handling/methods.rs index 998cfa5..5ca6211 100644 --- a/core/http/src/handling/methods.rs +++ b/core/http/src/handling/methods.rs @@ -1,6 +1,6 @@ use std::{fmt::Display, str::FromStr}; -#[derive(PartialEq, Eq, Clone, Debug, Copy)] +#[derive(PartialEq, Eq, Clone, Debug, Copy, PartialOrd, Ord)] pub enum Method { Get, Head, diff --git a/core/http/src/handling/request.rs b/core/http/src/handling/request.rs index ba1cf37..adfb6b8 100644 --- a/core/http/src/handling/request.rs +++ b/core/http/src/handling/request.rs @@ -1,4 +1,4 @@ -use std::net::SocketAddr; +// use std::net::SocketAddr; use super::{methods::Method, routes::Uri}; @@ -11,12 +11,12 @@ pub struct Request<'a> { // pub connection: ConnectionMeta, } -struct ConnectionMeta { - remote: Option<SocketAddr>, - // certificates -} +// struct ConnectionMeta { +// remote: Option<SocketAddr>, +// // certificates +// } -#[derive(Clone, Debug, Copy)] +#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum MediaType { Json, Plain, diff --git a/core/http/src/setup.rs b/core/http/src/setup.rs index dfdb496..a50e7d1 100644 --- a/core/http/src/setup.rs +++ b/core/http/src/setup.rs @@ -1,8 +1,6 @@ -use std::{ - thread::available_parallelism, process::exit, -}; +use std::thread::available_parallelism; -use tokio::{net::TcpListener, signal::unix::{SignalKind, signal}, runtime, select}; +use tokio::{net::TcpListener, signal::unix::{SignalKind, signal}, select}; use crate::{ handlers::handlers::handle_connection, @@ -21,7 +19,8 @@ pub struct Config { } impl<'a> Config { - pub fn mount(mut self, mountpoint: Uri<'static>, routes: Vec<Route<'static>>) -> Config { + pub fn mount(mut self, mountpoint: Uri<'static>, mut routes: Vec<Route<'static>>) -> Config { + routes.sort_by(|a, b| a.rank.cmp(&b.rank)); let mut mount_message = format!(" >> \x1b[35m{}\x1b[0m\n", mountpoint); for (index, route) in routes.iter().enumerate() { let indent_sign; diff --git a/site/src/main.rs b/site/src/main.rs index fc87c26..e58195a 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -8,6 +8,15 @@ use http::handling::{ routes::{Data, Route}, }; +fn handle_static_hi(request: Request<'_>, data: Data) -> Outcome<Response, Status, Data> { + // Outcome::Success(Response { headers: vec![ + // format!("Content-Length: 4"), + // format!("Content-Type: text/plain") + // ], status: Some(Status::Ok), body: Box::new("jkl;") }) + Outcome::Forward(data) +} + + fn handler(request: Request<'_>, _data: Data) -> Outcome<Response, Status, Data> { let response = fileserver(request.uri.strip_prefix("static/").unwrap()); let response = match response { @@ -60,7 +69,7 @@ async fn main() { name: Some("file_server"), uri: "static/<path..>", method: Method::Get, - rank: 0, + rank: 1, }; let post_test = Route { @@ -72,7 +81,16 @@ async fn main() { rank: 0, }; + let static_hi = Route { + format: None, + handler: handle_static_hi, + name: Some("Handle_Static_hi"), + uri: "static/hi", + method: Method::Get, + rank: 0 + }; + http::build("127.0.0.1:8000").await - .mount("/", vec![fileserver, post_test]) + .mount("/", vec![fileserver, post_test, static_hi]) .launch().await; } diff --git a/site/static/hi b/site/static/hi new file mode 100644 index 0000000..8bd6648 --- /dev/null +++ b/site/static/hi @@ -0,0 +1 @@ +asdf -- GitLab