2022-03-30 08:29:10 +00:00
|
|
|
use actix_web::{App, get, HttpServer, web};
|
|
|
|
use actix_web::middleware::Logger;
|
2022-03-30 08:14:39 +00:00
|
|
|
use clap::Parser;
|
2022-03-29 17:32:31 +00:00
|
|
|
|
|
|
|
use basic_oidc::constants::{DEFAULT_ADMIN_PASSWORD, DEFAULT_ADMIN_USERNAME};
|
2022-03-30 08:14:39 +00:00
|
|
|
use basic_oidc::controllers::assets_controller::assets_route;
|
2022-03-30 08:29:10 +00:00
|
|
|
use basic_oidc::controllers::login_controller::login_route;
|
2022-03-29 17:32:31 +00:00
|
|
|
use basic_oidc::data::app_config::AppConfig;
|
|
|
|
use basic_oidc::data::entity_manager::EntityManager;
|
|
|
|
use basic_oidc::data::user::{hash_password, User};
|
|
|
|
|
|
|
|
#[get("/health")]
|
2022-03-30 08:14:39 +00:00
|
|
|
async fn health() -> &'static str {
|
2022-03-29 17:32:31 +00:00
|
|
|
"Running"
|
2022-03-29 16:19:23 +00:00
|
|
|
}
|
2022-03-29 17:32:31 +00:00
|
|
|
|
2022-03-30 08:14:39 +00:00
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
2022-03-29 17:32:31 +00:00
|
|
|
|
2022-03-30 08:14:39 +00:00
|
|
|
let config: AppConfig = AppConfig::parse();
|
2022-03-29 17:32:31 +00:00
|
|
|
|
|
|
|
if !config.storage_path().exists() {
|
2022-03-30 06:42:18 +00:00
|
|
|
log::error!(
|
|
|
|
"Specified storage path {:?} does not exists!",
|
|
|
|
config.storage_path()
|
|
|
|
);
|
2022-03-29 17:32:31 +00:00
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut users = EntityManager::<User>::open_or_create(config.users_file())
|
|
|
|
.expect("Failed to load users list!");
|
|
|
|
|
|
|
|
// Create initial user if required
|
2022-03-30 06:42:18 +00:00
|
|
|
if users.is_empty() {
|
2022-03-29 17:32:31 +00:00
|
|
|
log::info!("Create default {} user", DEFAULT_ADMIN_USERNAME);
|
2022-03-30 06:42:18 +00:00
|
|
|
let default_admin = User {
|
|
|
|
username: DEFAULT_ADMIN_USERNAME.to_string(),
|
|
|
|
password: hash_password(DEFAULT_ADMIN_PASSWORD).unwrap(),
|
|
|
|
need_reset_password: true,
|
|
|
|
authorized_services: None,
|
|
|
|
admin: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
users
|
|
|
|
.insert(default_admin)
|
2022-03-29 17:32:31 +00:00
|
|
|
.expect("Failed to create initial user!");
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:14:39 +00:00
|
|
|
log::info!("Server will listen on {}", config.listen_address);
|
|
|
|
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
2022-03-30 08:29:10 +00:00
|
|
|
.wrap(Logger::default())
|
2022-03-30 08:14:39 +00:00
|
|
|
.service(health)
|
|
|
|
.route("/assets/{path:.*}", web::get().to(assets_route))
|
2022-03-30 08:29:10 +00:00
|
|
|
.route("/login", web::get().to(login_route))
|
2022-03-30 08:14:39 +00:00
|
|
|
})
|
|
|
|
.bind(config.listen_address)?
|
|
|
|
.run()
|
|
|
|
.await
|
2022-03-30 06:42:18 +00:00
|
|
|
}
|