diff options
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/server.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs new file mode 100644 index 0000000..eb290db --- /dev/null +++ b/src/bin/server.rs | |||
@@ -0,0 +1,44 @@ | |||
1 | use actix_cors::Cors; | ||
2 | use actix_identity::{CookieIdentityPolicy, IdentityService}; | ||
3 | use actix_web::middleware; | ||
4 | use actix_web::{web, App, HttpServer}; | ||
5 | use diesel::r2d2::{ConnectionManager, Pool}; | ||
6 | use diesel::MysqlConnection; | ||
7 | use furby::handlers::smoke::manual_hello; | ||
8 | use furby::handlers::users; | ||
9 | use rand::Rng; | ||
10 | |||
11 | #[actix_web::main] | ||
12 | async fn main() -> std::io::Result<()> { | ||
13 | pretty_env_logger::init(); | ||
14 | |||
15 | let db_url = env!("DATABASE_URL"); | ||
16 | let manager = ConnectionManager::<MysqlConnection>::new(db_url); | ||
17 | let pool = Pool::builder() | ||
18 | .build(manager) | ||
19 | .expect("Failed to create pool."); | ||
20 | |||
21 | let private_key = rand::thread_rng().gen::<[u8; 32]>(); | ||
22 | HttpServer::new(move || { | ||
23 | App::new() | ||
24 | .wrap(IdentityService::new( | ||
25 | CookieIdentityPolicy::new(&private_key) | ||
26 | .name("user-login") | ||
27 | .secure(false), | ||
28 | )) | ||
29 | .wrap(Cors::new().supports_credentials().finish()) | ||
30 | .wrap(middleware::Logger::default()) | ||
31 | .data(pool.clone()) | ||
32 | .service( | ||
33 | web::scope("/user") | ||
34 | .route("/existing", web::post().to(users::name_exists)) | ||
35 | .route("/login", web::post().to(users::login)) | ||
36 | .route("/{uname}", web::get().to(users::user_details)) | ||
37 | .route("/new", web::post().to(users::new_user)), | ||
38 | ) | ||
39 | .route("/hey", web::get().to(manual_hello)) | ||
40 | }) | ||
41 | .bind("127.0.0.1:7878")? | ||
42 | .run() | ||
43 | .await | ||
44 | } | ||