Skip to content

Commit 8c62c81

Browse files
committed
feat(static): 实现静态资源版本化和模板全局变量支持
- 在Dockerfile中添加默认环境变量配置 - 新增静态资源URL版本化管理功能 - 更新所有模板文件使用static_url函数替代硬编码路径 - 优化错误日志页面移动端按钮布局和响应式设计 - 简化异常处理器返回格式 BREAKING CHANGE: 静态资源URL格式变更,需要重新部署以确保资源正确加载
1 parent 05762cb commit 8c62c81

File tree

8 files changed

+320
-19
lines changed

8 files changed

+320
-19
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ COPY ./VERSION /app
88

99
RUN pip install --no-cache-dir -r requirements.txt
1010
COPY ./app /app/app
11+
ENV API_KEYS='["your_api_key_1"]'
12+
ENV ALLOWED_TOKENS='["your_token_1"]'
13+
ENV TZ='Asia/Shanghai'
1114

1215
# Expose port
1316
EXPOSE 8000

app/exception/exceptions.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ async def general_exception_handler(request: Request, exc: Exception):
130130
"""处理通用异常"""
131131
logger.exception(f"Unhandled Exception: {str(exc)}")
132132
return JSONResponse(
133-
status_code=exc.args[0],
134-
content={
135-
"error": {
136-
"code": exc.args[0],
137-
"message": exc.args[1],
138-
}
139-
},
133+
status_code=500,
134+
content=str(exc),
140135
)

app/router/routes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
)
2525
from app.service.key.key_manager import get_key_manager_instance
2626
from app.service.stats.stats_service import StatsService
27+
from app.utils.static_version import get_static_url
2728

2829
logger = get_routes_logger()
2930

3031
templates = Jinja2Templates(directory="app/templates")
32+
# 设置模板全局变量
33+
templates.env.globals["static_url"] = get_static_url
3134

3235

3336
def setup_routers(app: FastAPI) -> None:

app/templates/auth.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
</div>
5252

5353
<h2 class="text-3xl font-extrabold text-center text-gray-800 mb-8 animate-slide-down">
54-
<img src="/static/icons/logo.png" alt="Gemini Balance Logo" class="h-9 inline-block align-middle mr-2">
54+
<img src="{{ static_url('icons/logo.png') }}" alt="Gemini Balance Logo" class="h-9 inline-block align-middle mr-2">
5555
Gemini Balance
5656
</h2>
5757

