aboutsummaryrefslogtreecommitdiff
path: root/backend/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'backend/src/bin')
-rw-r--r--backend/src/bin/server.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/backend/src/bin/server.rs b/backend/src/bin/server.rs
new file mode 100644
index 0000000..7c67e4f
--- /dev/null
+++ b/backend/src/bin/server.rs
@@ -0,0 +1,94 @@
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}