forked from internetarchive/openlibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverstore-server
executable file
·88 lines (56 loc) · 2.11 KB
/
coverstore-server
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
#!/usr/bin/env python
"""Run coverstore server.
Usage:
start coverstore using dev webserver:
./scripts/coverstore-server coverstore.yml 8080
start coverstore as fastcgi:
./scripts/coverstore-server coverstore.yml fastcgi 7070
start coverstore using gunicorn webserver:
./scripts/coverstore-server coverstore.yml --gunicorn -b 0.0.0.0:8080
"""
import sys
import os
import _init_path # noqa: F401 Imported for its side effect of setting PYTHONPATH
import web
web.config.debug = False
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():
from openlibrary.coverstore import server
server.main(*sys.argv[1:])
def start_gunicorn_server():
"""Starts the coverstore server using gunicorn server."""
from gunicorn.app.base import Application
configfile = sys.argv.pop(1)
class WSGIServer(Application):
def init(self, parser, opts, args):
pass
def load(self):
from openlibrary.coverstore import server, code
server.setup(configfile)
return code.app.wsgifunc(https_middleware)
WSGIServer("%prog coverstore.yml --gunicorn [options]").run()
def https_middleware(app):
"""Hack to support https even when the app server http only.
The nginx configuration has changed to add the following setting:
proxy_set_header X-Scheme $scheme;
Using that value to overwrite wsgi.url_scheme in the WSGI environ,
which is used by all redirects and other utilities.
"""
print("** https_middleware")
def wrapper(environ, start_response):
print("HTTP_X_SCHEME", environ.get("HTTP_X_SCHEME"))
if environ.get('HTTP_X_SCHEME') == 'https':
environ['wsgi.url_scheme'] = 'https'
return app(environ, start_response)
return wrapper
if __name__ == "__main__":
setup_env()
if "--gunicorn" in sys.argv:
sys.argv.pop(sys.argv.index("--gunicorn"))
start_gunicorn_server()
else:
start_server()