/
medley.py
233 lines (194 loc) · 7.51 KB
/
medley.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Medley, a collection of web-based utilities
Medley is an application server for lightweight, single-purpose
applications that are too small or trivial to bother making
standalone.
Having them under one roof provides makes it possible to share
frequently-needed services instead of starting from zero. It also
helps each application stay relatively small.
"""
import importlib
import logging
import logging.config
import os
import os.path
import cherrypy
import plugins
import tools
# pylint: disable=too-many-statements
def main():
"""Configure and start the application server
The application server is the backbone that individual
applications are attached to. It does almost nothing by itself
except load applications and plugins.
"""
server_root = os.path.dirname(os.path.abspath(__file__))
app_root = os.path.join(server_root, "apps")
# Configuration - default values
cherrypy.config.update({
"cache_dir": "./cache",
"database_dir": "./db",
"engine.autoreload.on": True,
"local_maintenance": True,
"log.screen": True,
"log_dir": "./logs",
"memorize_checksums": True,
"request.show_tracebacks": True,
"server.daemonize": False,
"server.socket_host": "0.0.0.0",
"server.socket_port": 8085,
"server_root": server_root,
"tools.conditional_auth.on": False,
# Jinja templating won't work unless tools.encode is off
"tools.encode.on": False,
# Gzipping locally avoids Etag complexity. If a reverse
# proxy handles it, the Etag could be dropped.
"tools.gzip.on": True,
})
# Overrides to the default configuration are sourced from
# /etc/medley.conf or medley.conf in the application root,
# whichever is found first.
config_candidates = (
"/etc/medley.conf",
os.path.join(server_root, "medley.conf")
)
try:
config = next(
(candidate for candidate in config_candidates
if os.path.isfile(candidate)),
)
cherrypy.config.update(config)
except StopIteration:
pass
# Some configuration values are set automatically.
cherrypy.config.update({
"app_root": app_root
})
# Directory creation
#
# Filesystem paths declared by the config are expected to exist.
for key in ("database_dir", "cache_dir", "log_dir"):
value = cherrypy.config.get(key)
try:
os.mkdir(value)
except FileExistsError:
pass
except PermissionError:
raise SystemExit(
"Unable to create {} directory at {}".format(key, value)
)
# Mount the apps
for app in os.listdir(app_root):
if not os.path.isfile(os.path.join(app_root, app, "main.py")):
continue
app_module = importlib.import_module("apps.{}.main".format(app))
# Treat all controllers as exposed by default.
# This is a Cherrypy-ism.
if not hasattr(app_module.Controller, "exposed"):
app_module.Controller.exposed = True
# Treat all controllers as user-facing by default. This is a
# Medley-ism. Service apps should override this attribute
# locally.
if not hasattr(app_module.Controller, "user_facing"):
app_module.Controller.user_facing = True
app_config = {
"/": {
"request.dispatch": cherrypy.dispatch.MethodDispatcher()
},
}
# The homepage app is unique. Its app name is not its url, and
# its static path is not under its app path. It also has additional
# configuration for serving the favicon.
app_path = "/{}".format(app)
static_url = "/static"
if app == "homepage":
app_path = "/"
static_url = "/homepage/static"
app_config["/favicon.ico"] = {
"tools.staticfile.on": True,
"tools.staticfile.filename": os.path.realpath(
"./apps/shared/static/favicon/favicon.ico"
),
}
# An app can optionally have a dedicated directory for static assets
static_path = os.path.join(app_root, app, "static")
if os.path.isdir(static_path):
app_config[static_url] = {
"tools.gzip.on": True,
"tools.gzip.mime_types": ["text/*", "application/*"],
"tools.staticdir.on": True,
"tools.staticdir.dir": os.path.realpath(static_path),
"tools.expires.on": True,
"tools.expires.secs": 86400 * 7
}
cherrypy.tree.mount(
app_module.Controller(),
app_path,
app_config
)
# Plugins
plugins.applog.Plugin(cherrypy.engine).subscribe()
plugins.scheduler.Plugin(cherrypy.engine).subscribe()
plugins.audio.Plugin(cherrypy.engine).subscribe()
plugins.bookmarks.Plugin(cherrypy.engine).subscribe()
plugins.cache.Plugin(cherrypy.engine).subscribe()
plugins.capture.Plugin(cherrypy.engine).subscribe()
plugins.cdr.Plugin(cherrypy.engine).subscribe()
plugins.checksum.Plugin(cherrypy.engine).subscribe()
plugins.converters.Plugin(cherrypy.engine).subscribe()
plugins.formatting.Plugin(cherrypy.engine).subscribe()
plugins.geography.Plugin(cherrypy.engine).subscribe()
plugins.hasher.Plugin(cherrypy.engine).subscribe()
plugins.ip.Plugin(cherrypy.engine).subscribe()
plugins.jinja.Plugin(cherrypy.engine).subscribe()
plugins.logindex.Plugin(cherrypy.engine).subscribe()
plugins.maintenance.Plugin(cherrypy.engine).subscribe()
plugins.markup.Plugin(cherrypy.engine).subscribe()
plugins.notifier.Plugin(cherrypy.engine).subscribe()
plugins.parse.Plugin(cherrypy.engine).subscribe()
plugins.registry.Plugin(cherrypy.engine).subscribe()
plugins.speak.Plugin(cherrypy.engine).subscribe()
plugins.memorize.Plugin(cherrypy.engine).subscribe()
plugins.urlfetch.Plugin(cherrypy.engine).subscribe()
plugins.url.Plugin(cherrypy.engine).subscribe()
# Tools
cherrypy.tools.conditional_auth = tools.conditional_auth.Tool()
cherrypy.tools.response_time = tools.response_time.Tool()
cherrypy.tools.negotiable = tools.negotiable.Tool()
cherrypy.tools.template = tools.template.Tool()
cherrypy.tools.capture = tools.capture.Tool()
# Logging
error_log_handler = logging.handlers.TimedRotatingFileHandler(
os.path.join(
cherrypy.config.get("log_dir"),
"error.log"
),
when="D",
interval=1,
backupCount=14,
encoding="utf8"
)
error_log_handler.setLevel(logging.INFO)
# pylint: disable=protected-access
error_log_handler.setFormatter(cherrypy._cplogging.logfmt)
cherrypy.log.error_log.addHandler(error_log_handler)
access_log_handler = logging.handlers.TimedRotatingFileHandler(
os.path.join(
cherrypy.config.get("log_dir"),
"access.log"
),
when="D",
interval=1,
backupCount=14,
encoding="utf8"
)
access_log_handler.setLevel(logging.INFO)
# pylint: disable=protected-access
access_log_handler.setFormatter(cherrypy._cplogging.logfmt)
cherrypy.log.access_log.addHandler(access_log_handler)
cherrypy.engine.start()
cherrypy.engine.publish("scheduler:revive")
cherrypy.engine.publish("logindex:parse")
cherrypy.engine.publish("bookmarks:add:fulltext")
cherrypy.engine.block()
if __name__ == '__main__':
main()