From d0704be9edb02f9a70403c4fb298d3750e487ba3 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Thu, 29 Feb 2024 03:01:41 +0100 Subject: Flask server and endpoints --- server/.gitignore | 3 +++ server/requirements.txt | 9 +++++++++ server/server.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 server/.gitignore create mode 100644 server/requirements.txt create mode 100644 server/server.py (limited to 'server') diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..ee18991 --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,3 @@ +venv/ +*.pyc +__pycache__/ diff --git a/server/requirements.txt b/server/requirements.txt new file mode 100644 index 0000000..80ea565 --- /dev/null +++ b/server/requirements.txt @@ -0,0 +1,9 @@ +click==7.1.2 +Flask==1.1.2 +Flask-Cors==3.0.8 +itsdangerous==1.1.0 +Jinja2==2.11.2 +MarkupSafe==1.1.1 +pytz==2020.1 +six==1.15.0 +Werkzeug==1.0.1 diff --git a/server/server.py b/server/server.py new file mode 100644 index 0000000..f1ddaa9 --- /dev/null +++ b/server/server.py @@ -0,0 +1,28 @@ +from datetime import datetime +from flask import abort, Flask, jsonify +from flask_cors import CORS +from pytz import common_timezones, timezone + + +app = Flask(__name__) +CORS(app) + + +@app.route('/zones/', methods=['GET']) +def zones(): + """ Return a list of common timezones """ + return jsonify(common_timezones) + + +@app.route('/time-at/', methods=['GET']) +def time_at(zone): + """ Return current time at given timezone """ + if zone not in common_timezones: + abort(404) + + tzobj = timezone(zone) + + return jsonify({ + 'zone': tzobj.zone, + 'time': datetime.now(tzobj).strftime('%a %d %b %Y %H:%M:%S'), + }) -- cgit v1.2.1