2
2
import logging
3
3
import os
4
4
import sys
5
- from urllib .parse import urlunparse , quote as urlquote
5
+ from urllib .parse import quote as urlquote , urlunparse
6
6
7
7
import click
8
8
from flask import Flask , Response , request
12
12
from .web import blueprint
13
13
14
14
15
- def add_basic_auth (blueprint , username , password , realm = ' RQ Dashboard' ):
15
+ def add_basic_auth (blueprint , username , password , realm = " RQ Dashboard" ):
16
16
"""Add HTTP Basic Auth to a blueprint.
17
17
18
18
Note this is only for casual use!
19
19
20
20
"""
21
+
21
22
@blueprint .before_request
22
23
def basic_http_auth (* args , ** kwargs ):
23
24
auth = request .authorization
24
- if (auth is None or auth .password != password or
25
- auth .username != username ):
25
+ if auth is None or auth .password != password or auth .username != username :
26
26
return Response (
27
- ' Please login' ,
27
+ " Please login" ,
28
28
401 ,
29
- {'WWW-Authenticate' : 'Basic realm="{}"' .format (realm )})
29
+ {"WWW-Authenticate" : 'Basic realm="{}"' .format (realm )},
30
+ )
30
31
31
32
32
- def make_flask_app (config , username , password , url_prefix ,
33
- compatibility_mode = True ):
33
+ def make_flask_app (config , username , password , url_prefix , compatibility_mode = True ):
34
34
"""Return Flask app with default configuration and registered blueprint."""
35
35
app = Flask (__name__ )
36
36
@@ -42,8 +42,8 @@ def make_flask_app(config, username, password, url_prefix,
42
42
app .config .from_object (importlib .import_module (config ))
43
43
44
44
# Override from a configuration file in the env variable, if present.
45
- if ' RQ_DASHBOARD_SETTINGS' in os .environ :
46
- app .config .from_envvar (' RQ_DASHBOARD_SETTINGS' )
45
+ if " RQ_DASHBOARD_SETTINGS" in os .environ :
46
+ app .config .from_envvar (" RQ_DASHBOARD_SETTINGS" )
47
47
48
48
# Optionally add basic auth to blueprint and register with app.
49
49
if username :
@@ -55,66 +55,118 @@ def make_flask_app(config, username, password, url_prefix,
55
55
56
56
@click .command ()
57
57
@click .option (
58
- '-b' , '--bind' , default = '0.0.0.0' ,
59
- help = 'IP or hostname on which to bind HTTP server' )
60
- @click .option (
61
- '-p' , '--port' , default = 9181 , type = int ,
62
- help = 'Port on which to bind HTTP server' )
63
- @click .option (
64
- '--url-prefix' , default = '' ,
65
- help = 'URL prefix e.g. for use behind a reverse proxy' )
66
- @click .option (
67
- '--username' , default = None ,
68
- help = 'HTTP Basic Auth username (not used if not set)' )
69
- @click .option (
70
- '--password' , default = None ,
71
- help = 'HTTP Basic Auth password' )
72
- @click .option (
73
- '-c' , '--config' , default = None ,
74
- help = 'Configuration file (Python module on search path)' )
75
- @click .option (
76
- '-H' , '--redis-host' , default = None , hidden = True ,
77
- help = '[DEPRECATED] IP address or hostname of Redis server. Use --redis-url instead' )
78
- @click .option (
79
- '-P' , '--redis-port' , default = None , type = int , hidden = True ,
80
- help = '[DEPRECATED] Port of Redis server. Use --redis-url instead' )
81
- @click .option (
82
- '--redis-password' , default = None , hidden = True ,
83
- help = '[DEPRECATED] Password for Redis server. Use --redis-url instead' )
84
- @click .option (
85
- '-D' , '--redis-database' , default = None , type = int , hidden = True ,
86
- help = '[DEPRECATED] Database of Redis server, Use --redis-url instead' )
87
- @click .option (
88
- '-u' , '--redis-url' , default = None , multiple = True ,
89
- help = 'Redis URL. Can be specified multiple times. Default: redis://127.0.0.1:6379' )
90
- @click .option (
91
- '--redis-sentinels' , default = None , hidden = True ,
92
- help = '[DEPRECATED] List of redis sentinels. Use --redis-url instead' )
93
- @click .option (
94
- '--redis-master-name' , default = None , hidden = True ,
95
- help = '[DEPRECATED] Name of redis master. Only needed when using sentinels. Use --redis-url instead' )
96
- @click .option (
97
- '--poll-interval' , '--interval' , 'poll_interval' , default = None , type = int ,
98
- help = 'Refresh interval in ms' )
99
- @click .option (
100
- '--extra-path' , default = '.' , multiple = True ,
101
- help = 'Append specified directories to sys.path' )
102
- @click .option (
103
- '--web-background' , default = 'black' ,
104
- help = 'Background of the web interface' )
105
- @click .option (
106
- '--delete-jobs' , default = False , help = 'Delete jobs instead of cancel' )
107
- @click .option (
108
- '--debug/--normal' , default = False , help = 'Enter DEBUG mode' )
109
- @click .option (
110
- '-v' , '--verbose' , is_flag = True , default = False , help = 'Enable verbose logging' )
58
+ "-b" ,
59
+ "--bind" ,
60
+ default = "0.0.0.0" ,
61
+ help = "IP or hostname on which to bind HTTP server" ,
62
+ )
63
+ @click .option (
64
+ "-p" , "--port" , default = 9181 , type = int , help = "Port on which to bind HTTP server"
65
+ )
66
+ @click .option (
67
+ "--url-prefix" , default = "" , help = "URL prefix e.g. for use behind a reverse proxy"
68
+ )
69
+ @click .option (
70
+ "--username" , default = None , help = "HTTP Basic Auth username (not used if not set)"
71
+ )
72
+ @click .option ("--password" , default = None , help = "HTTP Basic Auth password" )
73
+ @click .option (
74
+ "-c" ,
75
+ "--config" ,
76
+ default = None ,
77
+ help = "Configuration file (Python module on search path)" ,
78
+ )
79
+ @click .option (
80
+ "-H" ,
81
+ "--redis-host" ,
82
+ default = None ,
83
+ hidden = True ,
84
+ help = "[DEPRECATED] IP address or hostname of Redis server. Use --redis-url instead" ,
85
+ )
86
+ @click .option (
87
+ "-P" ,
88
+ "--redis-port" ,
89
+ default = None ,
90
+ type = int ,
91
+ hidden = True ,
92
+ help = "[DEPRECATED] Port of Redis server. Use --redis-url instead" ,
93
+ )
94
+ @click .option (
95
+ "--redis-password" ,
96
+ default = None ,
97
+ hidden = True ,
98
+ help = "[DEPRECATED] Password for Redis server. Use --redis-url instead" ,
99
+ )
100
+ @click .option (
101
+ "-D" ,
102
+ "--redis-database" ,
103
+ default = None ,
104
+ type = int ,
105
+ hidden = True ,
106
+ help = "[DEPRECATED] Database of Redis server, Use --redis-url instead" ,
107
+ )
108
+ @click .option (
109
+ "-u" ,
110
+ "--redis-url" ,
111
+ default = None ,
112
+ multiple = True ,
113
+ help = "Redis URL. Can be specified multiple times. Default: redis://127.0.0.1:6379" ,
114
+ )
115
+ @click .option (
116
+ "--redis-sentinels" ,
117
+ default = None ,
118
+ hidden = True ,
119
+ help = "[DEPRECATED] List of redis sentinels. Use --redis-url instead" ,
120
+ )
121
+ @click .option (
122
+ "--redis-master-name" ,
123
+ default = None ,
124
+ hidden = True ,
125
+ help = "[DEPRECATED] Name of redis master. Only needed when using sentinels. Use --redis-url instead" ,
126
+ )
127
+ @click .option (
128
+ "--poll-interval" ,
129
+ "--interval" ,
130
+ "poll_interval" ,
131
+ default = None ,
132
+ type = int ,
133
+ help = "Refresh interval in ms" ,
134
+ )
135
+ @click .option (
136
+ "--extra-path" ,
137
+ default = "." ,
138
+ multiple = True ,
139
+ help = "Append specified directories to sys.path" ,
140
+ )
141
+ @click .option (
142
+ "--web-background" , default = "black" , help = "Background of the web interface"
143
+ )
144
+ @click .option ("--delete-jobs" , default = False , help = "Delete jobs instead of cancel" )
145
+ @click .option ("--debug/--normal" , default = False , help = "Enter DEBUG mode" )
146
+ @click .option (
147
+ "-v" , "--verbose" , is_flag = True , default = False , help = "Enable verbose logging"
148
+ )
111
149
def run (
112
- bind , port , url_prefix , username , password ,
113
- config ,
114
- redis_host , redis_port , redis_password , redis_database , redis_url ,
115
- redis_sentinels , redis_master_name ,
116
- poll_interval , extra_path , web_background , debug , delete_jobs ,
117
- verbose ):
150
+ bind ,
151
+ port ,
152
+ url_prefix ,
153
+ username ,
154
+ password ,
155
+ config ,
156
+ redis_host ,
157
+ redis_port ,
158
+ redis_password ,
159
+ redis_database ,
160
+ redis_url ,
161
+ redis_sentinels ,
162
+ redis_master_name ,
163
+ poll_interval ,
164
+ extra_path ,
165
+ web_background ,
166
+ debug ,
167
+ delete_jobs ,
168
+ verbose ,
169
+ ):
118
170
"""Run the RQ Dashboard Flask server.
119
171
120
172
All configuration can be set on the command line or through environment
@@ -129,63 +181,66 @@ def run(
129
181
if extra_path :
130
182
sys .path += list (extra_path )
131
183
132
- click .echo (' RQ Dashboard version {}' .format (VERSION ))
184
+ click .echo (" RQ Dashboard version {}" .format (VERSION ))
133
185
app = make_flask_app (config , username , password , url_prefix )
134
- app .config [' DEPRECATED_OPTIONS' ] = []
186
+ app .config [" DEPRECATED_OPTIONS" ] = []
135
187
if redis_url :
136
- app .config [' RQ_DASHBOARD_REDIS_URL' ] = redis_url
188
+ app .config [" RQ_DASHBOARD_REDIS_URL" ] = redis_url
137
189
else :
138
- app .config [' RQ_DASHBOARD_REDIS_URL' ] = ' redis://127.0.0.1:6379'
190
+ app .config [" RQ_DASHBOARD_REDIS_URL" ] = " redis://127.0.0.1:6379"
139
191
if redis_host :
140
- app .config [' DEPRECATED_OPTIONS' ].append (' --redis-host' )
192
+ app .config [" DEPRECATED_OPTIONS" ].append (" --redis-host" )
141
193
if redis_port :
142
- app .config [' DEPRECATED_OPTIONS' ].append (' --redis-port' )
194
+ app .config [" DEPRECATED_OPTIONS" ].append (" --redis-port" )
143
195
if redis_password :
144
- app .config [' DEPRECATED_OPTIONS' ].append (' --redis-password' )
196
+ app .config [" DEPRECATED_OPTIONS" ].append (" --redis-password" )
145
197
if redis_database :
146
- app .config [' DEPRECATED_OPTIONS' ].append (' --redis-database' )
198
+ app .config [" DEPRECATED_OPTIONS" ].append (" --redis-database" )
147
199
if redis_sentinels :
148
- app .config [' DEPRECATED_OPTIONS' ].append (' --redis-sentinels' )
200
+ app .config [" DEPRECATED_OPTIONS" ].append (" --redis-sentinels" )
149
201
if redis_master_name :
150
- app .config [' DEPRECATED_OPTIONS' ].append (' --redis-master-name' )
202
+ app .config [" DEPRECATED_OPTIONS" ].append (" --redis-master-name" )
151
203
if poll_interval :
152
- app .config [' RQ_DASHBOARD_POLL_INTERVAL' ] = poll_interval
204
+ app .config [" RQ_DASHBOARD_POLL_INTERVAL" ] = poll_interval
153
205
if web_background :
154
206
app .config ["RQ_DASHBOARD_WEB_BACKGROUND" ] = web_background
155
207
if delete_jobs :
156
208
app .config ["RQ_DASHBOARD_DELETE_JOBS" ] = delete_jobs
157
209
# Conditionally disable Flask console messages
158
210
# See: https://stackoverflow.com/questions/14888799
159
- log = logging .getLogger (' werkzeug' )
211
+ log = logging .getLogger (" werkzeug" )
160
212
if verbose :
161
213
log .setLevel (logging .DEBUG )
162
214
else :
163
215
log .setLevel (logging .ERROR )
164
216
log .error (" * Running on {}:{}" .format (bind , port ))
165
217
166
- if app .config [' DEPRECATED_OPTIONS' ] and not redis_url :
218
+ if app .config [" DEPRECATED_OPTIONS" ] and not redis_url :
167
219
# redis+sentinel://[:password@]host:port[,host2:port2,...][/service_name[/db]][?param1=value1[¶m2=value=2&...]]
168
- scheme = ' redis+sentinel' if redis_sentinels else ' redis'
220
+ scheme = " redis+sentinel" if redis_sentinels else " redis"
169
221
if redis_sentinels :
170
222
netloc = redis_sentinels
171
223
else :
172
- netloc = redis_host or ' localhost'
224
+ netloc = redis_host or " localhost"
173
225
if redis_port :
174
- netloc = ' %s:%s' % (netloc , redis_port )
226
+ netloc = " %s:%s" % (netloc , redis_port )
175
227
if redis_password :
176
- netloc = urlquote (redis_password ) + '@' + netloc
177
- path = ''
228
+ netloc = urlquote (redis_password ) + "@" + netloc
229
+ path = ""
178
230
if redis_master_name :
179
- path += ' /%s' % urlquote (redis_master_name )
231
+ path += " /%s" % urlquote (redis_master_name )
180
232
if redis_database :
181
- path += '/%s' % redis_database
182
- url = urlunparse ((scheme , netloc , path , '' , '' , '' ))
183
- log .error ('Use --redis-url=%s configuration option '
184
- 'instead of specifying host, port and other parameters separately' , url )
185
- app .config ['RQ_DASHBOARD_REDIS_URL' ] = url
233
+ path += "/%s" % redis_database
234
+ url = urlunparse ((scheme , netloc , path , "" , "" , "" ))
235
+ log .error (
236
+ "Use --redis-url=%s configuration option "
237
+ "instead of specifying host, port and other parameters separately" ,
238
+ url ,
239
+ )
240
+ app .config ["RQ_DASHBOARD_REDIS_URL" ] = url
186
241
187
242
app .run (host = bind , port = port , debug = debug )
188
243
189
244
190
245
def main ():
191
- run (auto_envvar_prefix = ' RQ_DASHBOARD' )
246
+ run (auto_envvar_prefix = " RQ_DASHBOARD" )
0 commit comments