mirror of
https://github.com/actix/actix-website
synced 2025-02-25 13:32:49 +01:00
Fix DB article code examples (#140)
This change fixes issue #139 and renders the code example
This commit is contained in:
parent
58341eca3a
commit
f30016e112
@ -1,98 +1,98 @@
|
|||||||
// <actor>
|
// <actor>
|
||||||
// use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
|
|
||||||
// struct DbExecutor(SqliteConnection);
|
struct DbExecutor(SqliteConnection);
|
||||||
|
|
||||||
// impl Actor for DbExecutor {
|
impl Actor for DbExecutor {
|
||||||
// type Context = SyncContext<Self>;
|
type Context = SyncContext<Self>;
|
||||||
// }
|
}
|
||||||
// </actor>
|
// </actor>
|
||||||
|
|
||||||
// <message>
|
// <message>
|
||||||
// struct CreateUser {
|
struct CreateUser {
|
||||||
// name: String,
|
name: String,
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl Message for CreateUser {
|
impl Message for CreateUser {
|
||||||
// type Result = Result<User, Error>;
|
type Result = Result<User, Error>;
|
||||||
// }
|
}
|
||||||
// </message>
|
// </message>
|
||||||
|
|
||||||
// <handler>
|
// <handler>
|
||||||
// impl Handler<CreateUser> for DbExecutor {
|
impl Handler<CreateUser> for DbExecutor {
|
||||||
// type Result = Result<User, Error>;
|
type Result = Result<User, Error>;
|
||||||
|
|
||||||
// fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result {
|
fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result {
|
||||||
// use self::schema::users::dsl::*;
|
use self::schema::users::dsl::*;
|
||||||
|
|
||||||
// // Create insertion model
|
// Create insertion model
|
||||||
// let uuid = format!("{}", uuid::Uuid::new_v4());
|
let uuid = format!("{}", uuid::Uuid::new_v4());
|
||||||
// let new_user = models::NewUser {
|
let new_user = models::NewUser {
|
||||||
// id: &uuid,
|
id: &uuid,
|
||||||
// name: &msg.name,
|
name: &msg.name,
|
||||||
// };
|
};
|
||||||
|
|
||||||
// // normal diesel operations
|
// normal diesel operations
|
||||||
// diesel::insert_into(users)
|
diesel::insert_into(users)
|
||||||
// .values(&new_user)
|
.values(&new_user)
|
||||||
// .execute(&self.0)
|
.execute(&self.0)
|
||||||
// .expect("Error inserting person");
|
.expect("Error inserting person");
|
||||||
|
|
||||||
// let mut items = users
|
let mut items = users
|
||||||
// .filter(id.eq(&uuid))
|
.filter(id.eq(&uuid))
|
||||||
// .load::<models::User>(&self.0)
|
.load::<models::User>(&self.0)
|
||||||
// .expect("Error loading person");
|
.expect("Error loading person");
|
||||||
|
|
||||||
// Ok(items.pop().unwrap())
|
Ok(items.pop().unwrap())
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// </handler>
|
// </handler>
|
||||||
|
|
||||||
// <main>
|
// <main>
|
||||||
// /// This is state where we will store *DbExecutor* address.
|
/// This is state where we will store *DbExecutor* address.
|
||||||
// struct State {
|
struct State {
|
||||||
// db: Addr<DbExecutor>,
|
db: Addr<DbExecutor>,
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fn main() {
|
fn main() {
|
||||||
// let sys = actix::System::new("diesel-example");
|
let sys = actix::System::new("diesel-example");
|
||||||
|
|
||||||
// // Start 3 parallel db executors
|
// Start 3 parallel db executors
|
||||||
// let addr = SyncArbiter::start(3, || {
|
let addr = SyncArbiter::start(3, || {
|
||||||
// DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
||||||
// });
|
});
|
||||||
|
|
||||||
// // Start http server
|
// Start http server
|
||||||
// HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
// App::with_state(State { db: addr.clone() })
|
App::with_state(State { db: addr.clone() })
|
||||||
// .resource("/{name}", |r| r.method(Method::GET).a(index))
|
.resource("/{name}", |r| r.method(Method::GET).a(index))
|
||||||
// })
|
})
|
||||||
// .bind("127.0.0.1:8080")
|
.bind("127.0.0.1:8080")
|
||||||
// .unwrap()
|
.unwrap()
|
||||||
// .start()
|
.start()
|
||||||
// .unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// println!("Started http server: 127.0.0.1:8080");
|
println!("Started http server: 127.0.0.1:8080");
|
||||||
// let _ = sys.run();
|
let _ = sys.run();
|
||||||
// }
|
}
|
||||||
// </main>
|
// </main>
|
||||||
|
|
||||||
// <index>
|
// <index>
|
||||||
// /// Async handler
|
/// Async 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"];
|
let name = &req.match_info()["name"];
|
||||||
|
|
||||||
// // Send message to `DbExecutor` actor
|
// Send message to `DbExecutor` actor
|
||||||
// req.state()
|
req.state()
|
||||||
// .db
|
.db
|
||||||
// .send(CreateUser {
|
.send(CreateUser {
|
||||||
// name: name.to_owned(),
|
name: name.to_owned(),
|
||||||
// })
|
})
|
||||||
// .from_err()
|
.from_err()
|
||||||
// .and_then(|res| match res {
|
.and_then(|res| match res {
|
||||||
// Ok(user) => Ok(HttpResponse::Ok().json(user)),
|
Ok(user) => Ok(HttpResponse::Ok().json(user)),
|
||||||
// Err(_) => Ok(HttpResponse::InternalServerError().into()),
|
Err(_) => Ok(HttpResponse::InternalServerError().into()),
|
||||||
// })
|
})
|
||||||
// .responder()
|
.responder()
|
||||||
// }
|
}
|
||||||
// </index>
|
// </index>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user