-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserve.py
178 lines (145 loc) · 5.41 KB
/
serve.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Python script to serve the FAQ-div website and examples.
You may need to ``pip install uvicorn, asgineer, markdown jinja2 pygments``.
"""
import os
import json
import socket
import build
import asgineer
import markdown
import jinja2
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name
# %% Collect and generate assets
def md_highlight(text):
"""Apply syntax highlighting."""
lines = []
code = []
for i, line in enumerate(text.splitlines()):
if line.startswith("```"):
if code:
formatter = HtmlFormatter()
try:
lexer = get_lexer_by_name(code[0].strip())
except Exception:
lexer = get_lexer_by_name("text")
html = highlight("\n".join(code[1:]), lexer, formatter)
lines.append(html)
code = []
else:
code.append(line[3:].strip()) # language
elif code:
code.append(line)
else:
lines.append(line)
return "\n".join(lines).strip()
def md2html(text):
text2 = md_highlight(text)
html = markdown.markdown(text2, extensions=[])
return html
def collect_assets():
# Collect
this_dir = os.path.abspath(os.path.dirname(__file__))
assets = {}
examples = []
for subdir in ("", "src", "dist", "website", "examples", "website/img"):
fulldir = os.path.join(this_dir, subdir) if subdir else this_dir
for fname in os.listdir(fulldir):
filename = os.path.join(fulldir, fname)
if not os.path.isfile(filename):
continue
elif fname.endswith((".md", ".html", ".js", ".css")):
with open(filename, "rb") as f:
assets[fname] = f.read().decode()
if subdir == "examples" and fname.endswith(".html"):
examples.append(fname[:-5])
elif fname.endswith((".png", ".jpg", ".ico", ".svg")):
with open(filename, "rb") as f:
assets[fname] = f.read()
# Collect blog pages
template = jinja2.Template(assets["template.html"])
blogpages = {}
for fname in os.listdir(os.path.join(this_dir, "website", "blog")):
if fname.endswith(".md"):
fname2 = "blog/" + fname[:-3] + ".html"
with open(os.path.join(this_dir, "website", "blog", fname), "rb") as f:
text = f.read().decode()
title = text.splitlines()[0].strip("#").strip()
date = text.split("-- DATE:")[1].split("--")[0].strip()
assert len(date) == 10
blogpages[date] = fname2, title
assets[fname2] = template.render(
title="FAQ-div blog: " + title,
header_image="faqdiv-blog.png",
content=md2html(text),
)
html = "<h2>Pages</h2>\n\n"
for date in sorted(blogpages.keys(), reverse=False):
fname, title = blogpages[date]
html += f"<a href='/{fname}'>{title}</a><br /><br />\n"
assets["blog"] = template.render(
title="FAQ-div blog",
header_image="faqdiv-blog.png",
content=html,
)
assets["blog/"] = assets["blog"] # aliases
# Generate sitemap
sitemap = ["", "blog"] + [fname for fname, title in blogpages.values()]
sitemap = ["https://faq-div.com/" + x for x in sitemap]
assets["sitemap.txt"] = "\n".join(sitemap)
# Generate robots.txt
robots = [
"Sitemap: https://faq-div.com/sitemap.txt",
"User-agent: *",
"Allow: /",
"",
]
assets["robots.txt"] = "\n".join(robots)
# Post processing
index_template = jinja2.Template(assets["index.html"])
assets["index.html"] = index_template.render(
example_names=", ".join(repr(x) for x in sorted(examples)),
faq=md2html(assets["faq.md"]),
)
#
assets["license_commercial.html"] = template.render(
title="FAQ-div license",
header_image="faqdiv-wide.png",
content=md2html(assets["license_commercial.md"]),
)
return assets
build.main()
asset_handler = asgineer.utils.make_asset_handler(collect_assets())
# %% Serving
stats_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send_stats(request, status_code=None, rtime=None, is_page=None):
"""Send request stats over UPD to a stats server."""
mypaas_service = os.getenv("MYPAAS_SERVICE", "")
if not mypaas_service:
return
p = request.path
stats = {"group": mypaas_service}
stats["requests|count"] = 1
stats["path|cat"] = f"{status_code} - {p}" if (status_code and p) else p
if rtime is not None:
stats["rtime|num|s"] = float(rtime)
if is_page: # anomimously register page view, visitors, language, and more
stats["pageview"] = request.headers
try:
stats_socket.sendto(json.dumps(stats).encode(), ("stats", 8125))
except Exception:
pass
@asgineer.to_asgi
async def main_handler(request):
path = request.path.lstrip("/")
response = await asset_handler(request, path or "index.html")
response = asgineer.utils.normalize_response(response)
is_page = "." not in path or path.endswith(".html")
send_stats(request, response[0], is_page=is_page)
return response
def main():
asgineer.run(main_handler, "uvicorn", "0.0.0.0:80", log_level="warning")
if __name__ == "__main__":
main()