minccino/src/main.rs
2022-09-23 22:39:17 -07:00

79 lines
2.1 KiB
Rust

#[macro_use]
extern crate rocket;
mod entity;
#[macro_use]
mod macros;
mod db;
mod llv6;
mod meta;
#[cfg(test)]
mod test;
mod types;
mod yui;
use db::Instances;
use migration::{Migrator, MigratorTrait};
use rocket::figment::providers::{Env, Format, Toml};
use rocket::Build;
use rocket::Rocket;
use rocket_db_pools::Database;
fn rocket() -> Rocket<Build> {
let rocket = rocket::build()
.attach(Instances::init())
.mount("/", meta::routes::all())
.mount("/_yui/", yui::routes::all());
#[cfg(test)]
let figment = {
std::env::set_var(
"MINCCINO_TEST_DATABASES",
"{instances={url=\"sqlite::memory:\"}}",
);
rocket::figment::Figment::from(rocket::Config::default())
.merge(Toml::file("minccino.toml").nested())
.merge(Env::prefixed("MINCCINO_TEST_"))
};
#[cfg(not(test))]
let figment = {
rocket::figment::Figment::from(rocket::Config::default())
.merge(Toml::file("minccino.toml").nested())
.merge(Env::prefixed("MINCCINO_"))
};
rocket
.configure(figment)
.attach(rocket::fairing::AdHoc::on_liftoff(
"Apply migrations, if needed",
|rocket| {
Box::pin(async move {
if let Some(db) = Instances::fetch(rocket) {
Migrator::up(&db.conn, None).await.unwrap();
}
})
},
))
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
let arg = std::env::args().nth(1);
match arg {
Some(x) => match x.as_str() {
"genkey" => {
let (key, hash) = yui::auth::genkey();
println!("\n# API Key: {}\n", key);
println!(
"# Put the following in your Rocket.toml:\n[minccino]\napi_hash = \"{}\"",
hash
);
}
_ => println!("not implemented"),
},
None => {
let _rocket = rocket().ignite().await?.launch().await?;
}
}
Ok(())
}