forked from openlibrary/openlibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenlibrary-server
More file actions
executable file
·170 lines (122 loc) · 4.53 KB
/
Copy pathopenlibrary-server
File metadata and controls
executable file
·170 lines (122 loc) · 4.53 KB
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
#! /usr/bin/env python
"""Script to run openlibrary server.
USAGE:
* Run openlibrary http server at port 8080.
$ ./scripts/openlibrary-server infogami.yml startserver 8080
* Run openlibrary as fastcgi server at port 7070
$ ./scripts/openlibrary-server infogami.yml startserver fastcgi 8080
"""
import sys
import os
import _init_path
import web
from gunicorn.app.base import Application
def setup_env():
# make sure PYTHON_EGG_CACHE is writable
os.environ['PYTHON_EGG_CACHE'] = "/tmp/.python-eggs"
# required when run as fastcgi
os.environ['REAL_SCRIPT_NAME'] = ""
def start_server():
args = sys.argv[1:]
if len(args) < 1 or sys.argv[0] in ['-h', '--help']:
print >> sys.stderr, "USAGE: %s configfile [subcommand] [arguments]" % (sys.argv[0])
sys.exit(1)
import infogami
from infogami import config
config.plugin_path += ['openlibrary.plugins']
config.site = "openlibrary.org"
infogami.main(*args)
def start_gunicorn_server():
"""Starts the openlibrary server using gunicorn server.
"""
configfile = sys.argv.pop(1)
web.config.debug = False
def dummy(*a, **kw):
pass
class WSGIServer(Application):
def init(self, parser, opts, args):
pass
def load(self):
import infogami
from infogami import config
from infogami.utils import delegate
config.plugin_path += ['openlibrary.plugins']
config.site = "openlibrary.org"
# add a dummy action and run it
infogami.action(dummy)
infogami.main(configfile, "dummy")
wsgi_app = delegate.app.wsgifunc(*config.middleware)
# setup middleware for handling /static
wsgi_app = web.httpserver.StaticMiddleware(wsgi_app)
return wsgi_app
WSGIServer("%prog openlibrary.yml --gunicorn [options]").run()
class BaseWSGIServer(Application):
def __init__(self, config_file):
self.config_file = config_file
Application.__init__(self)
def init(self, parser, opts, args):
pass
def load(self):
import infogami
from infogami import config
from infogami.utils import delegate
config.plugin_path += ['openlibrary.plugins']
config.site = "openlibrary.org"
infogami.load_config(self.config_file)
infogami._setup()
wsgi_app = self.get_wsgi_app()
# setup middleware for handling /static
wsgi_app = web.httpserver.StaticMiddleware(wsgi_app)
return wsgi_app
def get_wsgi_app(self):
raise NotImplementedError()
class OLWSGIServer(BaseWSGIServer):
def get_wsgi_app(self):
from infogami import config
from infogami.utils import delegate
return delegate.app.wsgifunc(*config.middleware)
class AdminWSGIServer(BaseWSGIServer):
def get_wsgi_app(self):
from openlibrary.admin import code
code.setup()
return code.app.wsgifunc()
def start_admin_server():
"""Starts the openlirbary admin using gunicorn server.
"""
from gunicorn.app.base import Application
configfile = sys.argv.pop(1)
web.config.debug = False
def dummy(*a, **kw):
pass
class WSGIServer(Application):
def init(self, parser, opts, args):
pass
def load(self):
import infogami
from infogami import config
from infogami.utils import delegate
config.plugin_path += ['openlibrary.plugins']
config.site = "openlibrary.org"
# add a dummy action and run it
infogami.action(dummy)
infogami.main(configfile, "dummy")
wsgi_app = delegate.app.wsgifunc(*config.middleware)
# setup middleware for handling /static
wsgi_app = web.httpserver.StaticMiddleware(wsgi_app)
return wsgi_app
WSGIServer("%prog openlibrary.yml --gunicorn [options]").run()
def main():
setup_env()
web.config.debug = False
if "--gunicorn" in sys.argv:
sys.argv.pop(sys.argv.index("--gunicorn"))
configfile = sys.argv.pop(1)
OLWSGIServer(configfile).run()
if "--admin" in sys.argv:
sys.argv.pop(sys.argv.index("--admin"))
configfile = sys.argv.pop(1)
AdminWSGIServer(configfile).run()
else:
start_server()
if __name__ == "__main__":
main()