aboutsummaryrefslogtreecommitdiff
path: root/src/bin/server.rs
blob: c28d04118528455a13438de376dcd23be46474f2 (plain)
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
use actix_cors::Cors;
use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::middleware;
use actix_web::{web, App, HttpServer};
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::MysqlConnection;
use furby::handlers::smoke::manual_hello;
use furby::handlers::{product, users};
use rand::Rng;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    pretty_env_logger::init();

    let db_url = env!("DATABASE_URL");
    let manager = ConnectionManager::<MysqlConnection>::new(db_url);
    let pool = Pool::builder()
        .build(manager)
        .expect("Failed to create pool.");

    let private_key = rand::thread_rng().gen::<[u8; 32]>();
    HttpServer::new(move || {
        App::new()
            .wrap(IdentityService::new(
                CookieIdentityPolicy::new(&private_key)
                    .name("user-login")
                    .secure(false),
            ))
            .wrap(Cors::new().supports_credentials().finish())
            .wrap(middleware::Logger::default())
            .data(pool.clone())
            .service(
                web::scope("/user")
                    .route("/existing", web::post().to(users::name_exists))
                    .route("/login", web::post().to(users::login))
                    .route("/{uname}", web::get().to(users::user_details))
                    .route("/new", web::post().to(users::new_user))
                    .route(
                        "/change_password",
                        web::post().to(users::change_password),
                    ),
            )
            .service(
                web::scope("/product")
                    .route("/new", web::post().to(product::new_product)),
            )
            .route("/hey", web::get().to(manual_hello))
    })
    .bind("127.0.0.1:7878")?
    .run()
    .await
}