eeg_certs/backend/src/main.rs

56 lines
1.4 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;
mod online_classroom;
2023-08-25 16:52:03 +00:00
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,
2023-08-31 03:34:27 +00:00
controller::person::options,
controller::person::create_person,
controller::course::get_all,
2023-08-28 22:28:58 +00:00
controller::register::insert_all,
controller::register::options,
2023-09-01 18:01:00 +00:00
controller::register::options_delete,
2023-08-28 22:28:58 +00:00
controller::register::get_by_dni,
2023-09-01 18:01:00 +00:00
controller::register::delete,
2023-09-03 00:42:43 +00:00
controller::custom_label::get_all,
],
)
2023-08-25 16:52:03 +00:00
}