From 44f11196624c33057cd4d5f32ce01e6532cb95da Mon Sep 17 00:00:00 2001 From: chang-ning Date: Sat, 28 Jan 2017 22:37:45 +0800 Subject: [PATCH] Add letsencrypt auto renew api :octocat: --- app.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 22a05185..8437a40d 100644 --- a/app.py +++ b/app.py @@ -9,19 +9,39 @@ DIR = os.path.dirname(os.path.realpath(__file__)) ROOT = os.path.join(DIR, 'docs', '_build', 'html') +def find_key(token): + if token == os.environ.get("ACME_TOKEN"): + return os.environ.get("ACME_KEY") + for k, v in os.environ.items(): + if v == token and k.startswith("ACME_TOKEN_"): + n = k.replace("ACME_TOKEN_", "") + return os.environ.get("ACME_KEY_{}".format(n)) + + app = Flask(__name__) + if 'DYNO' in os.environ: - sslify = SSLify(app) + sslify = SSLify(app, skips=['.well-known']) + @app.route('/') def static_proxy(path): """Static files proxy""" return send_from_directory(ROOT, path) + @app.route('/') def index_redirection(): """Redirecting index file""" return send_from_directory(ROOT, 'index.html') + +@app.route("/.well-known/acme-challenge/") +def acme(token): + key = find_key(token) + if key is None: abort(404) + return key + + if __name__ == "__main__": app.run(debug=True)