|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- encoding: utf-8 -*- |
|
3
|
|
|
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: |
|
4
|
|
|
# Author: Binux<[email protected]> |
|
5
|
|
|
# http://binux.me |
|
6
|
|
|
# Created on 2014-02-22 23:17:13 |
|
7
|
|
|
|
|
8
|
|
|
import os |
|
9
|
|
|
import sys |
|
10
|
|
|
import logging |
|
11
|
|
|
logger = logging.getLogger("webui") |
|
12
|
|
|
|
|
13
|
|
|
from six import reraise |
|
14
|
|
|
from six.moves import builtins |
|
15
|
|
|
from six.moves.urllib.parse import urljoin |
|
16
|
|
|
from flask import Flask |
|
17
|
|
|
from pyspider.fetcher import tornado_fetcher |
|
18
|
|
|
|
|
19
|
|
|
if os.name == 'nt': |
|
20
|
|
|
import mimetypes |
|
21
|
|
|
mimetypes.add_type("text/css", ".css", True) |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class QuitableFlask(Flask): |
|
25
|
|
|
"""Add quit() method to Flask object""" |
|
26
|
|
|
|
|
27
|
|
|
@property |
|
28
|
|
|
def logger(self): |
|
29
|
|
|
return logger |
|
30
|
|
|
|
|
31
|
|
|
def run(self, host=None, port=None, debug=None, **options): |
|
32
|
|
|
import tornado.wsgi |
|
33
|
|
|
import tornado.ioloop |
|
34
|
|
|
import tornado.httpserver |
|
35
|
|
|
import tornado.web |
|
36
|
|
|
|
|
37
|
|
|
if host is None: |
|
38
|
|
|
host = '127.0.0.1' |
|
39
|
|
|
if port is None: |
|
40
|
|
|
server_name = self.config['SERVER_NAME'] |
|
41
|
|
|
if server_name and ':' in server_name: |
|
42
|
|
|
port = int(server_name.rsplit(':', 1)[1]) |
|
43
|
|
|
else: |
|
44
|
|
|
port = 5000 |
|
45
|
|
|
if debug is not None: |
|
46
|
|
|
self.debug = bool(debug) |
|
47
|
|
|
|
|
48
|
|
|
hostname = host |
|
49
|
|
|
port = port |
|
50
|
|
|
application = self |
|
51
|
|
|
use_reloader = self.debug |
|
52
|
|
|
use_debugger = self.debug |
|
53
|
|
|
|
|
54
|
|
|
if use_debugger: |
|
55
|
|
|
from werkzeug.debug import DebuggedApplication |
|
56
|
|
|
application = DebuggedApplication(application, True) |
|
57
|
|
|
|
|
58
|
|
|
try: |
|
59
|
|
|
from .webdav import dav_app |
|
60
|
|
|
except ImportError as e: |
|
61
|
|
|
logger.error('WebDav interface not enabled: %r', e) |
|
62
|
|
|
dav_app = None |
|
63
|
|
|
if dav_app: |
|
64
|
|
|
from werkzeug.wsgi import DispatcherMiddleware |
|
65
|
|
|
application = DispatcherMiddleware(application, { |
|
66
|
|
|
'/dav': dav_app |
|
67
|
|
|
}) |
|
68
|
|
|
|
|
69
|
|
|
container = tornado.wsgi.WSGIContainer(application) |
|
70
|
|
|
http_server = tornado.httpserver.HTTPServer(container) |
|
71
|
|
|
http_server.listen(port, hostname) |
|
72
|
|
|
if use_reloader: |
|
73
|
|
|
from tornado import autoreload |
|
74
|
|
|
autoreload.start() |
|
75
|
|
|
|
|
76
|
|
|
self.logger.info('webui running on %s:%s', hostname, port) |
|
77
|
|
|
tornado.ioloop.IOLoop.current().start() |
|
78
|
|
|
|
|
79
|
|
|
def quit(self): |
|
80
|
|
|
import tornado.ioloop |
|
81
|
|
|
|
|
82
|
|
|
tornado.ioloop.IOLoop.current().stop() |
|
83
|
|
|
self.logger.info('webui exiting...') |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
app = QuitableFlask('webui', |
|
87
|
|
|
static_folder=os.path.join(os.path.dirname(__file__), 'static'), |
|
88
|
|
|
template_folder=os.path.join(os.path.dirname(__file__), 'templates')) |
|
89
|
|
|
app.secret_key = os.urandom(24) |
|
90
|
|
|
app.jinja_env.line_statement_prefix = '#' |
|
91
|
|
|
app.jinja_env.globals.update(builtins.__dict__) |
|
92
|
|
|
|
|
93
|
|
|
app.config.update({ |
|
94
|
|
|
'fetch': lambda x: tornado_fetcher.Fetcher(None, None, async=False).fetch(x)[1], |
|
95
|
|
|
'taskdb': None, |
|
96
|
|
|
'projectdb': None, |
|
97
|
|
|
'scheduler_rpc': None, |
|
98
|
|
|
'queues': dict(), |
|
99
|
|
|
}) |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
def cdn_url_handler(error, endpoint, kwargs): |
|
103
|
|
|
if endpoint == 'cdn': |
|
104
|
|
|
path = kwargs.pop('path') |
|
105
|
|
|
# cdn = app.config.get('cdn', 'http://cdn.staticfile.org/') |
|
106
|
|
|
# cdn = app.config.get('cdn', '//cdnjs.cloudflare.com/ajax/libs/') |
|
107
|
|
|
cdn = app.config.get('cdn', '//cdnjscn.b0.upaiyun.com/libs/') |
|
108
|
|
|
return urljoin(cdn, path) |
|
109
|
|
|
else: |
|
110
|
|
|
exc_type, exc_value, tb = sys.exc_info() |
|
111
|
|
|
if exc_value is error: |
|
112
|
|
|
reraise(exc_type, exc_value, tb) |
|
113
|
|
|
else: |
|
114
|
|
|
raise error |
|
115
|
|
|
app.handle_url_build_error = cdn_url_handler |
|
116
|
|
|
|