mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
cleanup examples
This commit is contained in:
parent
4dd3382ac7
commit
3c5fd18e02
@ -90,7 +90,7 @@ fn main() {
|
||||
httpcodes::HTTPFound
|
||||
.build()
|
||||
.header("LOCATION", "/index.html")
|
||||
.body(Body::Empty)
|
||||
.finish()
|
||||
})))
|
||||
.bind("127.0.0.1:8080").unwrap()
|
||||
.start();
|
||||
|
@ -19,7 +19,7 @@ extern crate env_logger;
|
||||
use actix_web::*;
|
||||
use actix::prelude::*;
|
||||
use diesel::prelude::*;
|
||||
use futures::future::{Future, ok};
|
||||
use futures::future::Future;
|
||||
|
||||
mod models;
|
||||
mod schema;
|
||||
@ -35,13 +35,13 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>>
|
||||
|
||||
Box::new(
|
||||
req.state().db.call_fut(CreateUser{name: name.to_owned()})
|
||||
.from_err()
|
||||
.and_then(|res| {
|
||||
match res {
|
||||
Ok(user) => ok(httpcodes::HTTPOk.build().json(user).unwrap()),
|
||||
Err(_) => ok(httpcodes::HTTPInternalServerError.response())
|
||||
Ok(user) => Ok(httpcodes::HTTPOk.build().json(user)?),
|
||||
Err(_) => Ok(httpcodes::HTTPInternalServerError.response())
|
||||
}
|
||||
})
|
||||
.map_err(|e| error::ErrorInternalServerError(e).into()))
|
||||
}))
|
||||
}
|
||||
|
||||
/// This is db executor actor. We are going to run 3 of them in parallele.
|
||||
|
@ -16,7 +16,7 @@ fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>>
|
||||
|
||||
Box::new(
|
||||
req.multipart() // <- get multipart stream for current request
|
||||
.map_err(Error::from) // <- convert multipart errors
|
||||
.from_err() // <- convert multipart errors
|
||||
.and_then(|item| { // <- iterate over multipart items
|
||||
match item {
|
||||
// Handle multipart Field
|
||||
|
@ -9,19 +9,20 @@ struct State {
|
||||
template: tera::Tera, // <- store tera template in application state
|
||||
}
|
||||
|
||||
fn index(req: HttpRequest<State>) -> HttpResponse {
|
||||
fn index(req: HttpRequest<State>) -> Result<HttpResponse> {
|
||||
let s = if let Some(name) = req.query().get("name") { // <- submitted form
|
||||
let mut ctx = tera::Context::new();
|
||||
ctx.add("name", name);
|
||||
ctx.add("text", &"Welcome!".to_owned());
|
||||
req.state().template.render("user.html", &ctx).unwrap()
|
||||
req.state().template.render("user.html", &ctx)
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
||||
} else {
|
||||
req.state().template.render("index.html", &tera::Context::new()).unwrap()
|
||||
req.state().template.render("index.html", &tera::Context::new())
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?
|
||||
};
|
||||
httpcodes::HTTPOk.build()
|
||||
.content_type("text/html")
|
||||
.body(s)
|
||||
.unwrap()
|
||||
Ok(httpcodes::HTTPOk.build()
|
||||
.content_type("text/html")
|
||||
.body(s)?)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -200,12 +200,12 @@ fn main() {
|
||||
let state = WsChatSessionState { addr: server.clone() };
|
||||
|
||||
Application::with_state(state)
|
||||
// redirect to websocket.html
|
||||
.resource("/", |r| r.method(Method::GET).f(|req| {
|
||||
// redirect to websocket.html
|
||||
.resource("/", |r| r.method(Method::GET).f(|_| {
|
||||
httpcodes::HTTPFound
|
||||
.build()
|
||||
.header("LOCATION", "/static/websocket.html")
|
||||
.body(Body::Empty)
|
||||
.finish()
|
||||
}))
|
||||
// websocket
|
||||
.resource("/ws/", |r| r.route().f(chat_route))
|
||||
|
Loading…
Reference in New Issue
Block a user