You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6 KiB

  1. #![forbid(unsafe_code)]
  2. #[macro_use]
  3. extern crate diesel;
  4. #[macro_use]
  5. extern crate derive_new;
  6. #[macro_use]
  7. extern crate log;
  8. mod actions;
  9. mod errors;
  10. mod middlewares;
  11. pub mod models;
  12. mod routes;
  13. mod schema;
  14. mod services;
  15. mod types;
  16. mod utils;
  17. use actix_files as fs;
  18. use actix_identity::{CookieIdentityPolicy, IdentityService};
  19. use actix_web::{cookie::SameSite, middleware, web, App, HttpServer};
  20. use actix_web::{middleware::Logger, web::ServiceConfig};
  21. use rand::Rng;
  22. use serde::Deserialize;
  23. use types::DbPool;
  24. build_info::build_info!(pub fn get_build_info);
  25. #[derive(Deserialize, Debug, Clone)]
  26. pub struct EnvConfig {
  27. pub database_url: String,
  28. pub http_host: String,
  29. #[serde(default = "default_hash_cost")]
  30. pub hash_cost: u8,
  31. }
  32. #[derive(Deserialize, Debug, Clone)]
  33. pub struct AppConfig {
  34. pub hash_cost: u8,
  35. }
  36. #[derive(Clone)]
  37. pub struct AppData {
  38. pub config: AppConfig,
  39. pub pool: DbPool,
  40. }
  41. pub fn default_hash_cost() -> u8 {
  42. 8
  43. }
  44. pub fn configure_app(app_data: AppData) -> Box<dyn Fn(&mut ServiceConfig)> {
  45. Box::new(move |cfg: &mut ServiceConfig| {
  46. cfg.data(app_data.clone())
  47. .service(
  48. web::scope("/api")
  49. .service(routes::users::get_user)
  50. .service(routes::users::get_all_users)
  51. .service(web::scope("/get").route(
  52. "/build-info",
  53. web::get().to(routes::misc::build_info_req),
  54. )),
  55. )
  56. // .route("/api/users/get", web::get().to(user_controller.get_user.into()))
  57. .service(web::scope("/api/public")) // public endpoint - not implemented yet
  58. .service(routes::auth::login)
  59. .service(routes::auth::logout)
  60. .service(routes::auth::index)
  61. .service(routes::users::add_user)
  62. .service(fs::Files::new("/", "./static"));
  63. })
  64. }
  65. pub fn id_service(
  66. private_key: &[u8],
  67. ) -> actix_identity::IdentityService<CookieIdentityPolicy> {
  68. IdentityService::new(
  69. CookieIdentityPolicy::new(&private_key)
  70. .name("my-app-auth")
  71. .secure(false)
  72. .same_site(SameSite::Lax),
  73. )
  74. }
  75. pub fn app_logger() -> Logger {
  76. middleware::Logger::default()
  77. }
  78. pub async fn run(addr: String, app_data: AppData) -> std::io::Result<()> {
  79. info!("Starting server at {}", addr);
  80. let private_key = rand::thread_rng().gen::<[u8; 32]>();
  81. let app = move || {
  82. App::new()
  83. .configure(configure_app(app_data.clone()))
  84. .wrap(id_service(&private_key))
  85. .wrap(app_logger())
  86. };
  87. HttpServer::new(app).bind(addr)?.run().await
  88. }