-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.rs
140 lines (125 loc) · 4.97 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::{env, io};
use ntex::http::{header, Method, StatusCode};
use ntex::web::{self, error, guard, middleware, App, Error, HttpRequest, HttpResponse};
use ntex::{channel::mpsc, util::Bytes};
use ntex_files as fs;
use ntex_session::{CookieSession, Session};
/// favicon handler
#[web::get("/favicon")]
async fn favicon() -> Result<fs::NamedFile, Error> {
Ok(fs::NamedFile::open("static/favicon.ico")?)
}
/// simple index handler
#[web::get("/welcome")]
async fn welcome(session: Session, req: HttpRequest) -> Result<HttpResponse, Error> {
println!("{:?}", req);
// session
let mut counter = 1;
if let Some(count) = session.get::<i32>("counter")? {
println!("SESSION value: {}", count);
counter = count + 1;
}
// set counter to session
session.set("counter", counter)?;
// response
Ok(HttpResponse::build(StatusCode::OK)
.content_type("text/html; charset=utf-8")
.body(include_str!("../static/welcome.html")))
}
/// 404 handler
async fn p404() -> Result<fs::NamedFile, Error> {
Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
}
/// response body
async fn response_body(path: web::types::Path<String>) -> HttpResponse {
let text = format!("Hello {}!", *path);
let (tx, rx_body) = mpsc::channel();
let _ = tx.send(Ok::<_, Error>(Bytes::from(text)));
HttpResponse::Ok().streaming(rx_body)
}
/// handler with path parameters like `/user/{name}/`
async fn with_param(
req: HttpRequest,
path: web::types::Path<(String,)>,
) -> HttpResponse {
println!("{:?}", req);
HttpResponse::Ok()
.content_type("text/plain")
.body(format!("Hello {}!", path.0))
}
/// Handler to match all paths starting with /files
#[web::get("/files/{all}*")]
async fn match_all_paths(path: web::types::Path<String>) -> HttpResponse {
println!("path: {:?}", path);
HttpResponse::Ok()
.content_type("text/plain")
.body("it's matching !")
}
#[tokio::main]
async fn main() -> io::Result<()> {
env::set_var("RUST_LOG", "ntex=info");
env_logger::init();
ntex::rt::System::new("main")
.run_local(async {
web::server(|| {
App::new()
// cookie session middleware
.wrap(CookieSession::signed(&[0; 32]).secure(false))
// enable logger
.wrap(middleware::Logger::default())
.service((
// register favicon
favicon,
// register simple route, handle all methods
welcome,
// register match_all_paths method
match_all_paths,
// with path parameters
web::resource("/user/{name}").route(web::get().to(with_param)),
// async response body
web::resource("/async-body/{name}")
.route(web::get().to(response_body)),
web::resource("/test").to(|req: HttpRequest| async move {
match *req.method() {
Method::GET => HttpResponse::Ok(),
Method::POST => HttpResponse::MethodNotAllowed(),
_ => HttpResponse::NotFound(),
}
}),
web::resource("/error").to(|| async {
error::InternalError::new(
io::Error::new(io::ErrorKind::Other, "test"),
StatusCode::INTERNAL_SERVER_ERROR,
)
}),
// static files
fs::Files::new("/static", "static").show_files_listing(),
// redirect
web::resource("/").route(web::get().to(
|req: HttpRequest| async move {
println!("{:?}", req);
HttpResponse::Found()
.header(header::LOCATION, "static/welcome.html")
.finish()
},
)),
))
// default
.default_service(
// 404 for GET request
web::resource("")
.route(web::get().to(p404))
// all requests that are not `GET`
.route(
web::route()
.guard(guard::Not(guard::Get()))
.to(|| async { HttpResponse::MethodNotAllowed() }),
),
)
})
.bind("127.0.0.1:8080")?
.run()
.await
})
.await
}