-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (93 loc) · 3.38 KB
/
Copy pathapp.py
File metadata and controls
110 lines (93 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
from datetime import datetime
from flask import (Flask, redirect, render_template, request,
send_from_directory, url_for, jsonify)
app = Flask(__name__)
# In-memory state
visitor_count = 0
guestbook = []
app_start_time = datetime.now()
@app.route('/')
def index():
global visitor_count
visitor_count += 1
print('Request for index page received')
return render_template('index.html', visit_count=visitor_count, server_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/hello', methods=['POST'])
def hello():
name = request.form.get('name')
if name:
print('Request for hello page received with name=%s' % name)
return render_template('hello.html', name=name,
greeting_time=datetime.now().strftime('%I:%M %p'))
else:
print('Request for hello page received with no name or blank name -- redirecting')
return redirect(url_for('index'))
@app.route('/guestbook')
def guestbook_page():
"""Guestbook page where visitors can leave messages."""
return render_template('guestbook.html', messages=guestbook)
@app.route('/guestbook/sign', methods=['POST'])
def sign_guestbook():
"""Add a message to the guestbook."""
name = request.form.get('name', 'Anonymous')
message = request.form.get('message', '')
if message.strip():
guestbook.append({
'name': name,
'message': message,
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
})
return redirect(url_for('guestbook_page'))
@app.route('/api/stats')
def stats():
"""API endpoint returning visitor stats as JSON."""
uptime = datetime.now() - app_start_time
return jsonify({
'visitor_count': visitor_count,
'guestbook_entries': len(guestbook),
'uptime_seconds': int(uptime.total_seconds()),
'server_time': datetime.now().isoformat(),
'status': 'healthy'
})
@app.route('/api/echo', methods=['POST'])
def echo():
"""Echo back posted JSON data with a timestamp."""
data = request.get_json(force=True)
return jsonify({
'echo': data,
'received_at': datetime.now().isoformat()
})
@app.route('/api/time')
def server_time():
"""Return current server time in multiple formats."""
now = datetime.now()
return jsonify({
'iso': now.isoformat(),
'readable': now.strftime('%B %d, %Y at %I:%M %p'),
'unix': int(now.timestamp())
})
@app.route('/api/health')
def health():
"""Health check endpoint."""
uptime = datetime.now() - app_start_time
return jsonify({
'status': 'ok',
'uptime': str(uptime).split('.')[0],
'started_at': app_start_time.isoformat()
})
@app.route('/dashboard')
def dashboard():
"""Dashboard page showing stats and server info."""
uptime = datetime.now() - app_start_time
return render_template('dashboard.html',
visit_count=visitor_count,
guestbook_count=len(guestbook),
uptime=str(uptime).split('.')[0],
server_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if __name__ == '__main__':
app.run(port=8080)