main.rs 1006 B

123456789101112131415161718192021222324252627282930313233343536
  1. use actix_web::{App, HttpServer, web};
  2. use std::sync::OnceLock;
  3. use mongodb::{Client, Database};
  4. mod routes;
  5. mod models;
  6. mod controllers;
  7. mod dto;
  8. mod app_error;
  9. pub static HTML: OnceLock<String> = OnceLock::new();
  10. #[actix_web::main]
  11. async fn main() -> std::io::Result<()> {
  12. let db = connect_db("mongodb://localhost:27017", "suma").await;
  13. let html = std::fs::read_to_string("src/views/build.html")
  14. .expect("Failed to read HTML");
  15. HTML.set(html).expect("Failed to set global HTML");
  16. HttpServer::new(move || {
  17. App::new()
  18. .app_data(web::Data::new(db.clone()))
  19. .configure(routes::other::config)
  20. .configure(routes::user::config)
  21. .configure(routes::account::config)
  22. })
  23. .bind(("127.0.0.1", 8000))?
  24. .run()
  25. .await
  26. }
  27. pub async fn connect_db(uri: &str, db_name: &str) -> Database {
  28. let client = Client::with_uri_str(uri).await.expect("Failed to connect to database");
  29. client.database(db_name)
  30. }