Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 50 additions & 61 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
workflow_dispatch:

permissions:
contents: write

env:
CARGO_TERM_COLOR: always
Expand All @@ -14,90 +18,75 @@ jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy

- uses: Swatinem/rust-cache@v2

- name: Check formatting
run: cargo fmt --all -- --check

- name: Clippy
run: cargo clippy --all-targets --all-features
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- name: Check formatting
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --all-targets --all-features

test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ ubuntu-latest, windows-latest ]
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable

- uses: Swatinem/rust-cache@v2

- name: Run tests
run: cargo test --release
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --release

build:
needs: [check, test]
needs: [ check, test ]
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
# --- Linux 64-bit ---
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
artifact_name: rust_rest_api
asset_name: rust_rest_api-linux-x86_64
# --- Windows 64-bit ---
- os: windows-latest
target: x86_64-pc-windows-msvc
artifact_name: rust_rest_api.exe
asset_name: rust_rest_api-windows-x86_64.exe
- os: macos-latest
target: x86_64-apple-darwin
artifact_name: rust_rest_api
asset_name: rust_rest_api-macos-x86_64
- os: macos-latest
target: aarch64-apple-darwin
artifact_name: rust_rest_api
asset_name: rust_rest_api-macos-aarch64
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}

- uses: Swatinem/rust-cache@v2

- name: Build release binary
run: cargo build --release --target ${{ matrix.target }}

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.asset_name }}
path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2
- name: Build release binary
run: cargo build --release --target ${{ matrix.target }}
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.asset_name }}
path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }}

release:
if: startsWith(github.ref, 'refs/tags/')
if: github.ref == 'refs/heads/master'
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts

- name: Create release
uses: softprops/action-gh-release@v1
with:
files: artifacts/**/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
merge-multiple: false
- name: Create draft release (nightly)
uses: softprops/action-gh-release@v1
with:
draft: true
tag_name: nightly-${{ github.run_number }}
name: Nightly ${{ github.run_number }}
generate_release_notes: true
files: artifacts/**/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49 changes: 2 additions & 47 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust_rest_api"
version = "0.2.0"
version = "0.2.1"
edition = "2024"
license = "MIT"
authors = ["Habibi-Dev"]
Expand All @@ -22,8 +22,8 @@ sea-orm = { version = "1.1.16", features = ["sqlx-sqlite", "runtime-tokio-rustls
once_cell = "1.21.3"
rust-embed = "8.7.2"
askama = { version = "0.14", features = ["full"] }
tower-http = { version = "0.6.6", features = ["fs"] }
http = "1.3.1"
mime_guess = "2.0.5"

[dependencies.migration]
path = "./migration"
36 changes: 31 additions & 5 deletions src/services/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ use crate::middleware::visit_event;
use crate::services::index::index_handler;
use crate::services::{AppState, country, health, ip, time};
use axum::routing::{MethodRouter, get};
use axum::{Router, middleware};
use std::path::PathBuf;
use tower_http::services::fs::ServeDir;
use axum::{Router, middleware, body::Body};
use axum::extract::Path;
use axum::response::{IntoResponse, Response};
use http::{header, StatusCode};
use rust_embed::RustEmbed;


#[derive(RustEmbed)]
#[folder = "src/assets/"]
struct Assets;

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

// routes that need state
let stateful_routes = Router::new()
Expand All @@ -26,7 +32,7 @@ impl Routes {
Router::new()
.merge(stateful_routes)
.merge(health::routers(app_state.clone()))
.nest_service("/assets", ServeDir::new(assets_path))
.route("/assets/{*path}", get(Self::serve_embedded_assets)) // Changed from /*path to /{*path}
.nest("/api/v1", api)
.route_layer(middleware::from_fn_with_state(
app_state.clone(),
Expand All @@ -41,4 +47,24 @@ impl Routes {
}
app
}

pub async fn serve_embedded_assets(Path(path): Path<String>) -> impl IntoResponse {
let path = path.trim_start_matches('/');

match Assets::get(path) {
Some(content) => {
let mime = mime_guess::from_path(path).first_or_octet_stream();

Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, mime.as_ref())
.body(Body::from(content.data.to_vec()))
.unwrap()
}
None => Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from("404 Not Found"))
.unwrap(),
}
}
}
Loading