59 lines
1.5 KiB
Rust
59 lines
1.5 KiB
Rust
use std::process::exit;
|
|
|
|
use clap::Parser;
|
|
use rocket::{Rocket, Build};
|
|
use rocket::fairing::AdHoc;
|
|
use figment::Figment;
|
|
|
|
use crate::config::Config;
|
|
use crate::routes::users::*;
|
|
use crate::routes::zones::*;
|
|
use crate::{DbConn};
|
|
use crate::cli::NomiloCommand;
|
|
|
|
|
|
#[derive(Parser)]
|
|
pub struct ServeCommand;
|
|
|
|
async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
|
|
embed_migrations!("migrations");
|
|
|
|
let conn = match DbConn::get_one(&rocket).await {
|
|
Some(c) => c,
|
|
None => {
|
|
error!("Could not get a database connection");
|
|
exit(1);
|
|
}
|
|
};
|
|
|
|
if let Err(e) = conn.run(|c| embedded_migrations::run(c)).await {
|
|
error!("Error running migrations: {}", e);
|
|
exit(1)
|
|
}
|
|
|
|
rocket
|
|
|
|
}
|
|
|
|
impl NomiloCommand for ServeCommand {
|
|
fn run(self, figment: Figment, app_config: Config) {
|
|
rocket::async_main(async move {
|
|
let _res = rocket::custom(figment)
|
|
.manage(app_config)
|
|
.attach(DbConn::fairing())
|
|
.attach(AdHoc::on_ignite("Database migration", run_migrations))
|
|
.mount("/api/v1", routes![
|
|
get_zone_records,
|
|
create_zone_records,
|
|
update_zone_records,
|
|
delete_zone_records,
|
|
get_zones,
|
|
create_zone,
|
|
add_member_to_zone,
|
|
create_auth_token,
|
|
create_user,
|
|
])
|
|
.launch().await;
|
|
});
|
|
}
|
|
}
|