aboutsummaryrefslogtreecommitdiff
path: root/src/bin/server.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-24 05:21:40 +0000
committerAkshay <[email protected]>2020-12-24 05:21:40 +0000
commit9d2b6ee10ec5359cc91769d430485c8c869ba1a8 (patch)
treeb2d541e575ab6e3f70cb709f156f06afca085881 /src/bin/server.rs
parent7c65421328552b08e64df25e224fe9d54d363e6e (diff)
monorepo
Diffstat (limited to 'src/bin/server.rs')
-rw-r--r--src/bin/server.rs94
1 files changed, 0 insertions, 94 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs
deleted file mode 100644
index 7c67e4f..0000000
--- a/src/bin/server.rs
+++ /dev/null
@@ -1,94 +0,0 @@
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::{cart_items, product, rating, 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 .domain("127.0.0.1")
28 .path("/")
29 .same_site(actix_web::cookie::SameSite::None)
30 .http_only(true)
31 .secure(false),
32 ))
33 .wrap(
34 Cors::default()
35 .allowed_origin("http://127.0.0.1:8000")
36 .allowed_origin("http://localhost:8000")
37 .allow_any_method()
38 .allow_any_header(),
39 )
40 .wrap(
41 middleware::DefaultHeaders::new()
42 .header("Access-Control-Allow-Credentials", "true")
43 .header("Access-Control-Expose-Headers", "set-cookie"),
44 )
45 .wrap(middleware::Logger::default())
46 .data(pool.clone())
47 .service(
48 web::scope("/user")
49 .route("/existing", web::post().to(users::name_exists))
50 .route("/login", web::post().to(users::login))
51 .route("/{uname}", web::get().to(users::user_details))
52 .route("/new", web::post().to(users::new_user))
53 .route(
54 "/change_password",
55 web::post().to(users::change_password),
56 ),
57 )
58 .service(
59 web::scope("/product")
60 .route("/catalog", web::get().to(product::get_all_products))
61 .route("/new", web::post().to(product::new_product))
62 .route("/{id}", web::get().to(product::product_details))
63 .route(
64 "/reviews/{id}",
65 web::get().to(product::get_product_reviews),
66 )
67 .route(
68 "/update_product/{id}",
69 web::post().to(product::update_product),
70 ),
71 )
72 .service(
73 web::scope("/cart")
74 .route(
75 "/items",
76 web::get().to(cart_items::get_user_cart_items),
77 )
78 .route("/add", web::post().to(cart_items::add_to_cart))
79 .route(
80 "/remove",
81 web::post().to(cart_items::remove_from_cart),
82 ),
83 )
84 .service(
85 web::scope("/rating")
86 .route("/add", web::post().to(rating::add_rating))
87 .route("/remove", web::post().to(rating::remove_rating)),
88 )
89 .route("/hey", web::get().to(manual_hello))
90 })
91 .bind("127.0.0.1:7878")?
92 .run()
93 .await
94}