-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
executable file
·140 lines (102 loc) · 3.7 KB
/
service
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
#!/usr/bin/env python3
import _jsonnet as j
from aiohttp import web
import logging
import os
import json
import yaml
logger = logging.getLogger("service")
logger.setLevel(logging.INFO)
class Generator:
def __init__(self, config, base="./templates/"):
self.jsonnet_base = base
self.config = config
def process(self, config):
res = j.evaluate_snippet("config", config, import_callback=self.load)
return json.loads(res)
def load(self, dir, filename):
logger.info("Request jsonnet: %s %s", dir, filename)
if filename == "config.json" and dir == "":
print("config.json...")
path = os.path.join(".", dir, filename)
return str(path), self.config
try:
if dir:
path = os.path.join(".", dir, filename)
else:
path = os.path.join(self.jsonnet_base, filename)
logger.debug("Try: %s", path)
with open(path, "rb") as f:
logger.debug("Loaded: %s", path)
return str(path), f.read()
except:
path = os.path.join(self.jsonnet_base, filename)
logger.debug("Try: %s", path)
with open(path, "rb") as f:
logger.debug("Loaded: %s", path)
return str(path), f.read()
class Api:
def __init__(self, patterns="", config=None):
if config:
self.config = config
else:
self.config = {}
self.patterns = patterns
self.port = int(self.config.get("port", "8081"))
self.app = web.Application(middlewares=[])
self.app.add_routes([web.post("/api/generate", self.generate)])
self.app.add_routes([web.get("/{tail:.*}", self.everything)])
async def everything(self, request):
if ".." in request.path:
return web.HTTPNotFound()
if request.path == "/":
with open("dist/index.html", "r") as f:
return web.Response(
text=f.read(), content_type="text/html"
)
print(request.path)
if request.path == "/api/patterns":
return web.Response(
text=self.patterns, content_type="application/json"
)
if request.path.endswith(".css"):
with open("dist" + request.path, "r") as f:
data = f.read()
return web.Response(
text=data, content_type="text/css"
)
if request.path.endswith(".js"):
with open("dist" + request.path, "r") as f:
data = f.read()
return web.Response(
text=data, content_type="text/javascript"
)
if request.path.endswith(".html"):
with open("dist" + request.path, "r") as f:
data = f.read()
return web.Response(
text=data, content_type="text/html"
)
return web.HTTPNotFound()
async def generate(self, request):
print("Generate...")
config = await request.text()
print(config)
config = config.encode("utf-8")
gen = Generator(config)
with open("./templates/config-loader.jsonnet", "r") as f:
wrapper = f.read()
processed = gen.process(wrapper)
return web.Response(
text=yaml.dump(processed), content_type = "text/plain"
)
def run(self):
web.run_app(self.app, port=self.port)
print("Create all-patterns from templates...")
with open("./templates/all-patterns.jsonnet", "r") as f:
cfg = f.read()
gen = Generator("")
patterns = gen.process(cfg)
print("Done.")
a = Api(patterns=json.dumps(patterns, indent=4))
a.run()