refactor: 优化代码结构与界面文字描述
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
- 规范化导入语句顺序,提升代码整洁度 - 统一使用双引号并优化代码缩进格式,提升代码风格一致性 - 更新 HTML 模板中的文字标签,将“容器主机名”改为“容器ID”并简化“学号”描述 - 优化了路由函数的代码排列和 JSON 数据的返回格式
This commit is contained in:
38
app.py
38
app.py
@@ -1,11 +1,13 @@
|
||||
from flask import Flask, jsonify
|
||||
import socket
|
||||
import os
|
||||
import socket
|
||||
from datetime import datetime
|
||||
|
||||
from flask import Flask, jsonify
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
|
||||
@app.route("/")
|
||||
def hello():
|
||||
return f"""
|
||||
<!DOCTYPE html>
|
||||
@@ -22,9 +24,9 @@ def hello():
|
||||
<body>
|
||||
<h1>🐳 Docker Swarm 服务演示</h1>
|
||||
<div class="info">
|
||||
<p>📦 容器主机名: <span class="highlight">{socket.gethostname()}</span></p>
|
||||
<p>🎓 学号服务: <span class="highlight">s{os.getenv('STUDENT_ID', '00')}</span></p>
|
||||
<p>🕒 访问时间: <span class="highlight">{datetime.now().strftime('%H:%M:%S')}</span></p>
|
||||
<p>📦 容器ID: <span class="highlight">{socket.gethostname()}</span></p>
|
||||
<p>🎓 学号: <span class="highlight">s{os.getenv("STUDENT_ID", "00")}</span></p>
|
||||
<p>🕒 访问时间: <span class="highlight">{datetime.now().strftime("%H:%M:%S")}</span></p>
|
||||
</div>
|
||||
<p style="margin-top: 20px; color: #666;">
|
||||
💡 提示:多次刷新页面,观察容器主机名的变化
|
||||
@@ -33,18 +35,22 @@ def hello():
|
||||
</html>
|
||||
"""
|
||||
|
||||
@app.route('/api')
|
||||
|
||||
@app.route("/api")
|
||||
def api():
|
||||
return jsonify({
|
||||
'hostname': socket.gethostname(),
|
||||
'student_id': os.getenv('STUDENT_ID', '00'),
|
||||
'timestamp': datetime.now().isoformat()
|
||||
})
|
||||
return jsonify(
|
||||
{
|
||||
"hostname": socket.gethostname(),
|
||||
"student_id": os.getenv("STUDENT_ID", "00"),
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
}
|
||||
)
|
||||
|
||||
@app.route('/health')
|
||||
|
||||
@app.route("/health")
|
||||
def health():
|
||||
return {'status': 'healthy'}, 200
|
||||
return {"status": "healthy"}, 200
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=80)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=80)
|
||||
|
||||
Reference in New Issue
Block a user