1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

upgrade actix-web to 0.6

This commit is contained in:
Nikolay Kim
2018-05-08 11:08:43 -07:00
parent 27de52b5d5
commit bbeb262a5c
55 changed files with 689 additions and 518 deletions

View File

@ -1,17 +1,16 @@
//! Db executor actor
use std::io;
use uuid;
use actix_web::*;
use actix::prelude::*;
use actix_web::*;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use std::io;
use uuid;
/// This is db executor actor. We are going to run 3 of them in parallel.
pub struct DbExecutor(pub Pool<SqliteConnectionManager>);
/// This is only message that this actor can handle, but it is easy to extend number of
/// messages.
/// This is only message that this actor can handle, but it is easy to extend
/// number of messages.
pub struct CreateUser {
pub name: String,
}
@ -31,11 +30,15 @@ impl Handler<CreateUser> for DbExecutor {
let conn = self.0.get().unwrap();
let uuid = format!("{}", uuid::Uuid::new_v4());
conn.execute("INSERT INTO users (id, name) VALUES ($1, $2)",
&[&uuid, &msg.name]).unwrap();
conn.execute(
"INSERT INTO users (id, name) VALUES ($1, $2)",
&[&uuid, &msg.name],
).unwrap();
Ok(conn.query_row("SELECT name FROM users WHERE id=$1", &[&uuid], |row| {
row.get(0)
}).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?)
Ok(conn.query_row(
"SELECT name FROM users WHERE id=$1",
&[&uuid],
|row| row.get(0),
).map_err(|_| io::Error::new(io::ErrorKind::Other, "db error"))?)
}
}

View File

@ -1,41 +1,42 @@
//! Actix web r2d2 example
extern crate serde;
extern crate serde_json;
extern crate uuid;
extern crate futures;
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;
extern crate r2d2;
extern crate r2d2_sqlite;
extern crate rusqlite;
extern crate serde;
extern crate serde_json;
extern crate uuid;
use actix::prelude::*;
use actix_web::{
middleware, http, server, App, AsyncResponder, HttpRequest, HttpResponse, Error};
use actix_web::{http, middleware, server, App, AsyncResponder, Error, HttpRequest,
HttpResponse};
use futures::future::Future;
use r2d2_sqlite::SqliteConnectionManager;
mod db;
use db::{CreateUser, DbExecutor};
/// State with DbExecutor address
struct State {
db: Addr<Syn, DbExecutor>,
}
/// Async request handler
fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
fn index(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
let name = &req.match_info()["name"];
req.state().db.send(CreateUser{name: name.to_owned()})
req.state()
.db
.send(CreateUser {
name: name.to_owned(),
})
.from_err()
.and_then(|res| {
match res {
Ok(user) => Ok(HttpResponse::Ok().json(user)),
Err(_) => Ok(HttpResponse::InternalServerError().into())
}
.and_then(|res| match res {
Ok(user) => Ok(HttpResponse::Ok().json(user)),
Err(_) => Ok(HttpResponse::InternalServerError().into()),
})
.responder()
}
@ -57,8 +58,9 @@ fn main() {
App::with_state(State{db: addr.clone()})
// enable logger
.middleware(middleware::Logger::default())
.resource("/{name}", |r| r.method(http::Method::GET).a(index))})
.bind("127.0.0.1:8080").unwrap()
.resource("/{name}", |r| r.method(http::Method::GET).a(index))
}).bind("127.0.0.1:8080")
.unwrap()
.start();
let _ = sys.run();