nom-nom-nix-gc/src/server/bin/main.rs

49 lines
2.0 KiB
Rust

use std::net::SocketAddr;
use actix_web::{App, web, HttpServer};
use clap::Parser;
use nom_nom_gc::handlers;
use nom_nom_gc::models::read_config;
use nom_nom_gc::models;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct CLIArgs {
#[arg(short, long)]
bind: String,
#[arg(short, long)]
config: Option<String>
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
let args = CLIArgs::parse();
let addr: SocketAddr = args.bind.parse().unwrap_or_else(|_| panic!("Cannot bind to {}. Please provide a host and port like [::1]:8000", &args.bind));
let config_path = args.config.unwrap_or("/etc/nom-nom-gc/config.json".to_owned());
let config = read_config(&config_path)
.unwrap_or_else(|e| panic!("Cannot read config file: {}", e));
let state = models::AppState::new(config);
println!("Running DB migrations");
state.run_migrations().await.unwrap_or_else(|e| panic!("Db migration error: {}", e));
println!("Server listening to {}", &args.bind);
HttpServer::new(
move || {
App::new().app_data(web::Data::new(state.clone()))
.route("/", web::get().to(handlers::landing_page))
.route("/account/register/{uuid}", web::get().to(handlers::webauthn_registration))
.route("/account/register-init", web::post().to(handlers::start_webauthn_registration))
.route("/account/register-finish", web::post().to(handlers::finish_webauthn_registration))
.route("/login", web::get().to(handlers::webauthn_login))
.route("/login/init", web::post().to(handlers::webauthn_login_init))
.route("/login/finish", web::post().to(handlers::webauthn_login_finish))
.route("/binary-cache/new", web::get().to(handlers::new_binary_cache))
.route("/binary-cache/new", web::post().to(handlers::new_binary_cache_post))
})
.bind(addr)
.unwrap()
.run()
.await
}