nomilo/src/main.rs

66 lines
1.6 KiB
Rust
Raw Normal View History

2021-03-20 02:31:41 +00:00
#![feature(proc_macro_hygiene, decl_macro)]
2022-04-23 10:14:19 +00:00
2021-03-20 02:31:41 +00:00
#[macro_use] extern crate rocket;
2021-03-26 22:30:38 +00:00
#[macro_use] extern crate diesel;
2022-04-23 10:14:19 +00:00
#[macro_use] extern crate diesel_migrations;
2021-03-26 22:30:38 +00:00
2021-04-02 20:09:51 +00:00
mod routes;
2022-04-23 10:14:19 +00:00
mod cli;
2022-04-29 02:29:10 +00:00
mod config;
mod dns;
mod models;
mod schema;
mod template;
mod controllers;
2021-03-20 02:31:41 +00:00
2022-04-22 22:27:16 +00:00
use std::process::exit;
2022-04-23 10:14:19 +00:00
use clap::Parser;
2022-04-22 22:27:16 +00:00
use figment::{Figment, Profile, providers::{Format, Toml, Env}};
2022-04-22 23:34:04 +00:00
use rocket_sync_db_pools::database;
2022-04-23 10:14:19 +00:00
use diesel::prelude::*;
2021-03-20 18:18:08 +00:00
2022-04-23 10:14:19 +00:00
use crate::cli::{NomiloCli, NomiloCommand};
2022-04-22 22:27:16 +00:00
#[database("sqlite")]
2021-03-26 22:30:38 +00:00
pub struct DbConn(diesel::SqliteConnection);
2021-03-20 02:31:41 +00:00
2022-04-22 23:34:04 +00:00
2022-04-23 10:14:19 +00:00
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);
}
};
2022-04-22 22:27:16 +00:00
2022-04-23 10:14:19 +00:00
match diesel::SqliteConnection::establish(&url) {
Ok(c) => c,
Err(e) => {
eprintln!("Error connecting to database at \"{}\": {}", url, e);
exit(1);
}
}
2022-04-22 23:34:04 +00:00
}
fn main() {
2022-04-22 22:27:16 +00:00
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);
}
};
2021-03-20 02:31:41 +00:00
2022-04-23 10:14:19 +00:00
let nomilo = NomiloCli::parse();
nomilo.run(figment, app_config);
2021-03-20 02:31:41 +00:00
}