-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
36 lines (28 loc) · 1.09 KB
/
main.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
mod server;
mod router;
use http::{httprequest::HttpRequest, httpresponse::HttpResponse};
use server::Server;
fn hello(req: &HttpRequest) -> HttpResponse {
let body = b"Hello World!".to_vec();
HttpResponse::new("200", None, Some(body))
}
fn greeting(req: &HttpRequest) -> HttpResponse {
let username = req.path_params.get("name").unwrap();
let body = format!("Hello {}!", username);
HttpResponse::new("200", None, Some(body.into_bytes()))
}
fn user_order_details(req: &HttpRequest) -> HttpResponse {
let user_id = req.path_params.get("user_id").unwrap();
let order_id = req.path_params.get("order_id").unwrap();
let body = format!("UserId: {}, OrderId: {}", user_id, order_id);
HttpResponse::new("200", None, Some(body.into_bytes()))
}
fn main() {
let bind_address = "127.0.0.1:8000";
let server = Server::new(&bind_address);
server.get("/hello", hello);
server.get("/hello/{name}", greeting);
server.get("/users/{user_id}/orders/{order_id}", user_order_details);
println!("Server is listening {}", bind_address);
server.run();
}