1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +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

@ -7,7 +7,7 @@ workspace = "../"
[dependencies]
env_logger = "0.5"
actix = "0.5"
actix-web = "^0.5"
actix-web = "^0.6"
futures = "0.1"
uuid = { version = "0.5", features = ["serde", "v4"] }

View File

@ -1,10 +1,10 @@
//! Db executor actor
use uuid;
use diesel;
use actix_web::*;
use actix::prelude::*;
use actix_web::*;
use diesel;
use diesel::prelude::*;
use diesel::r2d2::{Pool, ConnectionManager};
use diesel::r2d2::{ConnectionManager, Pool};
use uuid;
use models;
use schema;
@ -12,8 +12,8 @@ use schema;
/// This is db executor actor. We are going to run 3 of them in parallel.
pub struct DbExecutor(pub Pool<ConnectionManager<SqliteConnection>>);
/// 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,
}

View File

@ -1,28 +1,28 @@
//! Actix web diesel example
//!
//! Diesel does not support tokio, so we have to run it in separate threads.
//! Actix supports sync actors by default, so we going to create sync actor that use diesel.
//! Technically sync actors are worker style actors, multiple of them
//! can run in parallel and process messages from same queue.
//! Actix supports sync actors by default, so we going to create sync actor
//! that use diesel. Technically sync actors are worker style actors, multiple
//! of them can run in parallel and process messages from same queue.
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate diesel;
extern crate r2d2;
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 uuid;
use actix::prelude::*;
use actix_web::{http, server, middleware,
App, Path, State, HttpResponse, AsyncResponder, FutureResponse};
use actix_web::{http, middleware, server, App, AsyncResponder, FutureResponse,
HttpResponse, Path, State};
use diesel::prelude::*;
use diesel::r2d2::{ Pool, ConnectionManager };
use diesel::r2d2::{ConnectionManager, Pool};
use futures::future::Future;
mod db;
@ -31,7 +31,6 @@ mod schema;
use db::{CreateUser, DbExecutor};
/// State with DbExecutor address
struct AppState {
db: Addr<Syn, DbExecutor>,
@ -40,13 +39,15 @@ struct AppState {
/// Async request handler
fn index(name: Path<String>, state: State<AppState>) -> FutureResponse<HttpResponse> {
// send async `CreateUser` message to a `DbExecutor`
state.db.send(CreateUser{name: name.into_inner()})
state
.db
.send(CreateUser {
name: name.into_inner(),
})
.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()
}
@ -58,19 +59,20 @@ fn main() {
// Start 3 db executor actors
let manager = ConnectionManager::<SqliteConnection>::new("test.db");
let pool = r2d2::Pool::builder().build(manager).expect("Failed to create pool.");
let pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
let addr = SyncArbiter::start(3, move || {
DbExecutor(pool.clone())
});
let addr = SyncArbiter::start(3, move || DbExecutor(pool.clone()));
// Start http server
server::new(move || {
App::with_state(AppState{db: addr.clone()})
// enable logger
.middleware(middleware::Logger::default())
.resource("/{name}", |r| r.method(http::Method::GET).with2(index))})
.bind("127.0.0.1:8080").unwrap()
.resource("/{name}", |r| r.method(http::Method::GET).with2(index))
}).bind("127.0.0.1:8080")
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8080");