summaryrefslogtreecommitdiff
path: root/dichroism/src/main.rs
blob: 86de76fffe9e318b087042a22b3de492323cfd6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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(())
}