-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
162 lines (127 loc) · 4.19 KB
/
server.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import asyncio
from os import path
import sys
from typing import Optional
from certbot.main import main as certbot_main
from tornado.options import OptionParser
from tornado.web import Application, RequestHandler
class IndexHandler(RequestHandler):
def get(self, directory: str) -> None:
try:
self.render(path.join(directory, "index.html"))
except IOError:
self.send_error(404)
class MainHandler(RequestHandler):
def get(self, template: str) -> None:
try:
self.render(template)
except IOError:
self.send_error(404)
def bootstrap_ssl_certs(debug: bool = False) -> None:
certbot_args = [
"--non-interactive",
"--agree-tos",
"--email",
"vandeweertj@gmail.com",
"-d",
"jacobvandeweert.com",
"-d",
"www.jacobvandeweert.com",
"-d",
"jacobvandeweert.net",
"-d",
"www.jacobvandeweert.net",
"-d",
"jacobvdw.com",
"-d",
"www.jacobvdw.com",
"-d",
"jacobvdw.net",
"-d",
"www.jacobvdw.net",
"-d",
"jvandew.com",
"-d",
"www.jvandew.com",
"-d",
"jvandew.net",
"-d",
"www.jvandew.net",
"--nginx",
]
if debug:
certbot_args.append("--test-cert")
try:
print("Invoking certbot to generate new SSL certificates...")
status = certbot_main(certbot_args)
if status:
print(status)
sys.exit(1)
if debug:
print("Nginx running with config:")
with open("/etc/nginx/nginx.conf", "r") as nginx_conf:
print(nginx_conf.read())
except SystemExit:
print("ERROR: Certbot did not run successfully, shutting down...")
raise
# NOTE(jacob): This coroutine likely introduces a slow memory leak due to certbot setting
# a new sys.excepthook on every invocation and those all being cached. I'm not sure
# this will ever be an issue in practice, but worth flagging at least. See
# https://docs.python.org/3/library/sys.html#sys.excepthook for more details.
_renew_task: Optional[asyncio.Task] = None
async def renew_ssl_certs() -> None:
await asyncio.sleep(604800) # https://youtu.be/fC_q9KPczAg
try:
print("Invoking certbot to check renewal for SSL certificates...")
status = certbot_main(
[
"renew",
"--non-interactive",
"--nginx",
]
)
if status:
print(status)
except SystemExit:
print("ERROR: Certbot did not run renewal successfully, ignoring...")
finally:
_renew_task = asyncio.create_task(renew_ssl_certs())
def parse_options() -> OptionParser:
parser = OptionParser()
parser.define("port", default=8888, help="listen port", type=int)
parser.define("static_path", default="static", help="static assert directory")
parser.define("template_path", default="templates", help="template directory")
parser.define(
"certbot", default=False, help="invoke certbot to provision SSL certs"
)
parser.define("debug", default=False, help="start in debug mode", type=bool)
parser.parse_command_line()
return parser
async def main() -> None:
opts = parse_options()
if opts.certbot:
bootstrap_ssl_certs(debug=opts.debug)
else:
print("--certbot was not specified, skipping SSL certificate generation.")
_renew_task = asyncio.create_task(renew_ssl_certs())
app = Application(
handlers=[
(r"/(?P<directory>.*)/?", IndexHandler),
(r"/(?P<template>.*\.html)", MainHandler),
],
debug=opts.debug,
static_path=opts.static_path,
template_path=opts.template_path,
)
print(f"Running tornado server on port: {opts.port}")
server = app.listen(opts.port)
try:
await asyncio.Event().wait()
except asyncio.CancelledError:
print("Cancellation received, shutting down...")
finally:
_renew_task.cancel()
server.stop()
await server.close_all_connections()
if __name__ == "__main__":
asyncio.run(main())