-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_nginx_config.py
57 lines (45 loc) · 1.27 KB
/
generate_nginx_config.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
import yaml
def load_config():
with open("config.yml", "r") as config_file:
try:
return yaml.safe_load(config_file)
except yaml.YAMLError as exc:
print(exc)
def flatten_forwards(forwards):
result = {}
for path, target in forwards.items():
if isinstance(target, dict):
for subpath, subtarget in flatten_forwards(target).items():
result["/".join([path, subpath])] = subtarget
else:
result[path] = target
return result
def generate_forward(path, target):
return f"""
location = /{path} {{
return 301 {target};
}}
"""
def generate_nginx_config(config):
return f"""
events {{
worker_connections {config.get("connections", 1024)};
}}
http {{
include mime.types;
default_type application/octet-stream;
sendfile on;
server {{
listen 80;
listen [::]:80;
server_name {config.get("server_name")};
location /
{{
root /etc/nginx/html;
index index.html;
}}
{"\n".join(generate_forward(path, target) for path, target in flatten_forwards(config.get("forwards", [])).items())}
}}
}}
"""
print(generate_nginx_config(load_config()))