Skip to content

Commit b0a6172

Browse files
committed
refactor(api) and cd
1 parent 243ade8 commit b0a6172

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

api/src/dist.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
use actix_files::NamedFile;
2-
use actix_web::{HttpRequest, Responder};
2+
use actix_web::{HttpRequest, HttpResponse};
3+
use std::path::{Path, PathBuf};
4+
use tokio::fs;
35

4-
pub async fn dist(req: HttpRequest) -> impl Responder {
5-
let path = req.path();
6-
NamedFile::open_async(format!("/explore/html/{}", path))
7-
.await
8-
.unwrap_or(NamedFile::open_async("/explore/html/index.html").await.unwrap())
9-
.prefer_utf8(true)
10-
.use_etag(true)
11-
.use_last_modified(true)
6+
pub async fn dist(req: HttpRequest) -> HttpResponse {
7+
let base = Path::new("/explore/html");
8+
let request_path = req.path().trim_matches('/');
9+
let file_path = resolve_file_path(base, request_path).await;
10+
match NamedFile::open_async(&file_path).await {
11+
Ok(named_file) => {
12+
named_file
13+
.prefer_utf8(true)
14+
.use_etag(true)
15+
.use_last_modified(true)
16+
.into_response(&req)
17+
}
18+
Err(_) => {
19+
NamedFile::open_async(base.join("index.html"))
20+
.await
21+
.map(|f| f.into_response(&req))
22+
.unwrap_or_else(|_|
23+
HttpResponse::NotFound()
24+
.content_type("text/html")
25+
.body("<h1>404 Not Found</h1>")
26+
)
27+
}
28+
}
29+
}
30+
31+
async fn resolve_file_path(base: &Path, request_path: &str) -> PathBuf {
32+
if request_path.is_empty() {
33+
return base.join("index.html");
34+
}
35+
let full_path = base.join(request_path);
36+
if let Ok(metadata) = fs::metadata(&full_path).await {
37+
if metadata.is_dir() {
38+
return full_path.join("index.html");
39+
}
40+
}
41+
full_path
1242
}

docker-compose.yaml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,32 @@ services:
1010
- redis_data:/data
1111
restart: unless-stopped
1212
networks:
13-
- jzfs
13+
- traefik_web
1414
jzfs-git:
1515
build: .
1616
image: gitdatateam/jzfs-git:latest
1717
container_name: jzfs-git
1818
ports:
1919
- "22:30322"
20-
- "80:80"
2120
volumes:
2221
- ./data:/explore/data
2322
- ./logs:/explore/logs
24-
entrypoint:
25-
- REDIS_URL=redis://jzfs_redis:6379
26-
- PORT=80
27-
- ROOT_DATA=/explore/data
2823
env_file:
2924
- .env
3025
restart: unless-stopped
3126
networks:
32-
- jzfs
27+
- traefik_web
3328
labels:
3429
- "traefik.enable=true"
3530
- "traefik.http.routers.jzfs-git.rule=Host(`git.atom.gitdata.ai`)"
3631
- "traefik.http.routers.jzfs-git.entrypoints=websecure"
3732
- "traefik.http.routers.jzfs-git.tls.certresolver=letsencrypt"
38-
- "traefik.http.middlewares.https-redirect.redirectscheme.scheme=https"
39-
- "traefik.http.middlewares.https-redirect.redirectscheme.permanent=true"
33+
- "traefik.docker.network=traefik_web"
4034
depends_on:
4135
- jzfs_deps_redis
4236

4337
networks:
44-
jzfs:
45-
driver: bridge
46-
38+
traefik_web:
39+
external: true
4740
volumes:
4841
redis_data:

0 commit comments

Comments
 (0)