Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #coding: utf-8
- import os
- import cherrypy
- from app import application
- from mako.template import Template
- from mako.lookup import TemplateLookup
- class MakoHandler(cherrypy.dispatch.LateParamPageHandler):
- """Callable which sets response.body."""
- def __init__(self, template, next_handler):
- self.template = template
- self.next_handler = next_handler
- def __call__(self):
- env = globals().copy()
- env.update(self.next_handler())
- try:
- return self.template.render(**env)
- except:
- # something went wrong rendering the template
- # this will generate a pretty error page with details
- cherrypy.response.status = "500"
- return exceptions.html_error_template().render()
- class MakoLoader(object):
- def __init__(self):
- self.lookups = {}
- def __call__(self, filename, directories, module_directory=None,
- collection_size=-1):
- # Find the appropriate template lookup.
- key = (tuple(directories), module_directory)
- try:
- lookup = self.lookups[key]
- except KeyError:
- lookup = TemplateLookup(directories=directories,
- module_directory=module_directory,
- collection_size=collection_size,
- output_encoding='utf-8', encoding_errors='replace')
- self.lookups[key] = lookup
- cherrypy.request.lookup = lookup
- # Replace the current handler.
- cherrypy.request.template = t = lookup.get_template(filename)
- cherrypy.request.handler = MakoHandler(t, cherrypy.request.handler)
- main = MakoLoader()
- cherrypy.tools.mako = cherrypy.Tool('on_start_resource', main)
- cherrypy.config.update({'tools.sessions.on':True, 'tools.mako.collection_size' :500, 'tools.mako.directories':"content",'server.socket_host': '0.0.0.0','server.socket_port': 8080,})
- #--------------------------------------
- def main():
- #--------------------------------------
- # Get current directory
- try:
- current_dir = os.path.dirname(os.path.abspath(__file__))
- except:
- current_dir = os.path.dirname(os.path.abspath(sys.executable))
- # disable autoreload and timeout_monitor
- cherrypy.engine.autoreload.unsubscribe()
- cherrypy.engine.timeout_monitor.unsubscribe()
- # Static content config
- static_config = {
- '/': {
- 'tools.staticdir.root': current_dir,
- 'tools.staticdir.on': True,
- 'tools.staticdir.dir': './content',
- 'tools.staticdir.index': 'index.html'
- }
- }
- # Mount static content handler
- root_o = cherrypy.tree.mount(application.Root, '/', static_config)
- # suppress traceback-info
- cherrypy.config.update({'request.show_tracebacks': False})
- cherrypy.config.update({'server.socket_port': 8081}) # port 8080
- # Start server
- cherrypy.engine.start()
- cherrypy.engine.block()
- #--------------------------------------
- if __name__ == '__main__':
- #--------------------------------------
- main()
- # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement