mod api; mod auth; mod database; mod extractors; use actix_web::{web, App, HttpServer}; use dotenv::dotenv; use std::env; use std::sync::Arc; #[derive(Clone)] struct AppState { database: Arc, secret: String, } #[actix_web::main] async fn main() -> std::io::Result<()> { // Errors can get very tough, // the rust log saved my ass std::env::set_var("RUST_LOG", "debug"); env_logger::init(); dotenv().ok(); /* create database wrapper (reference: acsim) */ let db_raw = match database::DatabaseWrapper::new().await { Ok(res) => res, Err(_) => panic!("Error creating database wrapper"), }; let db = Arc::new(db_raw); /* get jwt secret from env */ let jwt_secret = env::var("SECRET") .expect("environment variable SECRET is *probably not setted up!!") .to_string(); /* application data struct */ let app_state = AppState { database: db, secret: jwt_secret, }; /* main server setup */ HttpServer::new(move || { App::new() .app_data(web::Data::new(app_state.clone())) .route("/", web::get().to(root)) .service(api::api_scope()) .service(auth::auth_scope()) }) .bind(("127.0.0.1", 8000))? .run() .await } /* main page*/ async fn root() -> String { String::from("Server is up and running") }