AkurAI Build
Menu

Public repository / feb7ac86b5e60d55b429eacb7146cf0dc32c6628

AkurAI-Drive / backend/src/main.rs

Latest change feb03c4c2e9a6b0e869a63e52329c938e68dc8e8 - backend: device-token auth, SigV4 presigning, and file API for the sync client by olibuijr

pub mod api;
pub mod auth;
pub mod config;
pub mod db;
pub mod device;
pub mod idp;
pub mod pages;
pub mod session;
pub mod sigv4;
pub mod util;

use axum::{
    Json, Router,
    http::StatusCode,
    response::IntoResponse,
    routing::{get, post},
};
use serde_json::json;

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
        )
        .init();

    tracing::info!("akurai-drive-api starting up");

    // Trigger DB initialization (opens the connection, sets pragmas, creates tables).
    db::with_db(|_conn| {});

    let cfg = config::get();

    let app = Router::new()
        .route("/healthz", get(healthz))
        .route("/auth/login", get(auth::login))
        .route("/auth/callback", get(auth::callback))
        .route("/auth/logout", get(auth::logout))
        .route("/auth/device", get(auth::device))
        .route("/api/me", get(api::me))
        .route(
            "/api/storage",
            get(api::storage_list).post(api::storage_create),
        )
        .route("/api/buckets", get(api::buckets))
        .route("/api/files", get(api::files_list).delete(api::files_delete))
        .route("/api/files/presign", post(api::files_presign))
        .route("/app", get(pages::app))
        .fallback(not_found);

    tracing::info!("listening on {}", cfg.listen_addr);

    let listener = tokio::net::TcpListener::bind(&cfg.listen_addr)
        .await
        .expect("failed to bind listener");

    axum::serve(listener, app).await.expect("server error");
}

async fn healthz() -> impl IntoResponse {
    Json(json!({"ok": true, "service": "akurai-drive-api"}))
}

async fn not_found() -> impl IntoResponse {
    (StatusCode::NOT_FOUND, Json(json!({"error": "not found"})))
}