2023-11-19 17:02:40 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2024-01-25 12:13:38 +01:00
|
|
|
use actix_web::{App, web, HttpServer, HttpResponse};
|
2022-12-20 19:21:05 +01:00
|
|
|
use clap::Parser;
|
|
|
|
|
2024-01-25 12:13:38 +01:00
|
|
|
use mime_guess::from_path;
|
2023-11-19 17:02:40 +01:00
|
|
|
use nom_nom_gc::handlers;
|
|
|
|
use nom_nom_gc::models::read_config;
|
|
|
|
use nom_nom_gc::models;
|
2024-01-23 14:06:45 +01:00
|
|
|
use nom_nom_gc::s3::check_bucket;
|
2024-01-25 12:13:38 +01:00
|
|
|
use rust_embed::RustEmbed;
|
2022-12-22 13:50:53 +01:00
|
|
|
|
2022-12-20 19:21:05 +01:00
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
struct CLIArgs {
|
|
|
|
#[arg(short, long)]
|
2023-10-06 15:47:15 +02:00
|
|
|
bind: String,
|
|
|
|
#[arg(short, long)]
|
2023-11-19 17:02:40 +01:00
|
|
|
config: Option<String>
|
2022-12-20 19:21:05 +01:00
|
|
|
}
|
|
|
|
|
2024-01-25 12:13:38 +01:00
|
|
|
#[derive(RustEmbed)]
|
|
|
|
#[folder = "src/static"]
|
|
|
|
struct Static;
|
|
|
|
|
|
|
|
async fn handle_embedded_file(path: web::Path<String>) -> HttpResponse {
|
|
|
|
match Static::get(&path) {
|
|
|
|
Some(content) => HttpResponse::Ok()
|
|
|
|
.content_type(from_path(&*path).first_or_octet_stream().as_ref())
|
|
|
|
.body(content.data.into_owned()),
|
|
|
|
None => HttpResponse::NotFound().body("404 Not Found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 19:21:05 +01:00
|
|
|
#[tokio::main]
|
2023-07-31 08:51:35 +02:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2022-12-20 19:21:05 +01:00
|
|
|
let args = CLIArgs::parse();
|
2023-09-30 20:23:30 +02:00
|
|
|
let addr: SocketAddr = args.bind.parse().unwrap_or_else(|_| panic!("Cannot bind to {}. Please provide a host and port like [::1]:8000", &args.bind));
|
2023-11-19 17:02:40 +01:00
|
|
|
let config_path = args.config.unwrap_or("/etc/nom-nom-gc/config.json".to_owned());
|
|
|
|
let config = read_config(&config_path)
|
2023-11-23 12:39:13 +01:00
|
|
|
.unwrap_or_else(|e| panic!("Cannot read config file: {}", e));
|
2024-01-23 14:06:45 +01:00
|
|
|
let state = models::AppState::new(config.clone()).await;
|
2023-11-19 17:02:40 +01:00
|
|
|
println!("Running DB migrations");
|
2023-11-23 12:39:13 +01:00
|
|
|
state.run_migrations().await.unwrap_or_else(|e| panic!("Db migration error: {}", e));
|
2024-01-23 14:06:45 +01:00
|
|
|
println!("Checking binary cache bucket");
|
|
|
|
let bucket = check_bucket(&state.s3_client, &config).await;
|
|
|
|
match bucket {
|
|
|
|
Ok(_) => println!("Connection to the bucket successful"),
|
|
|
|
Err(e) => panic!("Cannot connect to the binary cache bucket: {}", e)
|
|
|
|
}
|
2023-11-19 17:02:40 +01:00
|
|
|
println!("Server listening to {}", &args.bind);
|
2023-07-31 08:51:35 +02:00
|
|
|
HttpServer::new(
|
2023-10-06 15:47:15 +02:00
|
|
|
move || {
|
2024-01-25 12:13:38 +01:00
|
|
|
App::new()
|
|
|
|
.app_data(web::Data::new(state.clone()))
|
2023-07-31 08:51:35 +02:00
|
|
|
.route("/", web::get().to(handlers::landing_page))
|
2023-11-19 17:02:40 +01:00
|
|
|
.route("/account/register/{uuid}", web::get().to(handlers::webauthn_registration))
|
2023-07-31 08:51:35 +02:00
|
|
|
.route("/account/register-init", web::post().to(handlers::start_webauthn_registration))
|
|
|
|
.route("/account/register-finish", web::post().to(handlers::finish_webauthn_registration))
|
2023-11-23 22:08:13 +01:00
|
|
|
.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))
|
2023-12-13 17:36:25 +01:00
|
|
|
.route("/binary-cache/new", web::get().to(handlers::new_binary_cache))
|
|
|
|
.route("/binary-cache/new", web::post().to(handlers::new_binary_cache_post))
|
2024-01-23 14:06:45 +01:00
|
|
|
.route("/binary-cache/{id}", web::get().to(handlers::get_binary_cache))
|
2024-01-25 12:13:38 +01:00
|
|
|
.route("/static/{_:.*}", web::get().to(handle_embedded_file))
|
2023-07-31 08:51:35 +02:00
|
|
|
})
|
|
|
|
.bind(addr)
|
|
|
|
.unwrap()
|
|
|
|
.run()
|
|
|
|
.await
|
2022-12-19 20:02:47 +01:00
|
|
|
}
|