|
1 | | -import os |
2 | | -import cherrypy |
3 | | -from cherrypy import wsgiserver |
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
4 | 3 |
|
5 | | -from ptb_website import wsgi, settings |
| 4 | +import os |
| 5 | +import logging |
6 | 6 |
|
7 | | -from httplogger import HTTPLogger |
| 7 | +os.environ["DJANGO_SETTINGS_MODULE"] = "ptb_website.settings" |
8 | 8 |
|
9 | | -PATH = os.path.abspath(os.path.dirname(__file__)) |
| 9 | +import cherrypy |
| 10 | +import django |
| 11 | +django.setup() |
10 | 12 |
|
| 13 | +from django.conf import settings |
| 14 | +from django.core.handlers.wsgi import WSGIHandler |
| 15 | +from paste.translogger import TransLogger |
11 | 16 |
|
12 | | -class Root(object): |
13 | | - pass |
14 | | - |
15 | | -def make_static_config(static_dir_name): |
16 | | - """ |
17 | | - All custom static configurations are set here, since most are common, it |
18 | | - makes sense to generate them just once. |
19 | | - """ |
20 | | - static_path = os.path.join('/', static_dir_name) |
21 | | - path = os.path.join(PATH, static_dir_name) |
22 | | - configuration = {static_path: { |
23 | | - 'tools.staticdir.on': True, |
24 | | - 'tools.staticdir.dir': path} |
25 | | - } |
26 | | - |
27 | | - return cherrypy.tree.mount(Root(), '/', config=configuration) |
28 | | - |
29 | | -application = wsgiserver.WSGIPathInfoDispatcher( |
30 | | -{ |
31 | | - '/': wsgi.application, |
32 | | - settings.STATIC_URL[:-1]: make_static_config(settings.STATIC_URL[1:-1]), |
33 | | - settings.MEDIA_URL[:-1]: make_static_config(settings.MEDIA_URL[1:-1]), |
34 | | - '/.well-known': make_static_config('.well-known'), # Host files for letsencrypt |
35 | | -}) |
36 | | - |
37 | | -cherrypy.config.update({'environment': 'production', |
38 | | - 'log.error_file': 'site.log', |
39 | | - 'log.screen': True}) |
40 | | - |
41 | | -server = wsgiserver.CherryPyWSGIServer(('127.0.0.1', 8001), HTTPLogger(application), |
42 | | - server_name='python-telegram-bot.org') |
43 | | -try: |
44 | | - server.start() |
45 | | -except KeyboardInterrupt: |
46 | | - print("Terminating server...") |
47 | | - server.stop() |
| 17 | +PATH = os.path.abspath(os.path.dirname(__file__)) |
48 | 18 |
|
| 19 | +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 20 | + level=logging.INFO) |
| 21 | + |
| 22 | + |
| 23 | +class WebsiteApplication(object): |
| 24 | + HOST = "127.0.0.1" |
| 25 | + PORT = 8001 |
| 26 | + |
| 27 | + def mount_static(self, url, root): |
| 28 | + """ |
| 29 | + :param url: Relative url |
| 30 | + :param root: Path to static files root |
| 31 | + """ |
| 32 | + config = { |
| 33 | + 'tools.staticdir.on': True, |
| 34 | + 'tools.staticdir.dir': root, |
| 35 | + 'tools.expires.on': True, |
| 36 | + 'tools.expires.secs': 86400 |
| 37 | + } |
| 38 | + cherrypy.tree.mount(None, url, {'/': config}) |
| 39 | + |
| 40 | + def run(self): |
| 41 | + cherrypy.config.update({ |
| 42 | + 'environment': 'production', |
| 43 | + 'server.socket_host': self.HOST, |
| 44 | + 'server.socket_port': self.PORT, |
| 45 | + 'engine.autoreload_on': False, |
| 46 | + 'log.error_file': 'site.log', |
| 47 | + 'log.screen': True |
| 48 | + }) |
| 49 | + self.mount_static(settings.STATIC_URL, settings.STATIC_ROOT) |
| 50 | + self.mount_static('/.well-known', os.path.join(PATH, '.well-known')) |
| 51 | + |
| 52 | + cherrypy.log("Loading and serving Django application on /") |
| 53 | + cherrypy.tree.graft(TransLogger(WSGIHandler()), '/') |
| 54 | + cherrypy.engine.start() |
| 55 | + cherrypy.log("Your app is running at http://%s:%s" % (self.HOST, self.PORT)) |
| 56 | + |
| 57 | + cherrypy.engine.block() |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + WebsiteApplication().run() |
0 commit comments