-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rs
68 lines (60 loc) · 2.23 KB
/
server.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
use actix_web::dev::Server;
use actix_web::middleware::{ErrorHandlers, Logger};
use actix_web::{http, web, App, HttpServer};
use crate::auth::tokens_cache::TokensCache;
use crate::file_server::FileServer;
use crate::http::handlers::{config_handlers, render_40x, render_500};
use diesel::{
r2d2::{self, ConnectionManager},
SqliteConnection,
};
use r2d2::Pool;
use ring::hmac;
use std::net::TcpListener;
use std::sync::{Arc, RwLock};
pub struct AppData {
pub tokens_cache: TokensCache,
pub crypto_key: hmac::Key,
pub pool: Pool<ConnectionManager<SqliteConnection>>,
pub s3_fs: FileServer,
pub cors_origin: String,
}
pub fn run(listener: TcpListener, data: AppData) -> Result<Server, std::io::Error> {
let tokens_cache = Arc::new(RwLock::new(data.tokens_cache));
let crypto_key = Arc::new(data.crypto_key);
let pool = data.pool;
let s3_fs = Arc::new(data.s3_fs);
let cors_origin = data.cors_origin;
let server = HttpServer::new(move || {
let cors_origin = &cors_origin;
let mut cors_handler = actix_cors::Cors::default()
.allowed_methods(vec!["HEAD", "OPTIONS", "GET", "POST", "PUT", "DELETE"])
.allowed_headers(vec![
http::header::AUTHORIZATION,
http::header::ACCEPT,
http::header::CONTENT_TYPE,
])
.block_on_origin_mismatch(true)
.max_age(86400);
cors_handler = if cors_origin == "*" {
cors_handler.allow_any_origin()
} else {
cors_handler.allowed_origin(cors_origin)
};
App::new()
.app_data(web::Data::new(pool.clone()))
.app_data(web::Data::new(crypto_key.clone()))
.app_data(web::Data::new(tokens_cache.clone()))
.app_data(web::Data::new(s3_fs.clone()))
.wrap(ErrorHandlers::new().handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500))
.wrap(ErrorHandlers::new().handler(http::StatusCode::BAD_REQUEST, render_40x))
.wrap(Logger::default())
.wrap(cors_handler)
.wrap(sentry_actix::Sentry::with_transaction())
.configure(config_handlers)
})
.listen(listener)?
.run();
// No .await here!
Ok(server)
}