44 lines
1 KiB
Rust
44 lines
1 KiB
Rust
use std::env;
|
|
|
|
use actix_web::{web, App, HttpServer};
|
|
use sea_orm::{Database, DatabaseConnection};
|
|
|
|
mod controller;
|
|
mod routes;
|
|
|
|
#[derive(Clone)]
|
|
struct AppState {
|
|
db: DatabaseConnection,
|
|
}
|
|
|
|
#[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("Env DATABASE_URL must be set");
|
|
|
|
let conn = Database::connect(&db_url)
|
|
.await
|
|
.expect("Connecting to Database failed");
|
|
|
|
let state = AppState { db: conn };
|
|
|
|
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(routes::config)
|
|
})
|
|
.bind(("127.0.0.1", 8080))?
|
|
.run()
|
|
.await
|
|
}
|