nomilo/src/main.rs

66 lines
1.6 KiB
Rust

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate diesel;
#[macro_use] extern crate diesel_migrations;
mod routes;
mod cli;
mod config;
mod dns;
mod models;
mod schema;
mod template;
mod controllers;
use std::process::exit;
use clap::Parser;
use figment::{Figment, Profile, providers::{Format, Toml, Env}};
use rocket_sync_db_pools::database;
use diesel::prelude::*;
use crate::cli::{NomiloCli, NomiloCommand};
#[database("sqlite")]
pub struct DbConn(diesel::SqliteConnection);
pub fn get_db_conn(figment: &Figment) -> diesel::SqliteConnection {
let url = match figment.focus("databases.sqlite").extract_inner::<String>("url") {
Ok(url) => url,
Err(e) => {
eprintln!("Error loading configuration: {}", e);
exit(1);
}
};
match diesel::SqliteConnection::establish(&url) {
Ok(c) => c,
Err(e) => {
eprintln!("Error connecting to database at \"{}\": {}", url, e);
exit(1);
}
}
}
fn main() {
let figment = Figment::from(rocket::Config::default())
.merge(Toml::file(Env::var_or("NOMILO_CONFIG", "nomilo.toml")).nested())
.merge(Env::prefixed("NOMILO_").ignore(&["PROFILE"]).global())
.select(Profile::from_env_or("NOMILO_PROFILE", "release"));
let app_config = match figment.extract::<config::Config>() {
Ok(c) => c,
Err(e) => {
eprintln!("Error loading configuration: {}", e);
exit(1);
}
};
let nomilo = NomiloCli::parse();
nomilo.run(figment, app_config);
}