-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.py
195 lines (157 loc) · 9.56 KB
/
boot.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
## Written for Python 3.4
## (c) Alexander Balashov, 2014-2015. All rights reserved.
##
## Dependencies:
## Python Modules/Extensions (Install from PyPI):
## - PyMongo (Used v2.7.2 64-bit, for Python 3.4.2 64-bit)
## MongoDB Python Driver
## - PassLib (Used v1.6.2, Source Distribution Setup, for Python 3.4)
## Password Encryption Library
## - Jinja2 (Used v2.7.3, Installed w/ setup.py install + "distribute" extension)
## Twig & Django-like Templating Engine
## - Beaker 1.6.5
## Session management
## - wsgi-request-logger (Used 0.4.2)
## - redis (2.10.3)
## - beaker-extensions-0.2.0.dev0
## - requests
## - lxml
## - Markdown
## - python-slugify
## - CherryPy (Only for dev/local installs)
## WSGI-Compliant Multi-Threaded Webserver. May be replaced on production.
##
## If you want to install this as a Windows service, run 'python installAsWinService.py install'
## from the command line. This requires another Python module - pyWin32.exe. Install it by
## creating another user to manage it (find docs online).
##
import sys, imp, os
def application():
# Create global state module
globals = imp.new_module('app.globals')
sys.modules['app.state'] = globals
# Change current path to root of app for future file loads and such.
os.chdir(os.path.dirname(os.path.realpath(__file__)))
# Create global logging machine if not on OpenShift
print("Logmachine Status: " + str(os.environ.get('OPENSHIFT_LOGMACHINE')))
from app.utilities.issues_logger import LogMachine
globals.logMachine = LogMachine()
if __name__ != '__main__' and os.environ.get('OPENSHIFT_LOGMACHINE') is None:
globals.logMachine.commandLine = False
sys.stdout = globals.logMachine.writeLog
sys.stderr = globals.logMachine.errorLog
# Setup Bottle
from app.includes.bottle import Bottle, run, TEMPLATE_PATH, url, response, request, app as s_bottle_app
globals.app = Bottle()
try:
globals.app.config.load_config('./settings.default.ini') # Read config/settings, e.g. for MongoDB connection. Defaults first.
# Overwrite defaults w/ custom info.
# If not using settings.ini, make sure all environment variables are set to overwrite "..." fields in settings.ini
globals.app.config.load_config('./settings.ini')
except: pass
# Connect to MongoDB Instance
from pymongo import MongoClient
# Set configuration environment variables where possible.
if os.environ.get('OPENSHIFT_MONGODB_DB_URL') != None: globals.app.config['security.mongo_url'] = os.environ.get('OPENSHIFT_MONGODB_DB_URL')
elif os.environ.get('MONGODB_DB_URL') != None: globals.app.config['security.mongo_url'] = os.environ.get('MONGODB_DB_URL')
if os.environ.get('OPENSHIFT_MONGODB_DB_DB') != None: globals.app.config['security.mongo_db'] = os.environ.get('OPENSHIFT_MONGODB_DB_DB')
elif os.environ.get('MONGODB_DB_DB') != None: globals.app.config['security.mongo_db'] = os.environ.get('MONGODB_DB_DB')
if os.environ.get('REDIS_DB_URL') != None: globals.app.config['security.redis_url'] = os.environ.get('REDIS_DB_URL')
if os.environ.get('REDIS_DB_KEY') != None: globals.app.config['security.redis_password'] = os.environ.get('REDIS_DB_KEY')
if os.environ.get('SENDGRID_USERNAME') != None: globals.app.config['email.sendgrid_api_user'] = os.environ.get('SENDGRID_USERNAME')
if os.environ.get('SENDGRID_PASSWORD') != None: globals.app.config['email.sendgrid_api_key'] = os.environ.get('SENDGRID_PASSWORD')
if os.environ.get('EMAIL_REPLYTO') != None: globals.app.config['email.replyto_email'] = os.environ.get('EMAIL_REPLYTO')
if os.environ.get('GLOBAL_BCC_EMAIL') != None: globals.app.config['email.global_bcc_email'] = os.environ.get('GLOBAL_BCC_EMAIL')
if os.environ.get('GOOGLE_RECAPTCHA_KEY') != None: globals.app.config['security.google_recaptcha_key'] = os.environ.get('GOOGLE_RECAPTCHA_KEY')
if os.environ.get('DISQUS_PRIVATE_KEY') != None: globals.app.config['discussions.disqus_private_key'] = os.environ.get('DISQUS_PRIVATE_KEY')
if os.environ.get('DISQUS_PUBLIC_KEY') != None: globals.app.config['discussions.disqus_public_key'] = os.environ.get('DISQUS_PUBLIC_KEY')
if os.environ.get('DISQUS_ACCESS_NAME') != None: globals.app.config['discussions.disqus_access_name'] = os.environ.get('DISQUS_ACCESS_NAME')
if os.environ.get('DISQUS_ACCESS_TOKEN') != None: globals.app.config['discussions.disqus_access_token'] = os.environ.get('DISQUS_ACCESS_TOKEN')
# Set up MongoDB connection
globals.mongo_url = globals.app.config['security.mongo_url']
globals.mongo_client = MongoClient(globals.mongo_url)
globals.db = globals.mongo_client[globals.app.config['security.mongo_db']]
# Set Up Sessions
from beaker.middleware import SessionMiddleware
# Common
session_opts = {
'session.timeout' : int(globals.app.config['security.sessions_duration']),
'session.cookie_expires': int(globals.app.config['security.sessions_duration']),
'session.expire': int(globals.app.config['security.sessions_duration']),
'session.data_dir': globals.app.config['security.sessions_dir'],
'session.serializer': 'json'
}
# If Memcached
if globals.app.config['security.sessions_type'] == 'memcached':
session_opts['session.type'] = 'ext:memcached'
session_opts['session.url'] = globals.app.config['security.memcached_url']
session_opts['session.username'] = globals.app.config['security.memcached_user']
session_opts['session.password'] = globals.app.config['security.memcached_password']
# If Redis
elif globals.app.config['security.sessions_type'] == 'redis':
import app.includes.beaker_extensions
session_opts['session.type'] = 'redis'
session_opts['session.url'] = globals.app.config['security.redis_url']
session_opts['session.password'] = globals.app.config['security.redis_password']
# If Cookies
elif globals.app.config['security.sessions_type'] == 'cookie':
session_opts['session.type'] = 'cookie'
session_opts['session.key'] = globals.app.config['app_info.app_service_name']
session_opts['session.secret'] = globals.app.config['security.cookies_secret']
session_opts['session.validate_key'] = 'TESTMORE'
session_opts['session.serializer'] = 'pickle'
globals.beakerMiddleware = SessionMiddleware(globals.app, session_opts)
# Index DB Collections
globals.db.users.ensure_index([('username', 1)], cache_for=31536000, unique=True)
globals.db.issues.ensure_index([('title', 'text')], cache_for=31536000)
globals.db.tags.ensure_index([('title', 'text')], cache_for=31536000)
globals.db.flood_ip.ensure_index([('timestamp', -1)], cache_for=31536000, unique=False, expireAfterSeconds=int(globals.app.config['security.ip_flood_limit']))
# Schedule Any Tasks
import app.schedule_tasks
# Setup Jinja2 Templates. Jinja2 appears to be nearly identical to Twig, so it was chosen.
TEMPLATE_PATH.insert(0, './view/templates/')
import app.template_setup
# Route Up
import app.routing
# Set application variable to current runnable wsgi app.
# The obj given to OpenShift to run.
application = globals.beakerMiddleware
# Setup Logging to Files, if set
if globals.app.config['security.log_files'] not in [False, "false", "False"] and os.environ.get('OPENSHIFT_LOGMACHINE') is None:
from logging.handlers import TimedRotatingFileHandler
from requestlogger import WSGILogger, ApacheFormatter
log_handlers = [ TimedRotatingFileHandler(globals.app.config['app_info.log_dir'] + 'access.log', when='d', interval=1, backupCount=5) , ]
globals.loggedApp = WSGILogger(globals.beakerMiddleware, log_handlers, ApacheFormatter())
def fix_environ_middleware(app):
def fixed_app(environ, start_response):
environ['wsgi.url_scheme'] = 'http'
environ['HTTP_X_FORWARDED_HOST'] = globals.app.config['app_info.side_domain']
return app(environ, start_response)
return fixed_app
print(globals.loggedApp)
globals.loggedApp.wsgi = fix_environ_middleware(globals.loggedApp)
application = globals.loggedApp
return application
# Run on local w/ CherryPy if launched directly.
if __name__ == "__main__":
application = application()
# CherryPy Server Setup
import cherrypy
from app.state import app
cherrypy.tree.graft(application, '/')
cherrypy.config.update({
'log.access_file' : app.config['app_info.log_dir'] + 'access_cherry.txt',
'log.error_file' : app.config['app_info.log_dir'] + 'error_cherry.txt',
'log.screen' : False,
'engine.autoreload.on': False,
'server.socket_host': '0.0.0.0',
'environment' : 'production',
'server.socket_port' : int(app.config['app_info.site_port'])
})
# Debug
print('Ran through boot process. Starting server.')
cherrypy.engine.signals.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
# Go (Using Bottle; Alternate)
#run(server='cherrypy', app=globals.loggedApp, host='0.0.0.0', port=8004)