eithney commited on
Commit
6871abf
·
verified ·
1 Parent(s): 9657b6d

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +6 -0
  2. README.md +11 -5
  3. app.py +43 -0
  4. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+ WORKDIR /app
3
+ COPY requirements.txt .
4
+ RUN pip install --no-cache-dir -r requirements.txt
5
+ COPY app.py .
6
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,16 @@
1
  ---
2
- title: Hfsbase
3
- emoji: 🐢
4
- colorFrom: green
5
- colorTo: indigo
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
1
  ---
2
+ title: Tunnel Addr
3
+ emoji: 🚇
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: docker
7
+ app_port: 7860
8
  pinned: false
9
  ---
10
 
11
+ 极简的 trycloudflare 地址读写服务(开发机用,无鉴权)。
12
+
13
+ - `GET /url` → 返回当前地址(纯文本,`Cache-Control: no-store`)
14
+ - `POST /url`,请求体为地址文本 → 写入
15
+
16
+ 本地 cloudflared 拿到新地址后 POST 到 `/url`;设备开机 GET `/url` 拿当前地址。
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Request
3
+ from fastapi.responses import PlainTextResponse, JSONResponse
4
+ import uvicorn
5
+
6
+ # HF Spaces 开启持久存储时 /data 可持久;否则退回容器本地目录(冷启动会丢,
7
+ # 靠本地 publisher 的心跳每 60s 重推自愈,开发机够用)。
8
+ try:
9
+ os.makedirs("/data", exist_ok=True)
10
+ STATE = "/data/current_url.txt"
11
+ except OSError:
12
+ STATE = os.path.join(os.getcwd(), "current_url.txt")
13
+
14
+ app = FastAPI()
15
+
16
+
17
+ def read() -> str:
18
+ try:
19
+ with open(STATE) as f:
20
+ return f.read().strip()
21
+ except FileNotFoundError:
22
+ return ""
23
+
24
+
25
+ @app.get("/")
26
+ @app.get("/url")
27
+ async def get_url():
28
+ # no-store:保证设备/客户端每次都拿到最新地址,不被 CDN/浏览器缓存坑到
29
+ return PlainTextResponse(read(), headers={"Cache-Control": "no-store"})
30
+
31
+
32
+ @app.post("/url")
33
+ async def set_url(request: Request):
34
+ body = (await request.body()).decode().strip()
35
+ if not body:
36
+ return JSONResponse({"error": "empty body"}, status_code=400)
37
+ with open(STATE, "w") as f:
38
+ f.write(body)
39
+ return JSONResponse({"ok": True, "url": body})
40
+
41
+
42
+ if __name__ == "__main__":
43
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn