Skip to content

Commit 53fc159

Browse files
chore: add fallback
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent 4efaefc commit 53fc159

4 files changed

Lines changed: 24 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tracing-subscriber={version="0.3", features=["env-filter"]}
4747
ahash="0.8"
4848

4949
# web
50-
axum="0.8"
50+
axum={version="0.8", features=["macros"]}
5151
axum-extra={version="0.12", default-features=false, features=["cookie", "typed-header"]}
5252
http="1.4"
5353
headers="0.4"

src/web/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ use tower_http::{
1919
};
2020

2121
use crate::app::{Liwan, models::Event};
22+
use crate::web::webext::serve;
2223

2324
pub use session::{MaybeExtract, SessionId, SessionUser};
2425
use webext::StaticFile;
2526

2627
#[derive(RustEmbed, Clone)]
2728
#[folder = "./web/dist"]
28-
struct _Files;
29+
pub struct Files;
2930

3031
#[derive(RustEmbed, Clone)]
3132
#[folder = "./tracker"]
@@ -82,13 +83,12 @@ pub async fn start_webserver(app: Arc<Liwan>, events: Sender<Event>) -> Result<(
8283
.nest("/api", routes::event::router().layer(event_cors))
8384
.nest("/api/dashboard", dashboard)
8485
.route_service("/script.js", StaticFile::<Script>::new("script.min.js").layer(script_cors).into_service())
86+
.fallback(axum::routing::get(serve))
8587
.layer(CompressionLayer::new())
8688
.layer(set_headers)
8789
.with_state(RouterState { app: app.clone(), events })
8890
.finish_api(&mut api);
8991

90-
// todo: serve files with webext::call
91-
9292
match app.onboarding.token()? {
9393
Some(onboarding) => {
9494
let get_started = format!("{}/setup?t={}", app.config.base_url, onboarding);

src/web/webext.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use std::task::{Context, Poll};
88

99
use aide::OperationOutput;
1010
use aide::axum::IntoApiResponse;
11-
use axum::body::{Body, Bytes};
11+
use axum::body::Body;
12+
use axum::extract::Request;
1213
use axum::response::IntoResponse;
1314
use axum::{Json, extract};
14-
use http::{Request, Response, StatusCode, header};
15+
use http::{Response, StatusCode, header};
1516
use rust_embed::RustEmbed;
1617
use schemars::JsonSchema;
1718
use serde::Serialize;
@@ -90,10 +91,7 @@ impl IntoResponse for ApiError {
9091
}
9192
}
9293

93-
pub async fn call<E: RustEmbed + Send + Sync>(
94-
orig_uri: extract::OriginalUri,
95-
req: Request<Bytes>,
96-
) -> Result<impl IntoResponse, StatusCode> {
94+
pub(super) async fn serve(orig_uri: extract::OriginalUri, req: Request) -> Result<impl IntoResponse, StatusCode> {
9795
let mut path = req.uri().path().trim_start_matches('/').trim_end_matches('/').to_string();
9896
if path.is_empty() {
9997
path = "index.html".to_string();
@@ -109,11 +107,11 @@ pub async fn call<E: RustEmbed + Send + Sync>(
109107
path = parts.join("/");
110108
}
111109

112-
let file = if let Some(content) = E::get(&path) {
110+
let file = if let Some(content) = Files::get(&path) {
113111
Some(content)
114112
} else {
115113
path = format!("{path}/index.html");
116-
E::get(&path)
114+
Files::get(&path)
117115
};
118116

119117
let orig_path = orig_uri.path();
@@ -209,3 +207,5 @@ macro_rules! http_bail {
209207
}
210208

211209
pub(crate) use http_bail;
210+
211+
use crate::web::Files;

0 commit comments

Comments
 (0)