app/templates/base.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>{% block title %}Gemini Balance{% endblock %}</title>
7-
<link rel="manifest" href="/static/manifest.json" />
7+
<link rel="manifest" href="{{ static_url('manifest.json') }}" />
88
<meta name="theme-color" content="#4F46E5" />
99
<meta name="apple-mobile-web-app-capable" content="yes" />
1010
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
1111
<meta name="apple-mobile-web-app-title" content="GBalance" />
12-
<link rel="icon" href="/static/icons/icon-192x192.png" />
12+
<link rel="icon" href="{{ static_url('icons/icon-192x192.png') }}" />
1313
<link
14-
href="/static/css/fonts.css"
14+
href="{{ static_url('css/fonts.css') }}"
1515
rel="stylesheet"
1616
/>
1717
<link
1818
rel="stylesheet"
1919
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
2020
/>
21-
<script src="/static/js/tailwindcss.js"></script>
21+
<script src="{{ static_url('js/tailwindcss.js') }}"></script>
2222
<script>
2323
tailwind.config = {
2424
theme: {

app/templates/config_editor.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@
809809

810810
<h1 class="text-3xl font-extrabold text-center text-gray-800 mb-4">
811811
<img
812-
src="/static/icons/logo.png"
812+
src="{{ static_url('icons/logo.png') }}"
813813
alt="Gemini Balance Logo"
814814
class="h-9 inline-block align-middle mr-2"
815815
/>
@@ -2870,7 +2870,7 @@ <h2 id="modelHelperTitle" class="text-xl font-bold text-gray-800">
28702870
</div>
28712871

28722872
{% endblock %} {% block body_scripts %}
2873-
<script src="/static/js/config_editor.js"></script>
2873+
<script src="{{ static_url('js/config_editor.js') }}"></script>
28742874
<!-- 增强下拉框样式和交互性 -->
28752875
<script>
28762876
document.addEventListener("DOMContentLoaded", function () {

app/templates/error_logs.html

Lines changed: 178 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,179 @@
4545
.search-container {
4646
grid-template-columns: 1fr;
4747
}
48+
49+
/* 移动端主容器布局 */
50+
.mobile-buttons-container {
51+
display: flex !important;
52+
flex-direction: column !important;
53+
gap: 1rem !important;
54+
align-items: stretch !important;
55+
width: 100% !important;
56+
padding: 0 !important;
57+
margin: 0 !important;
58+
}
59+
60+
/* 移动端搜索控件布局优化 */
61+
.mobile-search-controls {
62+
grid-template-columns: 1fr !important;
63+
gap: 0.75rem !important;
64+
width: 100% !important;
65+
margin-bottom: 0.5rem !important;
66+
}
67+
68+
/* 按钮容器在移动端的布局 */
69+
.buttons-container-responsive {
70+
display: flex !important;
71+
flex-direction: column !important;
72+
gap: 0.5rem !important;
73+
width: 100% !important;
74+
align-items: stretch !important;
75+
justify-content: stretch !important;
76+
}
77+
78+
/* 移动端所有按钮样式 */
79+
.buttons-container-responsive button {
80+
width: 100% !important;
81+
max-width: 100% !important;
82+
justify-content: center !important;
83+
text-align: center !important;
84+
min-width: 0 !important;
85+
flex-shrink: 0 !important;
86+
box-sizing: border-box !important;
87+
padding: 0.5rem 1rem !important;
88+
font-size: 0.875rem !important;
89+
white-space: nowrap !important;
90+
overflow: hidden !important;
91+
text-overflow: ellipsis !important;
92+
}
93+
}
94+
95+
/* 中等屏幕优化 */
96+
@media (max-width: 1024px) and (min-width: 769px) {
97+
.buttons-container-responsive {
98+
flex-wrap: wrap !important;
99+
justify-content: center !important;
100+
}
101+
102+
.buttons-container-responsive button {
103+
flex-shrink: 1 !important;
104+
min-width: 0 !important;
105+
padding-left: 0.75rem !important;
106+
padding-right: 0.75rem !important;
107+
}
108+
}
109+
110+
/* 小屏幕(手机)特殊优化 - 确保按钮在边框内 */
111+
@media (max-width: 640px) {
112+
/* 强制重写主容器布局 */
113+
.mobile-buttons-container {
114+
display: flex !important;
115+
flex-direction: column !important;
116+
width: 100% !important;
117+
padding: 0 !important;
118+
margin: 0 !important;
119+
gap: 1rem !important;
120+
overflow: visible !important;
121+
}
122+
123+
/* 搜索区域在移动端占满宽度 */
124+
.mobile-search-controls {
125+
width: 100% !important;
126+
box-sizing: border-box !important;
127+
}
128+
129+
/* 按钮区域完全重新布局 */
130+
.buttons-container-responsive {
131+
display: flex !important;
132+
flex-direction: column !important;
133+
width: 100% !important;
134+
max-width: 100% !important;
135+
gap: 0.5rem !important;
136+
padding: 0 !important;
137+
margin: 0 !important;
138+
box-sizing: border-box !important;
139+
overflow: visible !important;
140+
}
141+
142+
/* 所有按钮统一样式 */
143+
.buttons-container-responsive button {
144+
display: flex !important;
145+
align-items: center !important;
146+
justify-content: center !important;
147+
width: 100% !important;
148+
max-width: 100% !important;
149+
box-sizing: border-box !important;
150+
padding: 0.5rem 1rem !important;
151+
margin: 0 !important;
152+
font-size: 0.875rem !important;
153+
line-height: 1.25rem !important;
154+
border-radius: 0.5rem !important;
155+
white-space: nowrap !important;
156+
overflow: hidden !important;
157+
text-overflow: ellipsis !important;
158+
flex-shrink: 0 !important;
159+
}
160+
161+
/* 特别针对清空全部按钮 */
162+
#deleteAllLogsBtn {
163+
background-color: #f87171 !important;
164+
border: 1px solid #f87171 !important;
165+
}
166+
167+
#deleteAllLogsBtn:hover {
168+
background-color: #ef4444 !important;
169+
border: 1px solid #ef4444 !important;
170+
}
171+
172+
/* 确保容器不会溢出父级 */
173+
.mobile-buttons-container,
174+
.mobile-buttons-container > *,
175+
.buttons-container-responsive,
176+
.buttons-container-responsive > * {
177+
max-width: 100% !important;
178+
box-sizing: border-box !important;
179+
}
180+
181+
/* 额外的安全边距控制 */
182+
.mobile-buttons-container .grid {
183+
padding-left: 0 !important;
184+
padding-right: 0 !important;
185+
margin-left: 0 !important;
186+
margin-right: 0 !important;
187+
}
188+
189+
/* 确保主内容区域有适当的内边距 */
190+
.rounded-xl.p-6 {
191+
padding-left: 1rem !important;
192+
padding-right: 1rem !important;
193+
}
194+
}
195+
196+
/* 超小屏幕额外优化 */
197+
@media (max-width: 480px) {
198+
.mobile-buttons-container {
199+
gap: 0.75rem !important;
200+
}
201+
202+
.buttons-container-responsive {
203+
gap: 0.4rem !important;
204+
}
205+
206+
.buttons-container-responsive button {
207+
padding: 0.4rem 0.8rem !important;
208+
font-size: 0.8rem !important;
209+
}
210+
211+
/* 主容器内边距进一步缩小 */
212+
.rounded-xl.p-6 {
213+
padding-left: 0.75rem !important;
214+
padding-right: 0.75rem !important;
215+
}
216+
217+
/* 确保清空全部按钮文字不会太挤 */
218+
#deleteAllLogsBtn i {
219+
margin-right: 0.25rem !important;
220+
}
48221
}
49222

