You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fast-insiders/server/src/lib.rs

85 lines
1.9 KiB

#![feature(proc_macro_hygiene, decl_macro)]
// Macros
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate lazy_static;
extern crate pretty_env_logger;
#[macro_use]
extern crate log;
// External crates
use rocket::{Build, Rocket};
use sea_orm_rocket::rocket::fairing::{self, AdHoc};
use sea_orm_rocket::Database;
// Local crates
use migration::MigratorTrait;
mod amf;
mod cors;
mod db;
mod env;
mod logger;
mod model;
mod repo;
mod route;
mod task;
use crate::task::run_tasks;
// Module imports
use crate::db::Db;
use env::Config;
lazy_static! {
/// Contains variables defind in .env file
static ref CONFIG: Config = Config::new();
}
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
let conn = &Db::fetch(&rocket).unwrap().conn;
let _ = migration::Migrator::up(conn, None).await;
Ok(rocket)
}
async fn start_rocket() -> Result<(), sea_orm_rocket::rocket::Error> {
rocket::build()
.attach(Db::init())
.attach(AdHoc::try_on_ignite("Migrations", run_migrations))
.attach(crate::cors::Cors)
.mount(
"/v1",
routes![
route::company::get_all,
route::company::get_by_isin,
route::transaction::get_transactions,
route::transaction::get_aggregated_transactions,
route::in_process_transaction::get_all,
route::in_process_transaction::retry_failed_transaction,
route::in_process_transaction::retry_all
],
)
.launch()
.await
.map(|_| ())
}
#[rocket::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
logger::init_log()?;
// Run tasks
tokio::task::spawn(async { run_tasks().await });
let result = start_rocket().await;
info!("Rocket: deorbit.");
if let Some(err) = result.err() {
println!("Error: {}", err);
}
Ok(())
}