Skip to content

Commit 7516cf8

Browse files
committed
fix serve_embedded_assets
1 parent 4c280c6 commit 7516cf8

4 files changed

Lines changed: 85 additions & 115 deletions

File tree

.github/workflows/rust.yml

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ on:
55
branches: [ "master" ]
66
pull_request:
77
branches: [ "master" ]
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: write
812

913
env:
1014
CARGO_TERM_COLOR: always
@@ -14,90 +18,75 @@ jobs:
1418
check:
1519
runs-on: ubuntu-latest
1620
steps:
17-
- uses: actions/checkout@v4
18-
19-
- uses: dtolnay/rust-toolchain@stable
20-
with:
21-
components: rustfmt, clippy
22-
23-
- uses: Swatinem/rust-cache@v2
24-
25-
- name: Check formatting
26-
run: cargo fmt --all -- --check
27-
28-
- name: Clippy
29-
run: cargo clippy --all-targets --all-features
21+
- uses: actions/checkout@v4
22+
- uses: dtolnay/rust-toolchain@stable
23+
with:
24+
components: rustfmt, clippy
25+
- uses: Swatinem/rust-cache@v2
26+
- name: Check formatting
27+
run: cargo fmt --all -- --check
28+
- name: Clippy
29+
run: cargo clippy --all-targets --all-features
3030

3131
test:
3232
runs-on: ${{ matrix.os }}
3333
strategy:
3434
matrix:
35-
os: [ubuntu-latest, windows-latest, macos-latest]
35+
os: [ ubuntu-latest, windows-latest ]
3636
steps:
37-
- uses: actions/checkout@v4
38-
39-
- uses: dtolnay/rust-toolchain@stable
40-
41-
- uses: Swatinem/rust-cache@v2
42-
43-
- name: Run tests
44-
run: cargo test --release
37+
- uses: actions/checkout@v4
38+
- uses: dtolnay/rust-toolchain@stable
39+
- uses: Swatinem/rust-cache@v2
40+
- name: Run tests
41+
run: cargo test --release
4542

4643
build:
47-
needs: [check, test]
44+
needs: [ check, test ]
4845
runs-on: ${{ matrix.os }}
4946
strategy:
5047
matrix:
5148
include:
49+
# --- Linux 64-bit ---
5250
- os: ubuntu-latest
5351
target: x86_64-unknown-linux-gnu
5452
artifact_name: rust_rest_api
5553
asset_name: rust_rest_api-linux-x86_64
54+
# --- Windows 64-bit ---
5655
- os: windows-latest
5756
target: x86_64-pc-windows-msvc
5857
artifact_name: rust_rest_api.exe
5958
asset_name: rust_rest_api-windows-x86_64.exe
60-
- os: macos-latest
61-
target: x86_64-apple-darwin
62-
artifact_name: rust_rest_api
63-
asset_name: rust_rest_api-macos-x86_64
64-
- os: macos-latest
65-
target: aarch64-apple-darwin
66-
artifact_name: rust_rest_api
67-
asset_name: rust_rest_api-macos-aarch64
6859
steps:
69-
- uses: actions/checkout@v4
70-
71-
- uses: dtolnay/rust-toolchain@stable
72-
with:
73-
targets: ${{ matrix.target }}
74-
75-
- uses: Swatinem/rust-cache@v2
76-
77-
- name: Build release binary
78-
run: cargo build --release --target ${{ matrix.target }}
79-
80-
- name: Upload artifact
81-
uses: actions/upload-artifact@v4
82-
with:
83-
name: ${{ matrix.asset_name }}
84-
path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}
60+
- uses: actions/checkout@v4
61+
- uses: dtolnay/rust-toolchain@stable
62+
with:
63+
targets: ${{ matrix.target }}
64+
- uses: Swatinem/rust-cache@v2
65+
- name: Build release binary
66+
run: cargo build --release --target ${{ matrix.target }}
67+
- name: Upload artifact
68+
uses: actions/upload-artifact@v4
69+
with:
70+
name: ${{ matrix.asset_name }}
71+
path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}
8572

