minimal salvo server with limited logging

This commit is contained in:
nuru 2024-07-06 16:15:32 +00:00
parent 2e843c7310
commit a0fbcfa30f
Signed by: nuru
GPG Key ID: FA028981DC86855A
9 changed files with 1757 additions and 317 deletions

1960
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,10 @@ version = "0.1.0"
edition = "2021"
[dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
fern = { version = "0.6.2" }
humantime = "2.1.0"
log = "0.4.22"
owo-colors = "4.0.0"
pico-args = "0.5.0"
salvo = "0.68.4"
tokio = { version = "1.38.0", features = ["full"] }

43
hako.log Normal file
View File

@ -0,0 +1,43 @@
[2024-07-06T15:33:52Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:37:37Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:37:39Z INFO hako::middleware] request received
[2024-07-06T15:38:05Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:38:08Z INFO hako::middleware] request received
[2024-07-06T15:41:00Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:41:02Z INFO hako::middleware] socket://127.0.0.1:59086
[2024-07-06T15:41:04Z INFO hako::middleware] socket://127.0.0.1:60840
[2024-07-06T15:41:42Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:41:45Z INFO hako::middleware] 127.0.0.1:46826
[2024-07-06T15:41:50Z INFO hako::middleware] 127.0.0.1:46832
[2024-07-06T15:43:55Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:44:06Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:44:08Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T15:44:30Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:47:45Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:47:48Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T15:48:24Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:48:45Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T15:48:47Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T15:48:48Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T16:01:12Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:01:13Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T16:01:37Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:01:38Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T16:01:52Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T16:01:55Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:01:58Z INFO hako::middleware] Response { status_code: None, version: HTTP/1.1, headers: {}, body: ResBody::None }
[2024-07-06T16:01:58Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T16:11:05Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:11:06Z INFO hako::middleware] Response { status_code: None, version: HTTP/1.1, headers: {}, body: ResBody::None }
[2024-07-06T16:11:06Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T16:12:57Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:13:00Z INFO hako::middleware] Response { status_code: None, version: HTTP/1.1, headers: {}, body: ResBody::None }
[2024-07-06T16:13:00Z INFO hako::middleware] GET -> http://localhost:8080/ <- 200
[2024-07-06T16:13:22Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:13:23Z INFO hako::middleware] Response { status_code: None, version: HTTP/1.1, headers: {}, body: ResBody::None }
[2024-07-06T16:13:23Z INFO hako::middleware] GET -> http://localhost:8080/
[2024-07-06T16:13:33Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:13:41Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:13:45Z INFO hako] Hako server listening on 127.0.0.1:8080
[2024-07-06T16:13:47Z INFO hako::middleware] GET -> http://localhost:8080/
[2024-07-06T16:13:48Z INFO hako::middleware] GET -> http://localhost:8080/

View File

@ -1,7 +1,4 @@
use std::{
net::{Ipv4Addr, SocketAddr},
path::{Path, PathBuf},
};
use std::{net::Ipv4Addr, path::PathBuf};
const HELP: &str = "\
App

View File

@ -1 +1,7 @@
#[macro_use]
extern crate log;
pub mod args;
pub mod logging;
pub mod middleware;
pub mod routes;

17
src/logging.rs Normal file
View File

@ -0,0 +1,17 @@
pub fn setup_logging() {
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{} {} {}] {}",
humantime::format_rfc3339_seconds(std::time::SystemTime::now()),
record.level(),
record.target(),
message
))
})
.level(log::LevelFilter::Info)
.chain(std::io::stderr())
.chain(fern::log_file("hako.log").expect("failed to open log file at hako.log"))
.apply()
.expect("failed to setup logger");
}

View File

@ -1,12 +1,25 @@
#[async_std::main]
use hako::routes;
use salvo::prelude::*;
#[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 listener = TcpListener::bind(format!("{}:{}", args.host, args.port))
.await
.expect("failed to bind tcp listener");
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;
info!("Hako server listening on {}:{}", args.host, args.port);
Server::new(listener).serve(router).await;
Ok(())
}

7
src/middleware.rs Normal file
View File

@ -0,0 +1,7 @@
use owo_colors::OwoColorize;
use salvo::{handler, Request};
#[handler]
pub async fn logger(req: &mut Request) {
info!("{} -> {}", req.method().green(), req.uri().bright_white());
}

7
src/routes.rs Normal file
View File

@ -0,0 +1,7 @@
use salvo::{handler, http::StatusCode, Response};
#[handler]
pub async fn hello(res: &mut Response) {
res.status_code(StatusCode::OK);
res.render("Hello, world!");
}