skantravels/rust-backend/src/main.rs

69 lines
1.8 KiB
Rust
Raw Normal View History

mod auth;
use actix_web::{get, Responder, HttpResponse, HttpServer, App, web};
use actix_web::middleware;
use std::sync::{Mutex, Arc};
use crate::auth::AuthorizedUser;
2024-08-13 12:57:14 +01:00
mod embedded {
use refinery::embed_migrations;
embed_migrations!("./migrations");
}
struct AppData {
database: Arc<Mutex<rusqlite::Connection>>,
}
2024-08-13 12:57:14 +01:00
#[get("/hc")]
async fn healthcheck(data: web::Data<AppData>) -> impl Responder {
let db = data.database.lock().unwrap();
match db.pragma_query(None, "integrity_check", |_| Ok(())) {
Ok(_) => HttpResponse::Ok().body("OK"),
Err(_) => HttpResponse::InternalServerError().body("Error"),
}
2024-08-13 12:57:14 +01:00
}
#[get("/authorized")]
async fn authorized(_: AuthorizedUser) -> impl Responder {
HttpResponse::Ok().body("Authorized")
}
2024-08-13 12:57:14 +01:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let port = std::env::var("RUST_BACKEND_PORT")
.ok()
.and_then(|port| port.parse::<u16>().ok())
.unwrap_or(8080);
let database_path = std::env::var("RUST_BACKEND_DB")
.unwrap_or("database.sqlite3".to_string());
println!("Opening database: {}", database_path);
let mut conn = rusqlite::Connection::open(database_path.clone()).unwrap();
embedded::migrations::runner().run(&mut conn).unwrap();
println!("Starting web server at port {}", port);
2024-08-13 12:57:14 +01:00
HttpServer::new(|| {
let database_path = std::env::var("RUST_BACKEND_DB")
.unwrap_or("database.sqlite3".to_string());
let conn = Arc::new(Mutex::new(rusqlite::Connection::open(database_path).unwrap()));
2024-08-13 12:57:14 +01:00
App::new()
.app_data(web::Data::new(AppData {
database: conn,
}))
2024-08-13 12:57:14 +01:00
.service(healthcheck)
.service(authorized)
2024-08-13 12:57:14 +01:00
})
.bind(("0.0.0.0", port))?
2024-08-13 12:57:14 +01:00
.run()
.await
}