Skip to content
Snippets Groups Projects
geant_acme_web.py 1.85 KiB
#!/usr/bin/env python3
"""
    run flask CGI that will trigger cert2json.py -p <provider>
"""
import subprocess as sp
from flask import Flask
from flask import request
from flask import render_template
from flask import send_from_directory

APP = Flask(__name__, root_path='/var/www/acme_web', template_folder='/var/www/acme_web',
            static_folder='static', static_url_path='/var/www/acme_web')
BASE_URLS = ['/sectigo_ev', '/sectigo_ov', '/letsencrypt',
             '/sectigo_ev/', '/sectigo_ov/', '/letsencrypt/',
             '/sectigo_ev/index.html', '/sectigo_ov/index.html',
             '/letsencrypt/index.html']
EXPIRED_URLS = ['/sectigo_ev/expiry_date.html', '/sectigo_ov/expiry_date.html',
                '/letsencrypt/expiry_date.html']


def create_json(provider):
    """ runs cert2json """
    script_cmd = ["cert2json.sh", provider]
    print(script_cmd)
    sp.Popen(script_cmd, stdout=sp.PIPE, stderr=sp.PIPE)


@APP.route('/<provider>')
@APP.route('/static/<filename>')
@APP.route('/')
def index(provider=None, filename=None):
    """ check the git environment and run g10k """
    url_name = str(request.path)
    print("provider is {}".format(provider))

    if url_name == '/':
        return render_template("index.html")

    if url_name.startswith("/static") or url_name.startswith("static"):
        return send_from_directory(APP.static_folder, filename)

    if url_name in BASE_URLS:
        stripped_url = url_name.replace("/", "")
        create_json(stripped_url)
        return render_template("{}/index.html".format(stripped_url))

    if url_name in EXPIRED_URLS:
        stripped_url = url_name.split("/")[0]
        create_json(stripped_url)
        return render_template("{}/expiry_date.html".format(stripped_url))

    return "Oh no! NOT Found", 404


if __name__ == '__main__':

    APP.run(debug=False, threaded=True, host='::', port=8000)