Add backend

This commit is contained in:
Jon Staab
2026-02-25 13:11:25 -08:00
parent d2ade19763
commit 42abde9dcd
11 changed files with 1104 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
mod api;
mod auth;
mod config;
mod db;
mod models;
mod repo;
use std::net::SocketAddr;
use anyhow::Result;
use axum::{routing::get, Router};
use tokio::net::TcpListener;
use tower_http::cors::CorsLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use crate::config::Config;
use crate::db::init_pool;
use crate::repo::Repo;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(tracing_subscriber::fmt::layer())
.init();
let config = Config::from_env();
ensure_sqlite_dir(&config.database_url)?;
let pool = init_pool(&config.database_url).await?;
let repo = Repo::new(pool);
let state = api::AppState {
repo,
admin_pubkeys: std::sync::Arc::new(config.admin_pubkeys.clone()),
};
let app = Router::new()
.merge(api::router(state))
.route("/healthz", get(healthz))
.layer(CorsLayer::permissive());
let addr: SocketAddr = format!("{}:{}", config.host, config.port).parse()?;
let listener = TcpListener::bind(addr).await?;
tracing::info!("listening on {}", addr);
axum::serve(listener, app).await?;
Ok(())
}
fn ensure_sqlite_dir(database_url: &str) -> Result<()> {
if let Some(path) = database_url.strip_prefix("sqlite://") {
if let Some(dir) = std::path::Path::new(path).parent() {
if !dir.as_os_str().is_empty() {
std::fs::create_dir_all(dir)?;
}
}
}
Ok(())
}
async fn healthz() -> &'static str {
"ok"
}