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.

113 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #[macro_use]
  2. extern crate diesel;
  3. #[macro_use]
  4. extern crate derive_new;
  5. extern crate bcrypt;
  6. extern crate custom_error;
  7. extern crate regex;
  8. extern crate validator;
  9. use actix_web::{cookie::SameSite, middleware, web, App, HttpServer};
  10. use actix_files as fs;
  11. use actix_identity::{CookieIdentityPolicy, IdentityService};
  12. use rand::Rng;
  13. use diesel::prelude::*;
  14. use diesel::r2d2::{self, ConnectionManager};
  15. use listenfd::ListenFd;
  16. use types::DbPool;
  17. mod actions;
  18. mod errors;
  19. mod middlewares;
  20. mod models;
  21. mod routes;
  22. mod schema;
  23. mod services;
  24. mod types;
  25. mod utils;
  26. #[macro_use]
  27. extern crate log;
  28. #[derive(Clone)]
  29. pub struct AppConfig {
  30. hash_cost: u32,
  31. pool: DbPool,
  32. }
  33. #[actix_web::main]
  34. async fn main() -> std::io::Result<()> {
  35. std::env::set_var("RUST_LOG", "debug");
  36. env_logger::init();
  37. dotenv::dotenv().ok();
  38. // let _basic_auth_middleware =
  39. // HttpAuthentication::basic(utils::auth::validator);
  40. // set up database connection pool
  41. let connspec =
  42. std::env::var("DATABASE_URL").expect("DATABASE_URL NOT FOUND");
  43. let manager = ConnectionManager::<SqliteConnection>::new(connspec);
  44. let pool = r2d2::Pool::builder()
  45. .build(manager)
  46. .expect("Failed to create pool.");
  47. diesel_migrations::run_pending_migrations(&pool.get().unwrap())
  48. .expect("Error running migrations");
  49. let hash_cost = std::env::var("HASH_COST")
  50. .map_err(|e| e.to_string())
  51. .and_then(|x| x.parse::<u32>().map_err(|e| e.to_string()))
  52. .unwrap_or_else(|_| {
  53. info!("Error parsing hash cost env variable, or it is not set. Using default cost of 8");
  54. 8
  55. });
  56. let config: AppConfig = AppConfig {
  57. pool: pool.clone(),
  58. hash_cost,
  59. };
  60. // let user_controller = UserController {
  61. // user_service: &user_service,
  62. // };
  63. let addr = std::env::var("BIND_ADDRESS").expect("BIND ADDRESS NOT FOUND");
  64. info!("Starting server {}", addr);
  65. let private_key = rand::thread_rng().gen::<[u8; 32]>();
  66. let app = move || {
  67. App::new()
  68. .data(config.clone())
  69. .wrap(IdentityService::new(
  70. CookieIdentityPolicy::new(&private_key)
  71. .name("my-app-auth")
  72. .secure(false)
  73. .same_site(SameSite::Lax),
  74. ))
  75. .wrap(middleware::Logger::default())
  76. .service(
  77. web::scope("/api/authzd") // endpoint requiring authentication
  78. // .wrap(_basic_auth_middleware.clone())
  79. .service(routes::users::get_user)
  80. .service(routes::users::get_all_users),
  81. )
  82. // .route("/api/users/get", web::get().to(user_controller.get_user.into()))
  83. .service(web::scope("/api/public")) // public endpoint - not implemented yet
  84. .service(routes::auth::login)
  85. .service(routes::auth::logout)
  86. .service(routes::auth::index)
  87. .service(routes::users::add_user)
  88. .service(fs::Files::new("/", "./static"))
  89. };
  90. // HttpServer::new(app).bind(addr)?.run().await
  91. let mut listenfd = ListenFd::from_env();
  92. let mut server = HttpServer::new(app);
  93. server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
  94. server.listen(l)?
  95. } else {
  96. server.bind(addr)?
  97. };
  98. server.run().await
  99. }