Alisa/crates/backend/src/main.rs

47 lines
1.1 KiB
Rust

use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use sea_orm::{Database, DatabaseConnection};
use std::env;
use routes::config;
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("DATABASE_URL must be set");
let conn = Database::connect(&db_url)
.await
.expect("Connecting to Database failed");
let state = AppState { db: conn };
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
}
async fn index() -> impl Responder {
HttpResponse::Ok().body("API Test Response")
}