Game sync and live services for independent game developers (targeting itch.io)
1use poem::http::StatusCode;
2use poem::{Endpoint, Request, Response, Result};
3use rust_embed::Embed;
4
5#[derive(Embed)]
6#[folder = "../landing/build"]
7struct Assets;
8
9pub struct StaticAssets;
10
11impl StaticAssets {
12 pub fn new() -> Self {
13 Self
14 }
15}
16
17impl Endpoint for StaticAssets {
18 type Output = Response;
19
20 async fn call(&self, req: Request) -> Result<Self::Output> {
21 let path = req.uri().path().trim_start_matches('/');
22
23 if let Some(file) = Assets::get(path) {
24 let mime = mime_guess::from_path(path)
25 .first_or_octet_stream()
26 .to_string();
27
28 Ok(Response::builder()
29 .header("Content-Type", &mime)
30 .header("Cache-Control", "public, max-age=31536000, immutable")
31 .body(file.data.into_owned()))
32 } else if let Some(file) = Assets::get("index.html") {
33 Ok(Response::builder()
34 .header("Content-Type", "text/html")
35 .body(file.data.into_owned()))
36 } else {
37 Ok(Response::builder()
38 .status(StatusCode::NOT_FOUND)
39 .body(Vec::new()))
40 }
41 }
42}