-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
92 lines (71 loc) · 3.3 KB
/
app.py
File metadata and controls
92 lines (71 loc) · 3.3 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
# app.py
from flask import Flask, request, render_template, jsonify, Response
import subprocess
from reservations import add_reservation_date, get_reservations
import time
from datetime import datetime, date
import os
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
result = ""
if request.method == "POST":
date_str = request.form.get("date") # YYYY-MM-DD 형식
time_str = request.form.get("time") # HH:MM 형식
try:
selected_datetime = datetime.strptime(f"{date_str} {time_str}", "%Y-%m-%d %H:%M")
now = datetime.now()
if selected_datetime < now:
result = "이미 지난 날짜와 시간은 예약할 수 없습니다."
return render_template("index.html", result=result, current_date=date.today().isoformat())
# 정시 단위 확인 (분이 00이 아니면 에러)
if selected_datetime.minute != 0:
result = "예약 시간은 정시 단위(예: 09:00, 10:00)로만 입력해주세요."
return render_template("index.html", result=result, current_date=date.today().isoformat())
except Exception as e:
result = f"날짜 또는 시간 형식이 잘못되었습니다: {str(e)}"
return render_template("index.html", result=result, current_date=date.today().isoformat())
# 예약 날짜와 예약 시간을 함께 기록
add_reservation_date(date_str, time_str)
# 스케줄러가 예약 실행을 담당하므로, 추가 스크립트 호출은 제거합니다.
result = f"{date_str} {time_str}부터 2시간 동안 예약이 등록되었습니다."
return render_template("index.html", result=result, current_date=date.today().isoformat())
@app.route("/logs")
def show_logs():
try:
with open("logs/reservation.log", "r", encoding="utf-8") as f:
log_lines = f.readlines()
except Exception as e:
log_lines = [f"로그 파일을 읽는 중 오류 발생: {e}"]
return render_template("logs.html", logs=log_lines)
@app.route('/stream')
def stream():
def event_stream():
log_file = "logs/reservation.log"
# 로그 파일이 없을 수 있으므로, 파일 존재 여부를 확인합니다.
if not os.path.exists(log_file):
open(log_file, "w").close() # 빈 파일 생성
with open(log_file, "r", encoding="utf-8") as f:
# 파일의 끝으로 이동하여 이후 추가되는 로그만 스트림합니다.
f.seek(0, os.SEEK_END)
while True:
line = f.readline()
if not line:
time.sleep(1)
continue
yield f"data: {line}\n\n"
return Response(event_stream(), mimetype="text/event-stream")
@app.route("/api/reservations")
def api_reservations():
reservations = get_reservations()
return jsonify(reservations=reservations)
@app.route("/calendar")
def calendar_view():
reservations = get_reservations()
return render_template("calendar.html", reservations=reservations)
@app.route("/dashboard")
def dashboard():
reservations = get_reservations()
return render_template("dashboard.html", reservations=reservations)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)