feat: 添加Docker Swarm演示路由和API接口
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
40
app.py
40
app.py
@@ -1,16 +1,50 @@
|
||||
from flask import Flask
|
||||
from flask import Flask, jsonify
|
||||
import socket
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def hello():
|
||||
return f"""
|
||||
容器主机名: {socket.gethostname()}
|
||||
学号服务: s{os.getenv('STUDENT_ID', '00')}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Docker Swarm Demo</title>
|
||||
<meta charset="utf-8">
|
||||
<style>
|
||||
body {{ font-family: Arial; max-width: 600px; margin: 50px auto; padding: 20px; }}
|
||||
.info {{ background: #f0f0f0; padding: 15px; border-radius: 5px; }}
|
||||
.highlight {{ color: #0066cc; font-weight: bold; }}
|
||||
</style>
|
||||
</head>
|
||||
<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>
|
||||
</div>
|
||||
<p style="margin-top: 20px; color: #666;">
|
||||
💡 提示:多次刷新页面,观察容器主机名的变化
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
@app.route('/api')
|
||||
def api():
|
||||
return jsonify({
|
||||
'hostname': socket.gethostname(),
|
||||
'student_id': os.getenv('STUDENT_ID', '00'),
|
||||
'timestamp': datetime.now().isoformat()
|
||||
})
|
||||
|
||||
@app.route('/health')
|
||||
def health():
|
||||
return {'status': 'healthy'}, 200
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=80)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user