2023-08-29 21:04:47 +00:00
|
|
|
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;
|
2023-08-29 21:04:47 +00:00
|
|
|
mod cors;
|
2023-08-25 16:52:03 +00:00
|
|
|
mod model;
|
2023-09-21 21:06:31 +00:00
|
|
|
mod online_classroom;
|
2023-08-25 16:52:03 +00:00
|
|
|
|
2023-08-29 21:04:47 +00:00
|
|
|
static DB: OnceCell<Pool<MySql>> = OnceCell::new();
|
2023-08-25 16:52:03 +00:00
|
|
|
|
2023-08-29 21:04:47 +00:00
|
|
|
pub fn db() -> &'static Pool<MySql> {
|
|
|
|
DB.get().expect("DB not initialized")
|
2023-08-25 16:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[launch]
|
2023-08-29 21:04:47 +00:00
|
|
|
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![
|
2023-08-25 22:31:46 +00:00
|
|
|
controller::person::get_by_dni,
|
2023-08-31 03:34:27 +00:00
|
|
|
controller::person::options,
|
|
|
|
controller::person::create_person,
|
2023-08-25 22:31:46 +00:00
|
|
|
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-29 21:04:47 +00:00
|
|
|
],
|
|
|
|
)
|
2023-08-25 16:52:03 +00:00
|
|
|
}
|