2024-07-05 16:10:03 +02:00
|
|
|
use actix_web::{web, App, HttpServer};
|
|
|
|
use migration::MigratorTrait;
|
2024-07-05 15:16:27 +02:00
|
|
|
use sea_orm::{Database, DatabaseConnection};
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
use routes::config;
|
2024-07-05 21:24:32 +02:00
|
|
|
mod auth;
|
2024-07-05 15:16:27 +02:00
|
|
|
mod controller;
|
|
|
|
mod routes;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct AppState {
|
|
|
|
db: DatabaseConnection,
|
2024-07-08 12:55:11 +02:00
|
|
|
secret: String,
|
2024-07-05 15:16:27 +02:00
|
|
|
}
|
2024-07-05 11:44:57 +02:00
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
println!("Running debug build -> enabling permissive CORS");
|
|
|
|
|
2024-07-05 15:16:27 +02:00
|
|
|
dotenvy::dotenv().ok();
|
|
|
|
|
|
|
|
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
2024-07-05 21:24:32 +02:00
|
|
|
let jwt_secret = env::var("TOKEN_SECRET").expect("TOKEN_SECRET must be set");
|
2024-07-05 15:16:27 +02:00
|
|
|
|
|
|
|
let conn = Database::connect(&db_url)
|
|
|
|
.await
|
|
|
|
.expect("Connecting to Database failed");
|
|
|
|
|
2024-07-05 16:10:03 +02:00
|
|
|
println!("Running Migrations");
|
|
|
|
migration::Migrator::up(&conn, None)
|
|
|
|
.await
|
|
|
|
.expect("Running migrations failed");
|
|
|
|
println!("Finished running migrations");
|
|
|
|
|
2024-07-05 21:24:32 +02:00
|
|
|
let state = AppState {
|
|
|
|
db: conn,
|
2024-07-08 12:55:11 +02:00
|
|
|
secret: jwt_secret,
|
2024-07-05 21:24:32 +02:00
|
|
|
};
|
2024-07-05 15:16:27 +02:00
|
|
|
|
2024-07-05 16:10:03 +02:00
|
|
|
println!("Listening for connections...");
|
2024-07-05 11:44:57 +02:00
|
|
|
HttpServer::new(move || {
|
|
|
|
let cors = if cfg!(debug_assertions) {
|
|
|
|
actix_cors::Cors::permissive()
|
|
|
|
} else {
|
|
|
|
actix_cors::Cors::default()
|
|
|
|
};
|
2024-07-05 15:16:27 +02:00
|
|
|
App::new()
|
|
|
|
.wrap(cors)
|
|
|
|
.app_data(web::Data::new(state.clone()))
|
|
|
|
.configure(config)
|
2024-07-05 11:44:57 +02:00
|
|
|
})
|
|
|
|
.bind(("127.0.0.1", 8080))?
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|