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
44
45
46
47
48
49
50
51
52
use anyhow::Context;
use tracing::info;
use super::context::App;

use axum::{
    extract::{Path, State},
    response::IntoResponse,
    routing::get,
    Router,
};

pub async fn route(State(app): State<App>, Path(asset): Path<String>) -> impl IntoResponse {
    if !asset.contains(".") {
        return app
            .store()
            .index()
            .into_response();
    }
    app
        .store()
        .get(&asset)
        .into_response()
}

pub async fn keycloak_config(State(app): State<App>) -> impl IntoResponse {
    app
        .store()
        .keycloak_config()
        .into_response()
}

pub async fn index(State(app): State<App>) -> impl IntoResponse {
    app
        .store()
        .index()
        .into_response()
}

pub async fn serve(app: App) -> anyhow::Result<()> {
    let addr = app.cfg().address().parse().unwrap();
    let router = Router::new()
        .route("/.well-known/config", get(keycloak_config))
        .route("/*asset", get(route))
        .fallback(get(index))
        .with_state(app);
    info!("Serve on http://{addr:?}/");
    axum::Server::bind(&addr)
        .serve(router.into_make_service())
        .await
        .context("error while starting server")?;
    Ok(())
}