Alisa/crates/backend/src/main.rs
2024-07-09 10:00:31 +02:00

57 lines
1.4 KiB
Rust

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