summaryrefslogblamecommitdiff
path: root/dichroism/src/main.rs
blob: 86de76fffe9e318b087042a22b3de492323cfd6d (plain) (tree)
1
2
3
4
5
6
7
8
9






                                                           

           
           
 






























                                                                                                 
 
use anyhow::Context;
use axum::{http::StatusCode, routing::get_service, Router};
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::params;
use std::io::Error;
use tower_http::services::ServeDir;

mod config;
mod result;

#[tokio::main]
async fn main() -> result::Result<()> {
    // read config from environment
    let config = config::Config::from_env()?;

    // start db connection pool
    let manager = SqliteConnectionManager::file(&config.db_path);
    let pool = Pool::new(manager).with_context(|| "Failed to create database connection pool.")?;

    // migrate/init db if empty
    pool.get()?
        .execute(include_str!("./migrations/photo_sets.sql"), params![])?;
    pool.get()?
        .execute(include_str!("./migrations/products.sql"), params![])?;

    let app = Router::new().nest(
        "/static",
        get_service(ServeDir::new("/zroot/gl/images")).handle_error(|error: Error| async move {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Unhandled internal error: {}", error),
            )
        }),
    );

    axum::Server::bind(&config.bind_addr)
        .serve(app.into_make_service())
        .await
        .with_context(|| "Server error!")?;

    Ok(())
}