-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertToNginx.py
60 lines (44 loc) · 1.06 KB
/
convertToNginx.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
import yaml
# Load links from links.yml
with open("links.yml", "r") as stream:
try:
links = yaml.safe_load(stream)
links = links["links"]
except yaml.YAMLError as exc:
print(exc)
print("Loaded " + str(len(links)) + " links")
def convertLinksToNginx(links):
# create for each link a nginx location
nginx = """
events {
worker_connections 4096; ## Default: 1024
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
listen [::]:80;
server_name link.thilo.scouts.ch;
location /
{
root /etc/nginx/html;
index index.html;
}
"""
for link in links:
print("Adding " + link + " to " + links[link])
nginx += "location = /" + link + " {\n"
nginx += "\treturn 301 " + links[link] + ";\n"
nginx += "}\n"
nginx += "\n"
nginx += """
}
}
"""
return nginx
nginx = convertLinksToNginx(links)
# Write nginx config to nginx.conf
with open("nginx.conf", "w", encoding="UTF-8") as f:
f.write(nginx)