Skip to content

Commit

Permalink
Add option to specify URL prefix for sqlite-web application.
Browse files Browse the repository at this point in the history
Refs #48
  • Loading branch information
coleifer committed Aug 8, 2018
1 parent 09f971f commit 62be325
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The following options are available:
* ``-x``, ``--no-browser``: do not open a web-browser when sqlite-web starts.
* ``-P``, ``--password``: prompt for password to access sqlite-web.
* ``-r``, ``--read-only``: open database in read-only mode.
* ``-u``, ``--url-prefix``: URL prefix for application, e.g. "/sqlite-web".

### Using docker

Expand Down
36 changes: 30 additions & 6 deletions sqlite_web/sqlite_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def login():
if request.method == 'POST':
if request.form.get('password') == app.config['PASSWORD']:
session['authorized'] = True
return redirect(session.get('next_url') or '/')
return redirect(session.get('next_url') or url_for('index'))
flash('The password you entered is incorrect.', 'danger')
return render_template('login.html')

Expand Down Expand Up @@ -674,6 +674,22 @@ def _close_db(exc):
if not dataset._database.is_closed():
dataset.close()


class PrefixMiddleware(object):
def __init__(self, app, prefix):
self.app = app
self.prefix = '/%s' % prefix.strip('/')
self.prefix_len = len(self.prefix)

def __call__(self, environ, start_response):
if environ['PATH_INFO'].startswith(self.prefix):
environ['PATH_INFO'] = environ['PATH_INFO'][self.prefix_len:]
environ['SCRIPT_NAME'] = self.prefix
return self.app(environ, start_response)
else:
start_response('404', [('Content-Type', 'text/plain')])
return ['URL does not match application prefix.'.encode()]

#
# Script options.
#
Expand Down Expand Up @@ -715,6 +731,11 @@ def get_option_parser():
action='store_true',
dest='read_only',
help='Open database in read-only mode.')
parser.add_option(
'-u',
'--url-prefix',
dest='url_prefix',
help='URL prefix for application.')
return parser

def die(msg, exit_code=1):
Expand All @@ -733,23 +754,23 @@ def _open_tab(url):
thread.daemon = True
thread.start()

def install_auth_handler(password):
def install_auth_handler(password, url_prefix=None):
app.config['PASSWORD'] = password

@app.before_request
def check_password():
if not session.get('authorized') and request.path != '/login/' and \
not request.path.startswith(('/static/', '/favicon')):
flash('You must log-in to view the database browser.', 'danger')
session['next_url'] = request.path
session['next_url'] = request.base_url
return redirect(url_for('login'))

def initialize_app(filename, read_only=False, password=None):
def initialize_app(filename, read_only=False, password=None, url_prefix=None):
global dataset
global migrator

if password:
install_auth_handler(password)
install_auth_handler(password, url_prefix)

if read_only:
if sys.version_info < (3, 4, 0):
Expand All @@ -767,6 +788,9 @@ def initialize_app(filename, read_only=False, password=None):
else:
dataset = SqliteDataSet('sqlite:///%s' % filename, bare_fields=True)

if url_prefix:
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=url_prefix)

migrator = dataset._migrator
dataset.close()

Expand All @@ -788,7 +812,7 @@ def main():
break

# Initialize the dataset instance and (optionally) authentication handler.
initialize_app(args[0], options.read_only, password)
initialize_app(args[0], options.read_only, password, options.url_prefix)

if options.browser:
open_browser_tab(options.host, options.port)
Expand Down

0 comments on commit 62be325

Please sign in to comment.