8673
release:
87-
if: startsWith(github.ref, 'refs/tags/')
74+
if: github.ref == 'refs/heads/master'
8875
needs: build
8976
runs-on: ubuntu-latest
9077
steps:
91-
- uses: actions/checkout@v4
92-
93-
- name: Download all artifacts
94-
uses: actions/download-artifact@v4
95-
with:
96-
path: artifacts
97-
98-
- name: Create release
99-
uses: softprops/action-gh-release@v1
100-
with:
101-
files: artifacts/**/*
102-
env:
103-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
- name: Download all artifacts
79+
uses: actions/download-artifact@v4
80+
with:
81+
path: artifacts
82+
merge-multiple: false
83+
- name: Create draft release (nightly)
84+
uses: softprops/action-gh-release@v1
85+
with:
86+
draft: true
87+
tag_name: nightly-${{ github.run_number }}
88+
name: Nightly ${{ github.run_number }}
89+
generate_release_notes: true
90+
files: artifacts/**/*
91+
env:
92+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust_rest_api"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2024"
55
license = "MIT"
66
authors = ["Habibi-Dev"]
@@ -22,8 +22,8 @@ sea-orm = { version = "1.1.16", features = ["sqlx-sqlite", "runtime-tokio-rustls
2222
once_cell = "1.21.3"
2323
rust-embed = "8.7.2"
2424
askama = { version = "0.14", features = ["full"] }
25-
tower-http = { version = "0.6.6", features = ["fs"] }
2625
http = "1.3.1"
26+
mime_guess = "2.0.5"
2727

2828
[dependencies.migration]
2929
path = "./migration"

src/services/routes.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ use crate::middleware::visit_event;
22
use crate::services::index::index_handler;
33
use crate::services::{AppState, country, health, ip, time};
44
use axum::routing::{MethodRouter, get};
5-
use axum::{Router, middleware};
6-
use std::path::PathBuf;
7-
use tower_http::services::fs::ServeDir;
5+
use axum::{Router, middleware, body::Body};
6+
use axum::extract::Path;
7+
use axum::response::{IntoResponse, Response};
8+
use http::{header, StatusCode};
9+
use rust_embed::RustEmbed;
10+
11+
12+
#[derive(RustEmbed)]
13+
#[folder = "src/assets/"]
14+
struct Assets;
815

916
pub struct Routes;
1017
impl Routes {
1118
pub fn routes(app_state: AppState) -> Router {
12-
let assets_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/assets");
1319

1420
// routes that need state
1521
let stateful_routes = Router::new()
@@ -26,7 +32,7 @@ impl Routes {
2632
Router::new()
2733
.merge(stateful_routes)
2834
.merge(health::routers(app_state.clone()))
29-
.nest_service("/assets", ServeDir::new(assets_path))
35+
.route("/assets/{*path}", get(Self::serve_embedded_assets)) // Changed from /*path to /{*path}
3036
.nest("/api/v1", api)
3137
.route_layer(middleware::from_fn_with_state(
3238
app_state.clone(),
@@ -41,4 +47,24 @@ impl Routes {
4147
}
4248
app
4349
}
50+
51+
pub async fn serve_embedded_assets(Path(path): Path<String>) -> impl IntoResponse {
52+
let path = path.trim_start_matches('/');
53+
54+
match Assets::get(path) {
55+
Some(content) => {
56+
let mime = mime_guess::from_path(path).first_or_octet_stream();
57+
58+
Response::builder()
59+
.status(StatusCode::OK)
60+
.header(header::CONTENT_TYPE, mime.as_ref())
61+
.body(Body::from(content.data.to_vec()))
62+
.unwrap()
63+
}
64+
None => Response::builder()
65+
.status(StatusCode::NOT_FOUND)
66+
.body(Body::from("404 Not Found"))
67+
.unwrap(),
68+
}
69+
}
4470
}

0 commit comments

Comments
 (0)