2024-08-14 15:33:38 +01:00
|
|
|
mod auth;
|
2024-08-15 10:02:40 +01:00
|
|
|
mod models;
|
2024-08-14 15:33:38 +01:00
|
|
|
|
2024-08-13 14:12:21 +01:00
|
|
|
use actix_web::{get, Responder, HttpResponse, HttpServer, App, web};
|
2024-08-15 10:02:40 +01:00
|
|
|
use std::sync::{Mutex, MutexGuard, Arc};
|
|
|
|
use auth::AuthorizedUser;
|
|
|
|
use models::Favorite;
|
2024-08-13 12:57:14 +01:00
|
|
|
|
2024-08-13 13:37:13 +01:00
|
|
|
mod embedded {
|
|
|
|
use refinery::embed_migrations;
|
|
|
|
embed_migrations!("./migrations");
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:12:21 +01:00
|
|
|
struct AppData {
|
|
|
|
database: Arc<Mutex<rusqlite::Connection>>,
|
|
|
|
}
|
|
|
|
|
2024-08-13 12:57:14 +01:00
|
|
|
#[get("/hc")]
|
2024-08-13 14:12:21 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-14 15:33:38 +01:00
|
|
|
#[get("/authorized")]
|
2024-08-14 16:53:29 +01:00
|
|
|
async fn authorized(auth: AuthorizedUser) -> impl Responder {
|
|
|
|
HttpResponse::Ok().body(format!("Authorized as {} ({})", auth.username, auth.user_id))
|
2024-08-14 15:33:38 +01:00
|
|
|
}
|
|
|
|
|
2024-08-15 10:02:40 +01:00
|
|
|
#[get("/favorites")]
|
|
|
|
async fn favorites(auth: AuthorizedUser, data: web::Data<AppData>) -> impl Responder {
|
|
|
|
let db = data.database.lock().unwrap();
|
|
|
|
|
|
|
|
match get_favorites(db, auth.user_id) {
|
|
|
|
Some(favorites) => HttpResponse::Ok().json(favorites),
|
|
|
|
None => HttpResponse::InternalServerError().finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_favorites(db: MutexGuard<'_, rusqlite::Connection>, user_id: String) -> Option<Vec<Favorite>> {
|
|
|
|
Some(
|
|
|
|
db.prepare("SELECT * FROM favorites WHERE user_id = :user_id").ok()?
|
|
|
|
.query_map(&[(":user_id", &user_id)], |row| Favorite::from_row(row))
|
|
|
|
.ok()?
|
|
|
|
.map(|fav| fav.unwrap())
|
|
|
|
.collect()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-08-13 12:57:14 +01:00
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2024-08-15 07:36:55 +01:00
|
|
|
let _ = dotenvy::dotenv();
|
|
|
|
|
2024-08-13 12:57:14 +01:00
|
|
|
let port = std::env::var("RUST_BACKEND_PORT")
|
|
|
|
.ok()
|
|
|
|
.and_then(|port| port.parse::<u16>().ok())
|
|
|
|
.unwrap_or(8080);
|
|
|
|
|
2024-08-13 13:37:13 +01:00
|
|
|
let database_path = std::env::var("RUST_BACKEND_DB")
|
|
|
|
.unwrap_or("database.sqlite3".to_string());
|
|
|
|
|
|
|
|
println!("Opening database: {}", database_path);
|
|
|
|
|
2024-08-13 14:12:21 +01:00
|
|
|
let mut conn = rusqlite::Connection::open(database_path.clone()).unwrap();
|
2024-08-13 13:37:13 +01:00
|
|
|
|
|
|
|
embedded::migrations::runner().run(&mut conn).unwrap();
|
|
|
|
|
|
|
|
println!("Starting web server at port {}", port);
|
|
|
|
|
2024-08-15 09:10:21 +01:00
|
|
|
let conn = Arc::new(Mutex::new(rusqlite::Connection::open(database_path).unwrap()));
|
2024-08-13 14:12:21 +01:00
|
|
|
|
2024-08-15 09:10:21 +01:00
|
|
|
HttpServer::new(move || {
|
2024-08-13 12:57:14 +01:00
|
|
|
App::new()
|
2024-08-13 14:12:21 +01:00
|
|
|
.app_data(web::Data::new(AppData {
|
2024-08-15 09:10:21 +01:00
|
|
|
database: conn.clone(),
|
2024-08-13 14:12:21 +01:00
|
|
|
}))
|
2024-08-13 12:57:14 +01:00
|
|
|
.service(healthcheck)
|
2024-08-14 15:33:38 +01:00
|
|
|
.service(authorized)
|
2024-08-15 10:02:40 +01:00
|
|
|
.service(favorites)
|
2024-08-13 12:57:14 +01:00
|
|
|
})
|
2024-08-13 13:37:13 +01:00
|
|
|
.bind(("0.0.0.0", port))?
|
2024-08-13 12:57:14 +01:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|
2024-08-14 15:33:38 +01:00
|
|
|
|