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.

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