aboutsummaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-04 08:53:46 +0100
committerAkshay <[email protected]>2020-10-04 08:53:46 +0100
commit057382f8d138c63cdc48b0facd9bf4a4a229057a (patch)
treeb30ba227c49918e0e65fc14068307cf11a9248bc /src/bin
init
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/server.rs44
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 @@
1use actix_cors::Cors;
2use actix_identity::{CookieIdentityPolicy, IdentityService};
3use actix_web::middleware;
4use actix_web::{web, App, HttpServer};
5use diesel::r2d2::{ConnectionManager, Pool};
6use diesel::MysqlConnection;
7use furby::handlers::smoke::manual_hello;
8use furby::handlers::users;
9use rand::Rng;
10
11#[actix_web::main]
12async 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}