50223
input[type="text"],
@@ -586,7 +759,7 @@
586759
class="text-3xl font-extrabold text-center text-gray-800 mb-4"
587760
>
588761
<img
589-
src="/static/icons/logo.png"
762+
src="{{ static_url('icons/logo.png') }}"
590763
alt="Gemini Balance Logo"
591764
class="h-9 inline-block align-middle mr-2"
592765
/>
@@ -636,10 +809,10 @@
636809

637810
<!-- 搜索与操作控件 -->
638811
<div
639-
class="grid grid-cols-1 lg:grid-cols-[1fr_auto] items-center gap-4 mb-6"
812+
class="grid grid-cols-1 lg:grid-cols-[1fr_auto] items-center gap-4 mb-6 mobile-buttons-container"
640813
>
641814
<div
642-
class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 w-full"
815+
class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 w-full mobile-search-controls"
643816
>
644817
<input
645818
type="text"
@@ -684,7 +857,7 @@
684857
</div>
685858
</div>
686859
</div>
687-
<div class="flex items-center gap-3 flex-shrink-0">
860+
<div class="flex items-center gap-3 flex-shrink-0 buttons-container-responsive">
688861
<button
689862
id="searchBtn"
690863
class="flex items-center justify-center px-4 py-1.5 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition-all duration-200 shadow-sm hover:shadow-md whitespace-nowrap"
@@ -1041,7 +1214,7 @@ <h2 class="text-lg font-semibold text-gray-800">确认删除</h2>
10411214
</div>
10421215
</div>
10431216
{% endblock %} {% block body_scripts %}
1044-
<script src="/static/js/error_logs.js"></script>
1217+
<script src="{{ static_url('js/error_logs.js') }}"></script>
10451218
<script>
10461219
// error_logs.html specific JS initialization (if any)
10471220
// e.g., initialize date pickers or other elements if needed

0 commit comments

Comments
 (0)