1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00
actix-web/examples/diesel/src/main.rs

79 lines
2.2 KiB
Rust
Raw Normal View History

2017-12-19 01:25:26 +01:00
//! Actix web diesel example
//!
//! Diesel does not support tokio, so we have to run it in separate threads.
2018-03-11 18:10:30 +01:00
//! 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
2017-12-19 05:03:42 +01:00
//! can run in parallel and process messages from same queue.
2017-12-19 01:25:26 +01:00
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate diesel;
2018-04-01 17:20:15 +02:00
extern crate r2d2;
2017-12-19 01:25:26 +01:00
extern crate uuid;
extern crate futures;
extern crate actix;
extern crate actix_web;
extern crate env_logger;
2018-03-31 03:54:38 +02:00
use actix::prelude::*;
2018-04-02 03:24:07 +02:00
use actix_web::{http, server, middleware,
App, Path, State, HttpResponse, AsyncResponder, FutureResponse};
2017-12-31 08:12:26 +01:00
2017-12-19 01:25:26 +01:00
use diesel::prelude::*;
use diesel::r2d2::{ Pool, ConnectionManager };
2017-12-21 01:32:31 +01:00
use futures::future::Future;
2017-12-19 01:25:26 +01:00
2017-12-21 02:43:43 +01:00
mod db;
2017-12-19 01:25:26 +01:00
mod models;
mod schema;
2017-12-21 02:43:43 +01:00
use db::{CreateUser, DbExecutor};
2017-12-19 01:30:35 +01:00
/// State with DbExecutor address
2018-04-02 03:24:07 +02:00
struct AppState {
2018-02-13 04:15:39 +01:00
db: Addr<Syn, DbExecutor>,
2017-12-19 01:25:26 +01:00
}
2017-12-19 01:30:35 +01:00
/// Async request handler
2018-04-02 03:24:07 +02:00
fn index(name: Path<String>, state: State<AppState>) -> FutureResponse<HttpResponse> {
2018-03-11 18:10:30 +01:00
// send async `CreateUser` message to a `DbExecutor`
state.db.send(CreateUser{name: name.into_inner()})
2017-12-21 06:06:04 +01:00
.from_err()
.and_then(|res| {
match res {
2018-03-31 08:37:15 +02:00
Ok(user) => Ok(HttpResponse::Ok().json(user)),
2018-03-31 03:54:38 +02:00
Err(_) => Ok(HttpResponse::InternalServerError().into())
2017-12-21 06:06:04 +01:00
}
})
.responder()
2017-12-19 01:25:26 +01:00
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
2018-04-01 17:20:15 +02:00
env_logger::init();
2017-12-19 01:25:26 +01:00
let sys = actix::System::new("diesel-example");
2018-03-11 18:10:30 +01:00
// Start 3 db executor actors
2018-04-01 17:20:15 +02:00
let manager = ConnectionManager::<SqliteConnection>::new("test.db");
let pool = r2d2::Pool::builder().build(manager).expect("Failed to create pool.");
let addr = SyncArbiter::start(3, move || {
DbExecutor(pool.clone())
2017-12-19 01:25:26 +01:00
});
// Start http server
2018-04-02 03:24:07 +02:00
server::new(move || {
App::with_state(AppState{db: addr.clone()})
2017-12-19 01:25:26 +01:00
// enable logger
2017-12-27 04:59:41 +01:00
.middleware(middleware::Logger::default())
.resource("/{name}", |r| r.method(http::Method::GET).with2(index))})
2017-12-19 01:25:26 +01:00
.bind("127.0.0.1:8080").unwrap()
2017-12-19 18:08:36 +01:00
.start();
2017-12-19 01:25:26 +01:00
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
}