0

I'm using Flask to serve a static folder:

from flask import Flask, send_from_directory
from flask_httpauth import HTTPBasicAuth

app = Flask(__name__,
            static_url_path='',
            static_folder='html_files')
...

@app.route('/')
@auth.login_required
def send_html_files():
    return send_from_directory('html_files', 'main.html')

I used the first example in Flask-HTTPAuth docs in order to add basic authentication to my website. Just a regular username and password is enough for me.

The problem is that the authentication dialog is not showing when the user go directly to http://localhost:5000/a/b/c (it works on http://localhost:5000/)

What is the proper way of doing this? On the other hand, what is the quick and dirty way?

Netanel
  • 459
  • 1
  • 5
  • 17

1 Answers1

1

@app.route('/') matches your root path only.

Try something like this to match every path:

@app.route('/<path:filename>')
@auth.login_required
def send_html_files(filename):  
    return send_from_directory('html_files', filename)
UjinT34
  • 4,784
  • 1
  • 12
  • 26
  • just figured it out by my own, but thanks a lot. BTW, how can I confiugre filename to be nullable so it will also work with localhost:5000/ ? – Netanel Nov 25 '21 at 14:50