2020-05-06 13:25:54 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2020-05-09 06:37:08 +00:00
|
|
|
#[macro_use]
|
2020-05-11 19:33:11 +00:00
|
|
|
extern crate derive_new;
|
2020-05-09 06:37:08 +00:00
|
|
|
extern crate bcrypt;
|
|
|
|
extern crate custom_error;
|
|
|
|
extern crate regex;
|
|
|
|
extern crate validator;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
use actix_web::{cookie::SameSite, middleware, web, App, HttpServer};
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
use actix_files as fs;
|
2020-05-09 06:37:08 +00:00
|
|
|
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
2020-05-06 13:25:54 +00:00
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use diesel::r2d2::{self, ConnectionManager};
|
2020-08-12 07:05:26 +00:00
|
|
|
use listenfd::ListenFd;
|
|
|
|
use types::DbPool;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
|
|
|
mod actions;
|
2020-05-09 06:37:08 +00:00
|
|
|
mod errors;
|
|
|
|
mod middlewares;
|
2020-05-06 13:25:54 +00:00
|
|
|
mod models;
|
|
|
|
mod routes;
|
|
|
|
mod schema;
|
2021-04-21 05:41:48 +00:00
|
|
|
mod services;
|
2020-05-06 13:25:54 +00:00
|
|
|
mod types;
|
2020-05-09 06:37:08 +00:00
|
|
|
mod utils;
|
2020-05-06 13:25:54 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
2020-08-12 07:05:26 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AppConfig {
|
|
|
|
hash_cost: u32,
|
|
|
|
pool: DbPool,
|
|
|
|
}
|
2020-05-06 13:25:54 +00:00
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
#[actix_web::main]
|
2020-05-06 13:25:54 +00:00
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
std::env::set_var("RUST_LOG", "debug");
|
|
|
|
env_logger::init();
|
|
|
|
dotenv::dotenv().ok();
|
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
// let _basic_auth_middleware =
|
|
|
|
// HttpAuthentication::basic(utils::auth::validator);
|
2020-05-06 13:25:54 +00:00
|
|
|
|
|
|
|
// set up database connection pool
|
2020-05-12 12:07:53 +00:00
|
|
|
let connspec =
|
|
|
|
std::env::var("DATABASE_URL").expect("DATABASE_URL NOT FOUND");
|
2020-05-06 13:25:54 +00:00
|
|
|
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
|
|
|
let pool = r2d2::Pool::builder()
|
|
|
|
.build(manager)
|
|
|
|
.expect("Failed to create pool.");
|
|
|
|
|
2020-05-11 19:33:11 +00:00
|
|
|
diesel_migrations::run_pending_migrations(&pool.get().unwrap())
|
|
|
|
.expect("Error running migrations");
|
|
|
|
|
2020-08-12 07:05:26 +00:00
|
|
|
let hash_cost = std::env::var("HASH_COST")
|
|
|
|
.map_err(|e| e.to_string())
|
|
|
|
.and_then(|x| x.parse::<u32>().map_err(|e| e.to_string()))
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
info!("Error parsing hash cost env variable, or it is not set. Using default cost of 8");
|
|
|
|
8
|
|
|
|
});
|
|
|
|
|
2021-04-21 05:41:48 +00:00
|
|
|
let config: AppConfig = AppConfig {
|
|
|
|
pool: pool.clone(),
|
|
|
|
hash_cost,
|
|
|
|
};
|
|
|
|
|
|
|
|
// let user_controller = UserController {
|
|
|
|
// user_service: &user_service,
|
|
|
|
// };
|
2020-08-12 07:05:26 +00:00
|
|
|
|
2020-05-06 13:25:54 +00:00
|
|
|
let addr = std::env::var("BIND_ADDRESS").expect("BIND ADDRESS NOT FOUND");
|
|
|
|
info!("Starting server {}", addr);
|
|
|
|
let private_key = rand::thread_rng().gen::<[u8; 32]>();
|
|
|
|
let app = move || {
|
|
|
|
App::new()
|
2020-08-12 07:05:26 +00:00
|
|
|
.data(config.clone())
|
2020-05-06 13:25:54 +00:00
|
|
|
.wrap(IdentityService::new(
|
|
|
|
CookieIdentityPolicy::new(&private_key)
|
|
|
|
.name("my-app-auth")
|
|
|
|
.secure(false)
|
2021-04-21 05:41:48 +00:00
|
|
|
.same_site(SameSite::Lax),
|
2020-05-06 13:25:54 +00:00
|
|
|
))
|
|
|
|
.wrap(middleware::Logger::default())
|
2020-08-11 19:51:20 +00:00
|
|
|
.service(
|
|
|
|
web::scope("/api/authzd") // endpoint requiring authentication
|
2021-04-21 05:41:48 +00:00
|
|
|
// .wrap(_basic_auth_middleware.clone())
|
2020-08-11 19:51:20 +00:00
|
|
|
.service(routes::users::get_user)
|
|
|
|
.service(routes::users::get_all_users),
|
|
|
|
)
|
2021-04-21 05:41:48 +00:00
|
|
|
// .route("/api/users/get", web::get().to(user_controller.get_user.into()))
|
2020-08-11 19:51:20 +00:00
|
|
|
.service(web::scope("/api/public")) // public endpoint - not implemented yet
|
2020-05-09 06:37:08 +00:00
|
|
|
.service(routes::auth::login)
|
|
|
|
.service(routes::auth::logout)
|
|
|
|
.service(routes::auth::index)
|
2020-08-11 19:51:20 +00:00
|
|
|
.service(routes::users::add_user)
|
2020-05-06 13:25:54 +00:00
|
|
|
.service(fs::Files::new("/", "./static"))
|
|
|
|
};
|
2020-08-12 07:05:26 +00:00
|
|
|
// HttpServer::new(app).bind(addr)?.run().await
|
|
|
|
let mut listenfd = ListenFd::from_env();
|
|
|
|
let mut server = HttpServer::new(app);
|
|
|
|
server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
|
|
|
|
server.listen(l)?
|
|
|
|
} else {
|
|
|
|
server.bind(addr)?
|
|
|
|
};
|
|
|
|
server.run().await
|
2020-05-06 13:25:54 +00:00
|
|
|
}
|