+router +splash -middleware
This commit is contained in:
parent
a0fbcfa30f
commit
7915e0b216
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/target
|
||||
hako.log
|
1447
Cargo.lock
generated
1447
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -5,9 +5,12 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
fern = { version = "0.6.2" }
|
||||
http-body-util = "0.1.2"
|
||||
humantime = "2.1.0"
|
||||
hyper = { version = "1.4.1", features = ["full"] }
|
||||
hyper-util = { version = "0.1.6", features = ["full"] }
|
||||
log = "0.4.22"
|
||||
owo-colors = "4.0.0"
|
||||
pico-args = "0.5.0"
|
||||
salvo = "0.68.4"
|
||||
route-recognizer = "0.3.1"
|
||||
tokio = { version = "1.38.0", features = ["full"] }
|
||||
|
24
hako.log
24
hako.log
@ -41,3 +41,27 @@
|
||||
[2024-07-06T16:13:45Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-06T16:13:47Z INFO hako::middleware] [32mGET[39m -> [97mhttp://localhost:8080/[39m
|
||||
[2024-07-06T16:13:48Z INFO hako::middleware] [32mGET[39m -> [97mhttp://localhost:8080/[39m
|
||||
[2024-07-06T16:19:34Z INFO hako::middleware] [32mGET[39m -> [97mhttp://localhost:8080/[39m
|
||||
[2024-07-06T16:24:52Z INFO hako::middleware] [32mGET[39m -> [97mhttp://localhost:8080/[39m
|
||||
[2024-07-07T02:41:43Z INFO hako::middleware] [32mGET[39m -> [97mhttp://localhost:8080/[39m
|
||||
[2024-07-24T20:47:30Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T20:47:46Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:02:14Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:05:12Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:08:32Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:09:11Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:09:27Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:09:32Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:09:40Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:09:47Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:09:57Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:10:05Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:22:04Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:24:24Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:25:27Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:26:22Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:28:46Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:28:49Z INFO hako::middleware] [32mGET[39m -> [97m/[39m
|
||||
[2024-07-24T21:35:05Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:35:47Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
[2024-07-24T21:36:15Z INFO hako] Hako server listening on 127.0.0.1:8080
|
||||
|
@ -1,7 +1,5 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub mod args;
|
||||
pub mod logging;
|
||||
pub mod middleware;
|
||||
pub mod routes;
|
||||
pub mod router;
|
||||
|
40
src/main.rs
40
src/main.rs
@ -1,25 +1,41 @@
|
||||
use hako::routes;
|
||||
use salvo::prelude::*;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use hako::router::Router;
|
||||
use hyper::server::conn::http1;
|
||||
use hyper_util::rt::TokioIo;
|
||||
use owo_colors::OwoColorize;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Hako v0.0.1");
|
||||
hako::logging::setup_logging();
|
||||
|
||||
let args = hako::args::parse_args().expect("error parsing args");
|
||||
|
||||
let router = Router::new()
|
||||
.hoop(hako::middleware::logger)
|
||||
.push(Router::with_path("/").get(routes::hello));
|
||||
let listener = TcpListener::new(format!("{}:{}", args.host, args.port))
|
||||
.bind()
|
||||
.await;
|
||||
println!("{}", include_str!("./splash.txt").bright_purple());
|
||||
println!("{}", "Hako v0.0.1".purple());
|
||||
|
||||
let addr: SocketAddr = format!("{}:{}", args.host, args.port)
|
||||
.parse()
|
||||
.expect("invalid HTTP address");
|
||||
|
||||
let listener = TcpListener::bind(addr).await?;
|
||||
info!("Hako server listening on {}:{}", args.host, args.port);
|
||||
|
||||
Server::new(listener).serve(router).await;
|
||||
let router = Router {};
|
||||
|
||||
Ok(())
|
||||
loop {
|
||||
let (stream, _) = listener.accept().await?;
|
||||
|
||||
let io = TokioIo::new(stream);
|
||||
let router = router.clone();
|
||||
|
||||
tokio::task::spawn(async move {
|
||||
if let Err(err) = http1::Builder::new().serve_connection(io, router).await {
|
||||
eprintln!("Error serving connection: {:?}", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
use owo_colors::OwoColorize;
|
||||
use salvo::{handler, Request};
|
||||
|
||||
#[handler]
|
||||
pub async fn logger(req: &mut Request) {
|
||||
info!("{} -> {}", req.method().green(), req.uri().bright_white());
|
||||
}
|
37
src/router.rs
Normal file
37
src/router.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use std::{future::Future, pin::Pin};
|
||||
|
||||
use route_recognizer::Router as Matcher;
|
||||
|
||||
use http_body_util::Full;
|
||||
use hyper::{
|
||||
body::{Bytes, Incoming},
|
||||
service::Service,
|
||||
Request, Response,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Router {}
|
||||
|
||||
impl Service<Request<Incoming>> for Router {
|
||||
type Response = Response<Full<Bytes>>;
|
||||
type Error = hyper::Error;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
||||
|
||||
fn call(&self, req: Request<Incoming>) -> Self::Future {
|
||||
fn mk_response(s: String) -> Result<Response<Full<Bytes>>, hyper::Error> {
|
||||
Ok(Response::builder().body(Full::new(Bytes::from(s))).unwrap())
|
||||
}
|
||||
let mut matcher = Matcher::new();
|
||||
|
||||
matcher.add("/", "Welcome to hako.");
|
||||
matcher.add("/posts", "Posts");
|
||||
matcher.add("/credits", "Written by Nuru");
|
||||
|
||||
let res = match matcher.recognize(req.uri().path()) {
|
||||
Ok(route) => mk_response(route.handler().to_string()),
|
||||
Err(err) => mk_response(err),
|
||||
};
|
||||
|
||||
Box::pin(async { res })
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
use salvo::{handler, http::StatusCode, Response};
|
||||
|
||||
#[handler]
|
||||
pub async fn hello(res: &mut Response) {
|
||||
res.status_code(StatusCode::OK);
|
||||
res.render("Hello, world!");
|
||||
}
|
15
src/splash.txt
Normal file
15
src/splash.txt
Normal file
@ -0,0 +1,15 @@
|
||||
⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣶⣶⣿⣿⣿⣿⣿⣷⣶⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣶⣶⡿⠿⢿⣿⣶⣶⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀
|
||||
⠀⠀⠀⠀⠀⣠⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠞⠋⠉⠀⠀⠀⠀⠀⠀⠀⠉⠛⢿⣿⣷⣄⠀⠀⠀⠀⠀
|
||||
⠀⠀⠀⣠⣾⣿⣿⣿⣿⠿⠛⠉⠁⠀⠀⠀⠀⠉⠙⠻⢿⣿⣿⣿⣿⣄⠀⠀⠀⠀⠀⠀⠀⠀⣀⣴⣶⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣷⣄⠀⠀⠀
|
||||
⠀⠀⣼⣿⣿⣿⡿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣷⡀⠀⠀⠀⢀⣶⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣧⠀⠀
|
||||
⠀⣼⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣄⠀⠀⣿⣿⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣧⠀
|
||||
⢸⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣿⣿⣿⢂⣾⣿⣿⣿⠿⠛⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⡄
|
||||
⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡿⢡⣿⣿⣿⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⡇
|
||||
⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣱⣿⣿⣿⡿⡁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⡇
|
||||
⢿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⡟⣴⣿⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⡇
|
||||
⠸⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⠏⢸⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⠁
|
||||
⠀⢻⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣿⣿⣿⡿⠃⠀⠀⠹⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⣿⣿⣿⠃⠀
|
||||
⠀⠀⠹⣿⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⣿⠟⠁⠀⠀⠀⠀⠈⢻⣿⣿⣿⣷⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⡿⠃⠀⠀
|
||||
⠀⠀⠀⠈⠻⣿⣿⣿⣿⣶⣤⣀⣀⠀⠀⠀⣀⣀⣤⣶⣿⣿⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣶⣤⣀⣀⠀⠀⠀⢀⣀⣤⣶⣿⣿⣿⣿⠟⠁⠀⠀⠀
|
||||
⠀⠀⠀⠀⠀⠈⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⠁⠀⠀⠀⠀⠀
|
||||
⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠻⠿⠿⠿⠿⠿⠟⠛⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠻⠿⢿⣿⣿⣿⠿⠿⠟⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀
|
Loading…
Reference in New Issue
Block a user