-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
main_diesel.rs
135 lines (117 loc) · 3.93 KB
/
main_diesel.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#[global_allocator]
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
#[macro_use]
extern crate diesel;
use actix::prelude::*;
use actix_http::error::ErrorInternalServerError;
use actix_web::{http, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use askama::Template;
use bytes::BytesMut;
mod db;
mod models;
mod schema;
mod utils;
use utils::Writer;
async fn world_row(db: web::Data<Addr<db::DbExecutor>>) -> Result<HttpResponse, Error> {
let res = db
.send(db::RandomWorld)
.await
.map_err(|e| ErrorInternalServerError(e))?;
match res {
Ok(row) => {
let mut body = BytesMut::with_capacity(33);
serde_json::to_writer(Writer(&mut body), &row).unwrap();
Ok(HttpResponse::Ok()
.header(http::header::SERVER, "Actix")
.header(http::header::CONTENT_TYPE, "application/json")
.body(body))
}
Err(_) => Ok(HttpResponse::InternalServerError().into()),
}
}
async fn queries(
req: HttpRequest,
db: web::Data<Addr<db::DbExecutor>>,
) -> Result<HttpResponse, Error> {
// get queries parameter
let q = utils::get_query_param(req.query_string());
// run sql queries
let res = db
.send(db::RandomWorlds(q))
.await
.map_err(|e| ErrorInternalServerError(e))?;
if let Ok(worlds) = res {
let mut body = BytesMut::with_capacity(35 * worlds.len());
serde_json::to_writer(Writer(&mut body), &worlds).unwrap();
Ok(HttpResponse::Ok()
.header(http::header::SERVER, "Actix")
.header(http::header::CONTENT_TYPE, "application/json")
.body(body))
} else {
Ok(HttpResponse::InternalServerError().into())
}
}
async fn updates(
req: HttpRequest,
db: web::Data<Addr<db::DbExecutor>>,
) -> Result<HttpResponse, Error> {
// get queries parameter
let q = utils::get_query_param(req.query_string());
// update worlds
let res = db
.send(db::UpdateWorld(q))
.await
.map_err(|e| ErrorInternalServerError(e))?;
if let Ok(worlds) = res {
let mut body = BytesMut::with_capacity(35 * worlds.len());
serde_json::to_writer(Writer(&mut body), &worlds).unwrap();
Ok(HttpResponse::Ok()
.header(http::header::SERVER, "Actix")
.header(http::header::CONTENT_TYPE, "application/json")
.body(body))
} else {
Ok(HttpResponse::InternalServerError().into())
}
}
#[derive(Template)]
#[template(path = "fortune.html")]
struct FortuneTemplate<'a> {
items: &'a Vec<models::Fortune>,
}
async fn fortune(db: web::Data<Addr<db::DbExecutor>>) -> Result<HttpResponse, Error> {
let res = db
.send(db::TellFortune)
.await
.map_err(|e| ErrorInternalServerError(e))?;
match res {
Ok(rows) => {
let tmpl = FortuneTemplate { items: &rows };
let res = tmpl.render().unwrap();
Ok(HttpResponse::Ok()
.header(http::header::SERVER, "Actix")
.header(http::header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(res))
}
Err(_) => Ok(HttpResponse::InternalServerError().into()),
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
println!("Starting http server: 127.0.0.1:8080");
let db_url = "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
// Start db executor actors
let addr =
SyncArbiter::start(num_cpus::get() * 3, move || db::DbExecutor::new(db_url));
// start http server
HttpServer::new(move || {
App::new()
.data(addr.clone())
.service(web::resource("/db").to(world_row))
.service(web::resource("/fortunes").to(fortune))
.service(web::resource("/queries").to(queries))
.service(web::resource("/updates").to(updates))
})
.bind("0.0.0.0:8080")?
.run()
.await
}