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