This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunserver.py
executable file
·150 lines (117 loc) · 4.64 KB
/
runserver.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import logging
import time
import re
import ssl
import requests
from distutils.version import StrictVersion
from threading import Thread, Event
from webhook.app import Webhook
from webhook.utils import (get_args, now)
from time import strftime
class LogFilter(logging.Filter):
def __init__(self, level):
self.level = level
def filter(self, record):
return record.levelno < self.level
# Moved here so logger is configured at load time.
formatter = logging.Formatter('%(asctime)s [%(threadName)18s][%(levelname)8s] %(message)s')
# Redirect messages lower than WARNING to stdout
stdout_hdlr = logging.StreamHandler(sys.stdout)
stdout_hdlr.setFormatter(formatter)
log_filter = LogFilter(logging.WARNING)
stdout_hdlr.addFilter(log_filter)
stdout_hdlr.setLevel(5)
# Redirect messages equal or higher than WARNING to stderr
stderr_hdlr = logging.StreamHandler(sys.stderr)
stderr_hdlr.setFormatter(formatter)
stderr_hdlr.setLevel(logging.WARNING)
log = logging.getLogger()
log.addHandler(stdout_hdlr)
log.addHandler(stderr_hdlr)
# Patch to make exceptions in threads cause an exception.
def install_thread_excepthook():
"""
Workaround for sys.excepthook thread bug
(https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1230540&group_id=5470).
Call once from __main__ before creating any threads.
If using psyco, call psycho.cannotcompile(threading.Thread.run)
since this replaces a new-style class method.
"""
import sys
run_old = Thread.run
def run(*args, **kwargs):
try:
run_old(*args, **kwargs)
except (KeyboardInterrupt, SystemExit):
raise
except Exception:
exc_type, exc_value, exc_trace = sys.exc_info()
# Handle Flask's broken pipe when a client prematurely ends
# the connection.
if str(exc_value) == '[Errno 32] Broken pipe':
pass
else:
log.critical('Unhandled patched exception (%s): "%s".',
exc_type, exc_value)
sys.excepthook(exc_type, exc_value, exc_trace)
Thread.run = run
# Exception handler will log unhandled exceptions.
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
log.error("Uncaught exception", exc_info=(
exc_type, exc_value, exc_traceback))
def main():
# Patch threading to make exceptions catchable.
install_thread_excepthook()
# Make sure exceptions get logged.
sys.excepthook = handle_exception
args = get_args()
set_log_and_verbosity(log)
if (sys.version_info.major == 3):
log.info("Running under Python3")
else:
log.warning("Running under Python2")
sys.exit(1)
# Abort if status name is not valid.
regexp = re.compile('^([\w\s\-.]+)$')
if not regexp.match(args.status_name):
log.critical('Status name contains illegal characters.')
sys.exit(1)
app = Webhook(__name__,root_path=os.path.dirname(os.path.abspath(__file__)))
args.root_path = os.path.dirname(os.path.abspath(__file__))
ssl_context = None
if (args.ssl_certificate and args.ssl_privatekey and
os.path.exists(args.ssl_certificate) and
os.path.exists(args.ssl_privatekey)):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
ssl_context.load_cert_chain(
args.ssl_certificate, args.ssl_privatekey)
log.info('Web server in SSL mode.')
app.run(threaded=True, use_reloader=False, debug=False, host=args.host, port=args.port, ssl_context=ssl_context)
def set_log_and_verbosity(log):
# Always write to log file.
args = get_args()
# Create directory for log files.
if not os.path.exists(args.log_path):
os.mkdir(args.log_path)
if not args.no_file_logs:
filename = os.path.join(args.log_path, args.log_filename)
filelog = logging.FileHandler(filename)
if args.log_wysiwyg:
filelog.setFormatter(logging.Formatter('%(asctime)s [%(threadName)18s][%(module)14s][%(levelname)8s] %(message)s'))
log.addHandler(filelog)
log.setLevel(logging.INFO)
# These are very noisy, let's shush them up a bit.
logging.getLogger('requests').setLevel(logging.WARNING)
logging.getLogger('werkzeug').setLevel(logging.ERROR)
# This sneaky one calls log.warning() on every retry.
urllib3_logger = logging.getLogger(requests.packages.urllib3.__package__)
urllib3_logger.setLevel(logging.ERROR)
if __name__ == '__main__':
main()