-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathprofiling.rs
48 lines (38 loc) · 1.18 KB
/
profiling.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use log::info;
use std::time::Instant;
use thruster::{m, middleware_fn};
use thruster::{App, BasicContext as Ctx, Request, Server, ThrusterServer};
use thruster::{MiddlewareNext, MiddlewareResult};
#[middleware_fn]
async fn profiling(mut context: Ctx, next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
let start_time = Instant::now();
context = next(context).await?;
let elapsed_time = start_time.elapsed();
info!(
"[{}μs] {} -- {}",
elapsed_time.as_micros(),
context.request.method(),
context.request.path()
);
Ok(context)
}
#[middleware_fn]
async fn plaintext(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
let val = "Hello, World!";
context.body(val);
Ok(context)
}
#[middleware_fn]
async fn test_fn_404(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
context.body("404");
Ok(context)
}
fn main() {
env_logger::init();
info!("Starting server...");
let app = App::<Request, Ctx, ()>::new_basic()
.get("/plaintext", m![profiling, plaintext])
.set404(m![test_fn_404]);
let server = Server::new(app);
server.start("0.0.0.0", 4321);
}