ars_server/
server.rs

1use anyhow::Context;
2use tracing::info;
3use super::context::App;
4
5use axum::{
6    extract::{Path, State},
7    response::IntoResponse,
8    routing::get,
9    Router,
10};
11
12pub async fn route(State(app): State<App>, Path(asset): Path<String>) -> impl IntoResponse {
13    if !asset.contains('.') {
14        return app
15            .store()
16            .index()
17            .into_response();
18    }
19    app
20        .store()
21        .get(&asset)
22        .into_response()
23}
24
25pub async fn keycloak_config(State(app): State<App>) -> impl IntoResponse {
26    app
27        .store()
28        .keycloak_config()
29        .into_response()
30}
31
32pub async fn index(State(app): State<App>) -> impl IntoResponse {
33    app
34        .store()
35        .index()
36        .into_response()
37}
38
39pub async fn serve(app: App) -> anyhow::Result<()> {
40    let addr = app.cfg().address().parse().unwrap();
41    let router = Router::new()
42        .route("/.well-known/config.json", get(keycloak_config))
43        .route("/*asset", get(route))
44        .fallback(get(index))
45        .with_state(app);
46    info!("Serve on http://{addr:?}/");
47    axum::Server::bind(&addr)
48        .serve(router.into_make_service())
49        .await
50        .context("error while starting server")?;
51    Ok(())
52}