Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Created May 24, 2018 09:58
Show Gist options
  • Save charlesreid1/0a51b8ef01463fca084a61937812e037 to your computer and use it in GitHub Desktop.
Save charlesreid1/0a51b8ef01463fca084a61937812e037 to your computer and use it in GitHub Desktop.
flask for static files - plus index.html when a directory is specified
import os, json
from werkzeug.contrib.fixers import ProxyFix
from flask import Flask, redirect, url_for, send_from_directory
from flask_dance.contrib.github import make_github_blueprint, github
from os.path import join, isfile, isdir
# http://librelist.com/browser/flask/2012/2/22/flask-on-heroku-+-gunicorn-static-files/#109c5d714212f483bcbeecd778c879ad
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_PATH = 'samplesite'
app = Flask(__name__)
#static_folder=STATIC_PATH, #os.path.join(PROJECT_ROOT,STATIC_PATH),
#static_url_path='')
app.wsgi_app = ProxyFix(app.wsgi_app)
app.secret_key = os.environ.get("FLASK_SECRET_KEY", "supersekrit")
app.config["RESULT_STATIC_PATH"] = STATIC_PATH
app.config["GITHUB_OAUTH_CLIENT_ID"] = os.environ.get("GITHUB_OAUTH_CLIENT_ID")
app.config["GITHUB_OAUTH_CLIENT_SECRET"] = os.environ.get("GITHUB_OAUTH_CLIENT_SECRET")
github_bp = make_github_blueprint(
client_id = os.environ.get('GITHUB_OAUTH_CLIENT_ID'),
client_secret = os.environ.get('GITHUB_OAUTH_CLIENT_SECRET'),
scope='read:org')
app.register_blueprint(github_bp, url_prefix="/login")
#@app.route('/')
#def root():
# return send_from_directory(app.config["RESULT_STATIC_PATH"],'index.html')
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
rsp = app.config["RESULT_STATIC_PATH"]
if(isdir(join(rsp,path))):
return send_from_directory(join(rsp,path),'index.html')
elif(isfile(join(rsp,path))):
return send_from_directory(rsp, path)
elif(path==''):
return send_from_directory(rsp, 'index.html')
else:
return send_from_directory(rsp, '404.html')
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment