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.

134 lines
3.7 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
3 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 std::io;
  16. use std::io::ErrorKind;
  17. use types::DbPool;
  18. mod actions;
  19. mod errors;
  20. mod middlewares;
  21. mod models;
  22. mod routes;
  23. mod schema;
  24. mod services;
  25. mod types;
  26. mod utils;
  27. #[macro_use]
  28. extern crate log;
  29. #[derive(Clone)]
  30. pub struct AppConfig {
  31. hash_cost: u32,
  32. pool: DbPool,
  33. }
  34. #[actix_web::main]
  35. async fn main() -> std::io::Result<()> {
  36. std::env::set_var("RUST_LOG", "debug");
  37. env_logger::init();
  38. dotenv::dotenv().map_err(|err| {
  39. io::Error::new(
  40. ErrorKind::Other,
  41. format!("Failed to set up env: {:?}", err),
  42. )
  43. })?;
  44. // let _basic_auth_middleware =
  45. // HttpAuthentication::basic(utils::auth::validator);
  46. // set up database connection pool
  47. let connspec = std::env::var("DATABASE_URL").map_err(|err| {
  48. io::Error::new(
  49. ErrorKind::Other,
  50. format!("Database url is not set: {:?}", err),
  51. )
  52. })?;
  53. let manager = ConnectionManager::<SqliteConnection>::new(connspec);
  54. let pool = r2d2::Pool::builder().build(manager).map_err(|err| {
  55. io::Error::new(
  56. ErrorKind::Other,
  57. format!("Failed to create pool: {:?}", err),
  58. )
  59. })?;
  60. {
  61. let conn = &pool.get().map_err(|err| {
  62. io::Error::new(
  63. ErrorKind::Other,
  64. format!("Failed to get connection: {:?}", err),
  65. )
  66. })?;
  67. diesel_migrations::run_pending_migrations(conn).map_err(|err| {
  68. io::Error::new(
  69. ErrorKind::Other,
  70. format!("Error running migrations: {:?}", err),
  71. )
  72. })?;
  73. }
  74. let hash_cost = std::env::var("HASH_COST")
  75. .map_err(|e| e.to_string())
  76. .and_then(|x| x.parse::<u32>().map_err(|e| e.to_string()))
  77. .unwrap_or_else(|err| {
  78. info!(
  79. "Error getting hash cost: {:?}. Using default cost of 8",
  80. err
  81. );
  82. 8
  83. });
  84. let config: AppConfig = AppConfig {
  85. pool: pool.clone(),
  86. hash_cost,
  87. };
  88. // let user_controller = UserController {
  89. // user_service: &user_service,
  90. // };
  91. let addr = std::env::var("BIND_ADDRESS")
  92. .unwrap_or_else(|_| "127.0.0.1:7800".to_owned());
  93. info!("Starting server at {}", addr);
  94. let private_key = rand::thread_rng().gen::<[u8; 32]>();
  95. let app = move || {
  96. App::new()
  97. .data(config.clone())
  98. .wrap(IdentityService::new(
  99. CookieIdentityPolicy::new(&private_key)
  100. .name("my-app-auth")
  101. .secure(false)
  102. .same_site(SameSite::Lax),
  103. ))
  104. .wrap(middleware::Logger::default())
  105. .service(
  106. web::scope("/api")
  107. .service(routes::users::get_user)
  108. .service(routes::users::get_all_users),
  109. )
  110. // .route("/api/users/get", web::get().to(user_controller.get_user.into()))
  111. .service(web::scope("/api/public")) // public endpoint - not implemented yet
  112. .service(routes::auth::login)
  113. .service(routes::auth::logout)
  114. .service(routes::auth::index)
  115. .service(routes::users::add_user)
  116. .service(fs::Files::new("/", "./static"))
  117. };
  118. HttpServer::new(app).bind(addr)?.run().await
  119. }