-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
199 lines (181 loc) · 9.12 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
# ChlorideSiteGenerator 2
# Copyright (C) 2016 Sebastian "Chloride Cull" Johansson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from bottle import Bottle, static_file, HTTPResponse, view, request, response
from waitress import serve as waitress_serve
import inspect
import os
import sass
import json
import argparse
import importlib
import sys
import glob
import random
import uuid
import sandbox
import configman
class CSG2Server:
runningsessions = {}
def __init__(self, themedir, siteconftemplate):
self.installdir = sys.path[0] # Note: this should ideally be gotten from somewhere else.
self.wsgiapp = Bottle()
self.apiclass = sandbox.csg2api(self.wsgiapp, self.runningsessions)
# Parse arguments
argparser = argparse.ArgumentParser()
argparser.add_argument('sitesfolder', metavar='<site storage folder>', help="path to the folder containing sites")
argparser.add_argument('siteroot', metavar='<site name>', help="site name/the folder with config.json")
self.parsedargs = argparser.parse_args()
# Setup configuration and path to site
self.sitepath = os.path.abspath(os.path.join(self.parsedargs.sitesfolder, self.parsedargs.siteroot))
siteconffile = open(os.path.join(self.sitepath, "config.json"), mode="rt", encoding="utf-8")
self.siteconf = configman.normalize_config(json.load(siteconffile), self.parsedargs.siteroot)
siteconffile.close()
# Setup theming
themesroot = os.path.abspath(themedir)
self.themepath = os.path.join(themesroot, self.siteconf["site"]["theme"])
os.chdir(self.sitepath)
# Assign routes (done before the site code to allow overrides)
# This is functionally equivalent of what the language does, but makes sure Bottle will call the right instance.
self.getrandstaticredirect = self.wsgiapp.route("/rand/<filepath:path>")(self.getrandstaticredirect)
self.getstatic = self.wsgiapp.route("/static/<filepath:path>")(self.getstatic)
self.compilethemesass = self.wsgiapp.route("/theme/sass/master.scss")(self.compilethemesass)
self.getthemeasset = self.wsgiapp.route("/theme/static/<filepath:path>")(self.getthemeasset)
self.compilesass = self.wsgiapp.route("/sass/<filename:re:.*\.scss>")(self.compilesass)
self.catchall = self.wsgiapp.route("/")(
self.wsgiapp.route("/<filepath:path>")(
view(os.path.join(self.themepath, "master.tpl"))(self.catchall)
)
)
self.dologin = self.wsgiapp.route("/login", method="POST")(self.dologin)
# If they have code, run it
if "additional_code" in self.siteconf["site"]:
oldpath = sys.path
sys.path[0] = self.sitepath
importlib.invalidate_caches()
with open(os.path.join(self.sitepath, self.siteconf["site"]["additional_code"]), mode="rt") as codefile:
sandbox.create_box(codefile.read(), self.wsgiapp, apiclass=self.apiclass) # This file is excempt from the linking clauses in the license, allowing it to be non-(A)GPL.
sys.path = oldpath
importlib.invalidate_caches()
# Configure Nginx
socketpath = "/tmp/csg2_{}.sock".format(self.siteconf["site"]["domain_name"].replace(".", "_"))
print("-> Generating config.")
with open(os.path.abspath(siteconftemplate), mode="rt", encoding="utf-8") as sitetemplate:
sitetemplatetxt = sitetemplate.read()
newsite = sitetemplatetxt.replace("%%SERVERNAME%%", self.siteconf["site"]["domain_name"]).replace("%%SOCKETPATH%%", socketpath)
with open("/tmp/{}.csg2nginx".format(self.siteconf["site"]["domain_name"].replace(".", "_")), mode="wt", encoding="utf-8") as newconf:
newconf.write(newsite)
# Serve site.
print("-> Serving up site on '{}'.".format(socketpath))
waitress_serve(self.wsgiapp, unix_socket=socketpath)
# Route: "/rand/<filepath:path>"
def getrandstaticredirect(self, filepath):
extmap = {
"image": ["png", "jpg", "jpeg", "gif"] # Using "image" as extention allows matching with all image types
}
response.status = 307 # Temporary Redirect
modfilepath = filepath.split(".")
targetmap = modfilepath[-1:]
matches = []
if modfilepath[-1] in extmap.keys():
targetmap = extmap[modfilepath[-1]]
for ext in targetmap:
modfilepathstr = ".".join(modfilepath[:-1] + ["*"] + [ext])
matches = matches + glob.glob(os.path.join(self.sitepath, "static/", modfilepathstr))
if len(matches) == 0:
response.status = 404
return "Files not found."
pick = random.choice(matches).replace(self.sitepath, "").replace("\\", "/")
response.set_header("Location", pick)
return ""
# Route: "/static/<filepath:path>"
def getstatic(self, filepath):
response.set_header("Cache-Control", "max-age=300")
return static_file(filepath, root=os.path.join(self.sitepath, "static/"))
# Route: "/theme/sass/master.scss"
def compilethemesass(self):
output = ""
response.set_header("Cache-Control", "max-age=300")
response.content_type = "text/css"
with open(os.path.join(self.sitepath, "scss/theme.scss"), mode="rt") as fl:
output += fl.read()
with open(os.path.join(self.themepath, "master.scss"), mode="rt") as fl:
output += fl.read()
return sass.compile(string=output)
# Route: "/theme/static/<filepath:path>"
def getthemeasset(self, filepath):
response.set_header("Cache-Control", "max-age=300")
return static_file(filepath, root=os.path.join(self.themepath, "assets/"))
# Route: "/sass/<filename:re:.*\.scss>"
def compilesass(self, filename):
output = ""
response.set_header("Cache-Control", "max-age=300")
response.content_type = "text/css"
if not os.path.exists(os.path.join(self.sitepath, "scss/" + filename)):
response.status = 404
return "SCSS file not found."
with open(os.path.join(self.sitepath, "scss/" + filename), mode="rt") as fl:
output += fl.read()
return sass.compile(string=output)
# Route: "/"
# Route: "/<filepath:path>"
def catchall(self, filepath="index"):
if filepath[-1] == "/":
filepath = filepath[:-1]
pageindex = -1
for i in range(0, len(self.siteconf["pages"])):
if self.siteconf["pages"][i]["path"] == filepath:
pageindex = i
break
templatepath = os.path.join(self.sitepath, filepath + ".tpl")
if not os.path.exists(templatepath):
templatepath = os.path.join(self.installdir, "default-files/", filepath + ".tpl")
if not os.path.exists(templatepath):
response.status = 404
return "Page not found :C"
if self.apiclass.authhook != None:
response.set_header("Cache-Control", "no-cache")
if (self.siteconf["pages"][pageindex]["require_auth"]) and (request.get_cookie("csg2sess") not in self.runningsessions) and (filepath != "login"):
response.status = "307 Not Logged In"
response.set_header("Location", "/login")
return ""
else:
response.set_header("Cache-Control", "max-age=300")
return {
"title": self.siteconf["site"]["title"],
"link_elements": self.siteconf["pages"][pageindex]["link_elements"],
"nav_links": [("/" + k["path"], k["title"],) for k in self.siteconf["pages"] if k["hidden"] == False],
"content": templatepath,
"csg2api": self.apiclass
}
#Route: "/login", method="POST"
def dologin(self):
if self.apiclass.authhook == None:
response.status = "303 No Need To Log In"
response.set_header("Location", "/")
return ""
if self.apiclass.authhook(request.forms.user, request.forms.password):
uid = uuid.uuid4().hex + uuid.uuid4().hex
response.set_cookie("csg2sess", uid)
self.runningsessions[uid] = request.forms.user
response.status = "303 Successfully Logged In"
response.set_header("Location", "/")
return ""
else:
response.status = "303 Incorrect Credentials"
response.set_header("Location", "/login")
return ""