chore: sync local changes (2026-03-12)

This commit is contained in:
2026-03-12 18:58:36 +08:00
parent 2bd4f06188
commit d8c1e1ee6c
49 changed files with 409 additions and 182 deletions

View File

@@ -1,48 +0,0 @@
FROM python:3.11-slim AS base
# Install Node.js 20 + nginx for building frontend and serving
RUN apt-get update \
&& apt-get install -y curl gnupg nginx \
&& curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y nodejs build-essential \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Copy source code
COPY mengyadriftbottle-backend ./mengyadriftbottle-backend
COPY mengyadriftbottle-frontend ./mengyadriftbottle-frontend
# -------- Build frontend --------
WORKDIR /app/mengyadriftbottle-frontend
RUN npm install \
&& npm run build
# -------- Install backend deps --------
WORKDIR /app/mengyadriftbottle-backend
RUN pip install --no-cache-dir -r requirements.txt \
&& pip install --no-cache-dir gunicorn
# Prepare runtime artifacts
WORKDIR /app
RUN mkdir -p frontend-dist \
&& cp -r /app/mengyadriftbottle-frontend/dist/* /app/frontend-dist/
# Seed data directory (can be overridden via volume)
RUN mkdir -p /app/data \
&& cp /app/mengyadriftbottle-backend/*.json /app/data/
# Copy nginx config and startup script
COPY nginx.conf /etc/nginx/nginx.conf
COPY start.sh /app/start.sh
RUN chmod +x /app/start.sh
EXPOSE 6767
ENV PORT=6767 \
BACKEND_PORT=5002 \
DRIFT_BOTTLE_FRONTEND_DIST=/app/frontend-dist \
DRIFT_BOTTLE_DATA_DIR=/app/data
CMD ["/app/start.sh"]

9
build_frontend.bat Normal file
View File

@@ -0,0 +1,9 @@
@echo off
chcp 65001 >nul
echo 构建前端项目...
cd mengyadriftbottle-frontend
if not exist node_modules (
echo 正在安装依赖...
call npm install
)
npm run build

View File

@@ -1,35 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
IMAGE_NAME=${IMAGE_NAME:-mengyadriftbottle}
CONTAINER_NAME=${CONTAINER_NAME:-mengyadriftbottle}
FRONTEND_PORT=${FRONTEND_PORT:-6767}
BACKEND_PORT=${BACKEND_PORT:-5002}
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
DATA_DIR="${DATA_DIR:-}"
echo "[1/3] Building image $IMAGE_NAME ..."
docker build -t "$IMAGE_NAME" "$PROJECT_DIR"
echo "[2/3] Removing old container if it exists ..."
if docker ps -a --format '{{.Names}}' | grep -Eq "^${CONTAINER_NAME}$"; then
docker rm -f "$CONTAINER_NAME" >/dev/null
fi
echo "[3/3] Starting container $CONTAINER_NAME ..."
RUN_ARGS=(
-d
--name "$CONTAINER_NAME"
-p ${FRONTEND_PORT}:6767
-e BACKEND_PORT=5002
)
if [ -n "$DATA_DIR" ]; then
mkdir -p "$DATA_DIR"
RUN_ARGS+=( -v "$DATA_DIR:/app/data" )
fi
docker run "${RUN_ARGS[@]}" "$IMAGE_NAME"
echo "Container is up. Frontend: http://localhost:${FRONTEND_PORT}"
echo "Logs: docker logs -f ${CONTAINER_NAME}"

View File

@@ -1,14 +0,0 @@
version: "3.9"
services:
mengyadriftbottle:
build: .
container_name: mengyadriftbottle
restart: unless-stopped
ports:
- "6767:6767"
environment:
BACKEND_PORT: "5002"
DRIFT_BOTTLE_DATA_DIR: "/app/data"
volumes:
- /shumengya/docker/storage/mengyadriftbottle:/app/data

View File

@@ -0,0 +1,18 @@
__pycache__
*.pyc
*.pyo
*.pyd
.Python
*.so
*.egg
*.egg-info
dist
build
.venv
venv
.env
.env.local
.git
.gitignore
README.md
*.md

View File

@@ -0,0 +1,23 @@
FROM python:3.11-slim
WORKDIR /app
# 复制依赖文件
COPY requirements.txt .
# 安装依赖
RUN pip install --no-cache-dir -r requirements.txt
# 复制应用代码
COPY . .
# 暴露端口
EXPOSE 4343
# 设置环境变量
ENV DRIFT_BOTTLE_DATA_DIR=/data
ENV FLASK_APP=app.py
ENV PORT=4343
# 运行应用
CMD ["python", "app.py"]

View File

@@ -42,6 +42,7 @@ PROJECT_ROOT = BACKEND_ROOT.parent # Root project directory
FRONTEND_ROOT = PROJECT_ROOT / "mengyadriftbottle-frontend" # Frontend directory
TEMPLATES_DIR = FRONTEND_ROOT / "templates" # Templates in frontend
STATIC_DIR = FRONTEND_ROOT / "static" # Static files in frontend
BACKGROUND_DIR = BACKEND_ROOT / "background" # Background images directory
FRONTEND_DIST = Path(
os.environ.get("DRIFT_BOTTLE_FRONTEND_DIST") or PROJECT_ROOT / "frontend-dist"
)
@@ -547,7 +548,20 @@ def admin_settings():
# ---------------------------------------------------------------------- #
# ==============================后端公开API============================== #
# ==============================静态文件服务============================== #
# ---------------------------------------------------------------------- #
@app.route("/background/<path:filename>")
def serve_background_image(filename: str):
"""Serve background images from the background directory."""
if not BACKGROUND_DIR.exists():
abort(404)
return send_from_directory(str(BACKGROUND_DIR), filename)
# ---------------------------------------------------------------------- #
# ==============================前端应用服务============================== #
# ---------------------------------------------------------------------- #
@@ -559,6 +573,10 @@ def serve_frontend_app(path: str):
if path.startswith("api/"):
abort(404)
# 如果路径是 background/,由上面的路由处理
if path.startswith("background/"):
abort(404)
if not FRONTEND_DIST.exists():
return ("Frontend build not found. Please run npm run build first.", 404)
@@ -569,4 +587,6 @@ def serve_frontend_app(path: str):
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5002, debug=True)
port = int(os.environ.get("PORT", 5002))
debug = os.environ.get("FLASK_DEBUG", "False").lower() == "true"
app.run(host="0.0.0.0", port=port, debug=debug)

View File

Before

Width:  |  Height:  |  Size: 18 MiB

After

Width:  |  Height:  |  Size: 18 MiB

View File

Before

Width:  |  Height:  |  Size: 4.3 MiB

After

Width:  |  Height:  |  Size: 4.3 MiB

View File

Before

Width:  |  Height:  |  Size: 1.7 MiB

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

Before

Width:  |  Height:  |  Size: 480 KiB

After

Width:  |  Height:  |  Size: 480 KiB

View File

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

View File

Before

Width:  |  Height:  |  Size: 920 KiB

After

Width:  |  Height:  |  Size: 920 KiB

View File

Before

Width:  |  Height:  |  Size: 3.1 MiB

After

Width:  |  Height:  |  Size: 3.1 MiB

View File

Before

Width:  |  Height:  |  Size: 3.6 MiB

After

Width:  |  Height:  |  Size: 3.6 MiB

View File

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 2.4 MiB

View File

Before

Width:  |  Height:  |  Size: 265 KiB

After

Width:  |  Height:  |  Size: 265 KiB

View File

Before

Width:  |  Height:  |  Size: 9.0 MiB

After

Width:  |  Height:  |  Size: 9.0 MiB

View File

Before

Width:  |  Height:  |  Size: 4.2 MiB

After

Width:  |  Height:  |  Size: 4.2 MiB

View File

Before

Width:  |  Height:  |  Size: 8.4 MiB

After

Width:  |  Height:  |  Size: 8.4 MiB

View File

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

Before

Width:  |  Height:  |  Size: 5.5 MiB

After

Width:  |  Height:  |  Size: 5.5 MiB

View File

Before

Width:  |  Height:  |  Size: 5.7 MiB

After

Width:  |  Height:  |  Size: 5.7 MiB

View File

Before

Width:  |  Height:  |  Size: 2.5 MiB

After

Width:  |  Height:  |  Size: 2.5 MiB

View File

Before

Width:  |  Height:  |  Size: 5.2 MiB

After

Width:  |  Height:  |  Size: 5.2 MiB

View File

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 2.4 MiB

View File

Before

Width:  |  Height:  |  Size: 5.1 MiB

After

Width:  |  Height:  |  Size: 5.1 MiB

View File

Before

Width:  |  Height:  |  Size: 4.8 MiB

After

Width:  |  Height:  |  Size: 4.8 MiB

View File

Before

Width:  |  Height:  |  Size: 9.6 MiB

After

Width:  |  Height:  |  Size: 9.6 MiB

View File

Before

Width:  |  Height:  |  Size: 2.9 MiB

After

Width:  |  Height:  |  Size: 2.9 MiB

View File

Before

Width:  |  Height:  |  Size: 3.4 MiB

After

Width:  |  Height:  |  Size: 3.4 MiB

View File

Before

Width:  |  Height:  |  Size: 3.0 MiB

After

Width:  |  Height:  |  Size: 3.0 MiB

View File

@@ -0,0 +1,22 @@
version: '3.8'
services:
backend:
build: .
container_name: mengyadriftbottle-backend
ports:
- "3737:4343"
volumes:
- /shumengya/docker/mengyadriftbottle-backend/data:/data
- ./background:/app/background:ro
environment:
- DRIFT_BOTTLE_DATA_DIR=/data
- DRIFT_BOTTLE_SECRET=${DRIFT_BOTTLE_SECRET:-}
- DRIFT_BOTTLE_ADMIN_TOKEN=${DRIFT_BOTTLE_ADMIN_TOKEN:-shumengya520}
restart: unless-stopped
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:4343/api/health')"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s

View File

@@ -0,0 +1,28 @@
# 环境变量配置说明
## 开发环境
创建 `.env.development` 文件(或使用默认值):
```
VITE_API_BASE_URL=/api
VITE_ADMIN_URL=http://localhost:5002/admin/login
```
## 生产环境
创建 `.env.production` 文件:
```
VITE_API_BASE_URL=https://bottle.api.shumengya.top/api
VITE_ADMIN_URL=https://bottle.api.shumengya.top/admin/login
```
## 说明
- `VITE_API_BASE_URL`: 后端 API 的基础 URL
- `VITE_ADMIN_URL`: 管理员登录页面的 URL
前端会自动根据 `VITE_API_BASE_URL` 来设置背景图片的路径:
- 如果 `VITE_API_BASE_URL` 是完整的 URL`https://bottle.api.shumengya.top/api`),背景图片会从该域名加载
- 如果 `VITE_API_BASE_URL` 是相对路径(如 `/api`),背景图片会使用相对路径

View File

@@ -8,7 +8,12 @@
name="description"
content="让心意随海浪飘向远方,邂逅那个懂你的人——萌芽漂流瓶 React 前端"
/>
<meta name="theme-color" content="#0ea5e9" />
<link rel="manifest" href="/manifest.webmanifest" />
<link rel="icon" type="image/png" href="/logo.png" />
<link rel="apple-touch-icon" href="/logo.png" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
</head>
<body>
<div id="root"></div>

View File

@@ -0,0 +1,25 @@
{
"name": "萌芽漂流瓶",
"short_name": "漂流瓶",
"description": "让心意随海浪飘向远方,邂逅那个懂你的人——萌芽漂流瓶",
"lang": "zh-CN",
"start_url": "/",
"scope": "/",
"display": "standalone",
"background_color": "#0b1020",
"theme_color": "#0ea5e9",
"icons": [
{
"src": "/logo.png",
"sizes": "2048x2048",
"type": "image/png",
"purpose": "any"
},
{
"src": "/logo3.png",
"sizes": "2048x2048",
"type": "image/png",
"purpose": "maskable"
}
]
}

View File

@@ -0,0 +1,97 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#0ea5e9" />
<title>离线 - 萌芽漂流瓶</title>
<style>
:root {
color-scheme: light dark;
--bg: #0b1020;
--fg: #e5e7eb;
--muted: rgba(229, 231, 235, 0.7);
--accent: #0ea5e9;
}
body {
margin: 0;
min-height: 100vh;
display: grid;
place-items: center;
background: radial-gradient(
1200px 800px at 20% 10%,
rgba(14, 165, 233, 0.2),
transparent 60%
),
radial-gradient(
900px 700px at 80% 90%,
rgba(99, 102, 241, 0.18),
transparent 55%
),
var(--bg);
color: var(--fg);
font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto,
Helvetica, Arial, 'Apple Color Emoji', 'Segoe UI Emoji';
}
.card {
width: min(520px, calc(100vw - 32px));
padding: 20px 18px;
border-radius: 16px;
background: rgba(255, 255, 255, 0.06);
border: 1px solid rgba(255, 255, 255, 0.12);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(10px);
}
h1 {
margin: 0 0 8px;
font-size: 18px;
font-weight: 700;
letter-spacing: 0.2px;
}
p {
margin: 0;
line-height: 1.6;
color: var(--muted);
font-size: 14px;
}
.actions {
margin-top: 14px;
display: flex;
gap: 10px;
flex-wrap: wrap;
}
a,
button {
appearance: none;
border: 0;
border-radius: 999px;
padding: 10px 14px;
font-size: 14px;
cursor: pointer;
color: #001018;
background: linear-gradient(180deg, #22c3ff, #0ea5e9);
font-weight: 700;
text-decoration: none;
}
button.secondary {
color: var(--fg);
background: rgba(255, 255, 255, 0.08);
border: 1px solid rgba(255, 255, 255, 0.14);
font-weight: 600;
}
</style>
</head>
<body>
<main class="card">
<h1>当前处于离线状态</h1>
<p>网络连接不可用,已为你保留基础页面。恢复网络后可继续使用完整功能。</p>
<div class="actions">
<a href="/">返回首页</a>
<button class="secondary" type="button" onclick="location.reload()">
重新加载
</button>
</div>
</main>
</body>
</html>

View File

@@ -0,0 +1,102 @@
const CACHE_PREFIX = 'mengyadriftbottle'
const CACHE_VERSION = 'v1'
const STATIC_CACHE = `${CACHE_PREFIX}-static-${CACHE_VERSION}`
const RUNTIME_CACHE = `${CACHE_PREFIX}-runtime-${CACHE_VERSION}`
const PRECACHE_URLS = [
'/',
'/index.html',
'/offline.html',
'/manifest.webmanifest',
'/logo.png',
'/logo3.png',
]
self.addEventListener('install', (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(STATIC_CACHE)
await cache.addAll(PRECACHE_URLS)
self.skipWaiting()
})(),
)
})
self.addEventListener('activate', (event) => {
event.waitUntil(
(async () => {
const keys = await caches.keys()
await Promise.all(
keys.map((key) => {
if (
key.startsWith(`${CACHE_PREFIX}-`) &&
key !== STATIC_CACHE &&
key !== RUNTIME_CACHE
) {
return caches.delete(key)
}
return undefined
}),
)
await self.clients.claim()
})(),
)
})
self.addEventListener('message', (event) => {
if (event?.data?.type === 'SKIP_WAITING') {
self.skipWaiting()
}
})
function isSameOrigin(url) {
return url.origin === self.location.origin
}
self.addEventListener('fetch', (event) => {
const { request } = event
if (request.method !== 'GET') return
const url = new URL(request.url)
if (!isSameOrigin(url)) return
if (url.pathname.startsWith('/api')) return
if (request.mode === 'navigate') {
event.respondWith(
(async () => {
try {
const networkResponse = await fetch(request)
const cache = await caches.open(RUNTIME_CACHE)
cache.put('/index.html', networkResponse.clone())
return networkResponse
} catch {
const cache = await caches.open(RUNTIME_CACHE)
const cached =
(await cache.match('/index.html')) ||
(await caches.match('/index.html')) ||
(await caches.match('/offline.html'))
return cached || Response.error()
}
})(),
)
return
}
const isAsset = ['script', 'style', 'image', 'font'].includes(request.destination)
if (isAsset) {
event.respondWith(
(async () => {
const cached = await caches.match(request)
if (cached) return cached
const response = await fetch(request)
const cache = await caches.open(RUNTIME_CACHE)
cache.put(request, response.clone())
return response
})(),
)
return
}
})

View File

@@ -11,7 +11,28 @@ const DEFAULT_FORM = Object.freeze({
qq: '',
})
const BACKGROUND_IMAGES = Array.from({ length: 29 }, (_, index) => `/background/image${index + 1}.png`)
// 根据环境变量确定背景图片的基础路径
// 如果设置了 VITE_API_BASE_URL则使用该 URL 的基础路径;否则使用相对路径
const getBackgroundBaseUrl = () => {
const apiUrl = import.meta.env.VITE_API_BASE_URL
if (apiUrl && apiUrl.startsWith('http')) {
// 生产环境:从完整 API URL 中提取基础 URL
try {
const url = new URL(apiUrl)
return `${url.protocol}//${url.host}`
} catch {
return ''
}
}
// 开发环境:使用相对路径
return ''
}
const BACKGROUND_BASE_URL = getBackgroundBaseUrl()
const BACKGROUND_IMAGES = Array.from(
{ length: 29 },
(_, index) => `${BACKGROUND_BASE_URL}/background/image${index + 1}.png`
)
const formatCount = (value) => {
const num = typeof value === 'number' ? value : Number(value || 0)

View File

@@ -8,3 +8,26 @@ createRoot(document.getElementById('root')).render(
<App />
</StrictMode>,
)
if (import.meta.env.PROD && 'serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker
.register('/sw.js', { scope: '/', updateViaCache: 'none' })
.then((registration) => {
registration.update().catch(() => {})
registration.addEventListener('updatefound', () => {
const worker = registration.installing
if (!worker) return
worker.addEventListener('statechange', () => {
if (worker.state !== 'installed') return
if (navigator.serviceWorker.controller) {
console.info('发现新版本,请刷新页面以更新。')
} else {
console.info('PWA 已启用,可离线使用。')
}
})
})
})
.catch((err) => console.warn('Service Worker 注册失败:', err))
})
}

View File

@@ -1,54 +0,0 @@
worker_processes auto;
error_log /dev/stderr warn;
pid /tmp/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout main;
sendfile on;
keepalive_timeout 65;
client_max_body_size 10M;
# Temp directories for nginx when running as non-root
client_body_temp_path /tmp/client_body;
proxy_temp_path /tmp/proxy;
fastcgi_temp_path /tmp/fastcgi;
uwsgi_temp_path /tmp/uwsgi;
scgi_temp_path /tmp/scgi;
upstream backend {
server 127.0.0.1:5002;
}
server {
listen 6767;
server_name _;
root /app/frontend-dist;
index index.html;
# Frontend static files
location / {
try_files $uri $uri/ /index.html;
}
# Proxy API requests to backend
location /api/ {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}

View File

@@ -1,28 +0,0 @@
#!/bin/bash
set -e
# ensure data dir exists (mounted volume expected)
DATA_DIR=${DRIFT_BOTTLE_DATA_DIR:-/app/data}
mkdir -p "$DATA_DIR"
for json_file in bottles.json filter_words.json mottos.json config.json; do
if [ -f "/app/mengyadriftbottle-backend/${json_file}" ] && [ ! -f "$DATA_DIR/${json_file}" ]; then
cp "/app/mengyadriftbottle-backend/${json_file}" "$DATA_DIR/${json_file}"
fi
done
# start backend on localhost:5002 (not exposed outside container)
BACKEND_PORT=${BACKEND_PORT:-5002}
echo "Starting backend API on localhost:${BACKEND_PORT}"
cd /app/mengyadriftbottle-backend
gunicorn -b 127.0.0.1:${BACKEND_PORT} app:app &
BACKEND_PID=$!
# start nginx to serve frontend + proxy /api to backend
echo "Starting nginx on port 6767 (proxying /api to backend)"
nginx -g 'daemon off;' &
NGINX_PID=$!
trap "kill $BACKEND_PID $NGINX_PID" TERM INT
wait -n $BACKEND_PID $NGINX_PID

6
start_backend.bat Normal file
View File

@@ -0,0 +1,6 @@
@echo off
chcp 65001 >nul
echo 启动后端服务器...
cd mengyadriftbottle-backend
go mod tidy
go run main.go

7
start_frontend.bat Normal file
View File

@@ -0,0 +1,7 @@
@echo off
chcp 65001 >nul
echo 启动前端开发服务器...
cd mengyadriftbottle-frontend
echo 正在安装依赖...
call npm install
npm run dev

0
萌芽漂流瓶 Normal file
View File