eeg_certs/backend/src/main.rs

50 lines
1.1 KiB
Rust
Raw Normal View History

use cors::Cors;
use once_cell::sync::OnceCell;
use sqlx::mysql::MySqlPoolOptions;
use sqlx::{MySql, Pool};
use std::env;
2023-08-25 16:52:03 +00:00
#[macro_use]
extern crate rocket;
mod controller;
mod cors;
2023-08-25 16:52:03 +00:00
mod model;
static DB: OnceCell<Pool<MySql>> = OnceCell::new();
2023-08-25 16:52:03 +00:00
pub fn db() -> &'static Pool<MySql> {
DB.get().expect("DB not initialized")
2023-08-25 16:52:03 +00:00
}
#[launch]
async fn rocket() -> _ {
dotenvy::dotenv().expect("Failed to load .env file");
/*
Init DB
*/
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
let pool = MySqlPoolOptions::new()
.max_connections(5)
.connect(db_url.as_str())
.await;
match pool {
Ok(pool) => DB.set(pool).expect("Failed to set DB pool"),
Err(e) => println!("Error connecting to DB: {}", e),
}
/* Init Rocket */
rocket::build().attach(Cors {}).mount(
"/api",
routes![
controller::person::get_by_dni,
controller::course::get_all,
2023-08-28 22:28:58 +00:00
controller::register::insert_all,
controller::register::options,
controller::register::get_by_dni,
],
)
2023-08-25 16:52:03 +00:00
}