Example #1
0
def main (global_config, **settings):
	""" This function returns a Pyramid WSGI application.
	"""
	sapyens.helpers.set_utc_timezone()

	engine = engine_from_config(settings, 'sqlalchemy.')
	boardless.db.init(engine, settings)

	config = Configurator(
			settings = settings,
			root_factory = RootFactory,
			session_factory = pyramid.session.SignedCookieSessionFactory(
				# The secret should be at least as long as the block size of the selected hash algorithm. For sha512 this would mean a 128 bit (64 character) secret.
				# For sha512 this would mean a 128 bit (64 character) secret
				secret = 'sETbVPAqkZxJTneqWpgnczyGhuwtfHNYMFZUMVwRjDiIRuSKGzdymHNBjDatQlhr',
				hashalg = 'sha512',
				cookie_name = 'boardless.session',
				timeout = 60 * 60 * 24 * 3, # A number of seconds of inactivity before a session times out. If None then the cookie never expires
				max_age = 60 * 60 * 24 * 3, # The maximum age of the cookie used for sessioning (in seconds). Default: None (browser scope).
				domain = settings.get('cookie_domain', '.boardless.com'), # TODO
				set_on_exception = True, # If True, set a session cookie even if an exception occurs while rendering a view
			),
			authentication_policy = pyramid.authentication.SessionAuthenticationPolicy(
				callback = get_identifiers,
				debug = False
			),
			authorization_policy = pyramid.authorization.ACLAuthorizationPolicy(),
	)

	# Mailer
	config.include('pyramid_mailer')

	json_renderer = JSON()
	json_renderer.add_adapter(decimal.Decimal, h.decimal_json_encoder)

	config.add_renderer('json', json_renderer)
	config.set_request_property(h.get_user, 'user')
	config.add_static_view('static', 'static', cache_max_age = 3600)
	config.scan(boardless.views)

	# Recreate the pool to close left connections (e.g. after reflection)
	# It prevents connection sharing between later-forked server workers (e.g. gunicorn with preload_app)
	DBSession.remove()
	engine.dispose()

	return config.make_wsgi_app()
Example #2
0
def websocket_app (environ, start_response):
	try:
		if environ['PATH_INFO'] == '/playground':
			websocket = environ['wsgi.websocket']

			while True:
				try:
					data = websocket.receive()
					if not data:
						State.forget_client(websocket)
						break
					else:
						utils.handle_message(websocket, data)

				except WebSocketError:
					raise

				finally:
					DBSession.remove()


	except Exception:
		logger.warn("Playfield: uncaught error is occured.", exc_info = True)