-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.py
More file actions
122 lines (102 loc) ยท 4.06 KB
/
dev-server.py
File metadata and controls
122 lines (102 loc) ยท 4.06 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
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""
NUNCHI ยท ํ๋ก ํธ์๋ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์๋ฒ
=================================
Spring Boot ๋ฅผ ๋์ฐ์ง ์๊ณ ๋ธ๋ผ์ฐ์ ์์ ๋ฐ๋ก ํ์ธํ๊ธฐ ์ํ ์์ ์ ์ ์๋ฒ.
application-local.yml ์ ๋งคํ์ ๊ทธ๋๋ก ๋ชจ์ฌํ๋ค:
classpath:/static/front/ โ URL ๋ฃจํธ
classpath:/templates_front/ โ URL ๋ฃจํธ
์ฆ ๋ ํด๋๋ฅผ ํ๋์ ๊ฐ์ ๋ฃจํธ๋ก ํฉ์ณ์ ์๋นํ๋ค.
์คํ:
python3 dev-server.py # ๊ธฐ๋ณธ ํฌํธ 5500
python3 dev-server.py 8080 # ๋ค๋ฅธ ํฌํธ๋ก ๋์ฐ๋ ค๋ฉด ์ธ์๋ก ์ง์
์ ์ URL (๊ธฐ๋ณธ ํฌํธ 5500 ๊ธฐ์ค):
http://localhost:5500/ โ ํ(index.html)
http://localhost:5500/S00-start.html
http://localhost:5500/S01-mode.html
http://localhost:5500/S02-dine.html
http://localhost:5500/flowN/N02-menu.html โ ๋ฉ๋ด ํ์ด์ง
์ข
๋ฃ:
Ctrl + C
"""
import http.server
import socketserver
import os
import sys
import posixpath
from urllib.parse import unquote
# ---- ๋ ์ ์ ์์ ๋ฃจํธ (์ฐ์ ์์ ์) ----
HERE = os.path.dirname(os.path.abspath(__file__))
ROOTS = [
os.path.join(HERE, "src", "main", "resources", "static", "front"),
os.path.join(HERE, "src", "main", "resources", "templates_front"),
]
class MultiRootHandler(http.server.SimpleHTTPRequestHandler):
"""ROOTS ๋ฐฐ์ด์ ์ฐจ๋ก๋ก ๋ค์ ธ ๊ฐ์ฅ ๋จผ์ ๋ฐ๊ฒฌ๋ ํ์ผ์ ์๋ตํ๋ค."""
def translate_path(self, path):
path = path.split("?", 1)[0].split("#", 1)[0]
path = unquote(path)
# ๋ณด์: ์์๋ก ๋น ์ ธ๋๊ฐ๋ ๊ฒฝ๋ก ์ฐจ๋จ
path = posixpath.normpath(path).lstrip("/").lstrip("\\")
if path.startswith(".."):
return os.path.join(ROOTS[0], "")
for root in ROOTS:
full = os.path.join(root, path)
if os.path.isfile(full):
return full
# ๋๋ ํฐ๋ฆฌ๋ฉด index.html ํด๋ฐฑ
if os.path.isdir(full):
idx = os.path.join(full, "index.html")
if os.path.isfile(idx):
return idx
# ๋ชป ์ฐพ์ผ๋ฉด ์ฒซ ๋ฃจํธ ๊ธฐ์ค (404 ์ฒ๋ฆฌ)
return os.path.join(ROOTS[0], path)
def end_headers(self):
# ๋ธ๋ผ์ฐ์ ์บ์ ๋ฌด๋ ฅํ โ ์์ ์ฆ์ ๋ฐ์
self.send_header("Cache-Control", "no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
super().end_headers()
def log_message(self, fmt, *args):
# ๊น๋ํ ๋ก๊ทธ
sys.stdout.write(" %s\n" % (fmt % args))
def main():
port = 5500
if len(sys.argv) > 1:
try:
port = int(sys.argv[1])
except ValueError:
print(f"ํฌํธ ๋ฒํธ๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค: {sys.argv[1]}")
sys.exit(1)
# ๋ฃจํธ ์กด์ฌ ๊ฒ์ฆ
missing = [r for r in ROOTS if not os.path.isdir(r)]
if missing:
print("์๋ ํด๋๊ฐ ๋ณด์ด์ง ์์ต๋๋ค (ํ๋ก์ ํธ ๋ฃจํธ์์ ์คํํ์ธ์):")
for m in missing:
print(" - " + m)
sys.exit(1)
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", port), MultiRootHandler) as httpd:
print("=" * 60)
print(" NUNCHI ํ๋ก ํธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์๋ฒ ์์")
print("=" * 60)
print(f" ํฌํธ : {port}")
print(" ์ ์ ๋ฃจํธ :")
for r in ROOTS:
print(f" - {r}")
print()
print(" ๋ฐ๋ก๊ฐ๊ธฐ:")
print(f" ํ http://localhost:{port}/")
print(f" ๋ฉ๋ด(N02) http://localhost:{port}/flowN/N02-menu.html")
print(f" ๋ชจ๋(S01) http://localhost:{port}/S01-mode.html")
print(f" ํฌ์ฅ(S02) http://localhost:{port}/S02-dine.html")
print()
print(" Chrome DevTools โ ๋๋ฐ์ด์ค ํด๋ฐ โ 720 ร 1280 (Portrait) ๊ถ์ฅ")
print(" ์ข
๋ฃ: Ctrl + C")
print("=" * 60)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n์๋ฒ๋ฅผ ์ข
๋ฃํฉ๋๋ค.")
if __name__ == "__main__":
main()