Files
lrc-backend/backend/serve_frontend.py

77 lines
2.5 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import flask
from flask.json import dump
from jose import jwt, jwk
import os
from flask import render_template, send_from_directory, Blueprint, jsonify, url_for
from flask_pyoidc.user_session import UserSession
from backend import app
from backend.auth import oidc_auth
fe_path = os.path.abspath(os.path.join(app.root_path, os.pardir, os.pardir, "frontend", "dist"))
if not os.path.exists(fe_path) or not os.path.exists(os.path.join(fe_path, "index.html")):
app.logger.critical(
"Frontend path and/or index.html does not exist! Please build frontend before continuing! "
"You might want to go to ../frontend and continue from there.")
exit()
fe_bp = Blueprint('frontend', __name__, url_prefix='/', template_folder=os.path.join(fe_path, ""))
@fe_bp.route('/js/<path:path>')
def send_js(path):
return send_from_directory(os.path.join(fe_path, "js"), path)
@fe_bp.route('/css/<path:path>')
def send_css(path):
return send_from_directory(os.path.join(fe_path, "css"), path)
@fe_bp.route('/img/<path:path>')
def send_img(path):
return send_from_directory(os.path.join(fe_path, "img"), path)
@fe_bp.route('/test')
@oidc_auth.oidc_auth()
def test_oidc():
user_session = UserSession(flask.session)
access_token = user_session.access_token
token_claim = jwt.get_unverified_claims(access_token)
token_header = jwt.get_unverified_header(access_token)
return jsonify(id_token=flask.session['id_token'], access_token=flask.session['access_token'],
userinfo=flask.session['userinfo'],
token_claim=token_claim,
token_header=token_header)
def has_no_empty_params(rule):
defaults = rule.defaults if rule.defaults is not None else ()
arguments = rule.arguments if rule.arguments is not None else ()
return len(defaults) >= len(arguments)
@fe_bp.route("/site-map")
def site_map():
print("# serving site-map!!")
links = []
for rule in app.url_map.iter_rules():
# Filter out rules we can't navigate to in a browser
# and rules that require parameters
if has_no_empty_params(rule):
# if "GET" in rule.methods and has_no_empty_params(rule):
url = url_for(rule.endpoint, **(rule.defaults or {}))
links.append((url, rule.endpoint))
# links is now a list of url, endpoint tuples
# dump(links)
return jsonify(links)
@fe_bp.route('/', defaults={'path': ''})
@fe_bp.route('/<path:path>')
def catch_all(path):
return render_template("index.html")