mirror of
https://github.com/actix/examples
synced 2025-02-17 07:23:29 +01:00
git push fix basics
This commit is contained in:
parent
7648c0865e
commit
15663241e0
@ -16,11 +16,11 @@ use futures::future::{FutureResult, result};
|
|||||||
|
|
||||||
/// favicon handler
|
/// favicon handler
|
||||||
fn favicon(req: HttpRequest) -> Result<fs::NamedFile> {
|
fn favicon(req: HttpRequest) -> Result<fs::NamedFile> {
|
||||||
Ok(fs::NamedFile::open("../static/favicon.ico")?)
|
Ok(fs::NamedFile::open("static/favicon.ico")?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// simple index handler
|
/// simple index handler
|
||||||
fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
fn welcome(mut req: HttpRequest) -> Result<HttpResponse> {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
|
|
||||||
// example of ...
|
// example of ...
|
||||||
@ -50,7 +50,7 @@ fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
|||||||
|
|
||||||
/// 404 handler
|
/// 404 handler
|
||||||
fn p404(req: HttpRequest) -> Result<fs::NamedFile> {
|
fn p404(req: HttpRequest) -> Result<fs::NamedFile> {
|
||||||
Ok(fs::NamedFile::open("./static/404.html")?
|
Ok(fs::NamedFile::open("static/404.html")?
|
||||||
.set_status_code(StatusCode::NOT_FOUND))
|
.set_status_code(StatusCode::NOT_FOUND))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,31 +90,33 @@ fn main() {
|
|||||||
middleware::CookieSessionBackend::signed(&[0; 32]).secure(false)
|
middleware::CookieSessionBackend::signed(&[0; 32]).secure(false)
|
||||||
))
|
))
|
||||||
// register favicon
|
// register favicon
|
||||||
.resource("/favicon.ico", |r| r.f(favicon))
|
.resource("/favicon", |r| r.f(favicon))
|
||||||
// register simple route, handle all methods
|
// register simple route, handle all methods
|
||||||
.resource("/index.html", |r| r.f(index))
|
.resource("/welcome", |r| r.f(welcome))
|
||||||
// with path parameters
|
// with path parameters
|
||||||
.resource("/user/{name}/", |r| r.method(Method::GET).f(with_param))
|
.resource("/user/{name}", |r| r.method(Method::GET).f(with_param))
|
||||||
// async handler
|
// async handler
|
||||||
.resource("/async/{name}", |r| r.method(Method::GET).a(index_async))
|
.resource("/async/{name}", |r| r.method(Method::GET).a(index_async))
|
||||||
.resource("/test", |r| r.f(|req| {
|
.resource("/test", |r| r.f(|req| {
|
||||||
|
println!("=============test=============");
|
||||||
match *req.method() {
|
match *req.method() {
|
||||||
Method::GET => HttpResponse::Ok(),
|
Method::GET => HttpResponse::Ok(),
|
||||||
Method::POST => HttpResponse::MethodNotAllowed(),
|
Method::POST => HttpResponse::MethodNotAllowed(),
|
||||||
_ => HttpResponse::NotFound(),
|
_ => HttpResponse::NotFound(),
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.resource("/error.html", |r| r.f(|req| {
|
.resource("/error", |r| r.f(|req| {
|
||||||
|
println!("=============error=============");
|
||||||
error::InternalError::new(
|
error::InternalError::new(
|
||||||
io::Error::new(io::ErrorKind::Other, "test"), StatusCode::OK)
|
io::Error::new(io::ErrorKind::Other, "test"), StatusCode::OK)
|
||||||
}))
|
}))
|
||||||
// static files
|
// static files
|
||||||
.handler("/static/", fs::StaticFiles::new("static/"))
|
.handler("/static", fs::StaticFiles::new("static"))
|
||||||
// redirect
|
// redirect
|
||||||
.resource("/", |r| r.method(Method::GET).f(|req| {
|
.resource("/", |r| r.method(Method::GET).f(|req| {
|
||||||
println!("{:?}", req);
|
println!("{:?}", req);
|
||||||
HttpResponse::Found()
|
HttpResponse::Found()
|
||||||
.header(header::LOCATION, "/index.html")
|
.header(header::LOCATION, "static/index.html")
|
||||||
.finish()
|
.finish()
|
||||||
}))
|
}))
|
||||||
// default
|
// default
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<!DOCTYPE html><html><head><title>actix - basics</title>
|
<!DOCTYPE html><html><head><title>actix - basics</title>
|
||||||
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" /></head>
|
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" /></head>
|
||||||
<body>
|
<body>
|
||||||
<a href="index.html">back to home</a>
|
<a href="static/index.html">back to home</a>
|
||||||
<h1>404</h1>
|
<h1>404</h1>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user