Add /log to path so I could use it on server.

This commit is contained in:
Matt
2020-05-19 22:41:10 -05:00
parent 3882f914cd
commit 0a153a50aa
2 changed files with 8 additions and 4 deletions

View File

@ -6,7 +6,7 @@ shows what is done and not done makes it easy to check periodically.
# How? # How?
This tiny Flask API runs on a server (Raspberry Pi, etc.). It has very basic authentication This tiny Python 3 Flask API runs on a server (Raspberry Pi, etc.). It has very basic authentication
and waits for a curl (or similar) GET request with some small amount of data. The data are and waits for a curl (or similar) GET request with some small amount of data. The data are
displayed on a simple web page. displayed on a simple web page.
@ -16,6 +16,10 @@ whatever data you pass.
You can either run with the internal Flask development server or something like gunicorn. You can either run with the internal Flask development server or something like gunicorn.
I am experimenting with virtualenv, for which I am following the instructions at
https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask (this
is also the place from I cribbed the basic application functions).
# Other Use Cases? # Other Use Cases?
I'm sure there are some. I'm sure there are some.

6
app.py
View File

@ -27,7 +27,7 @@ def get_password(username):
def unauthorized(): def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 403) return make_response(jsonify({'error': 'Unauthorized access'}), 403)
@app.route('/new', methods=['POST', 'GET']) @app.route('/log/new', methods=['POST', 'GET'])
@auth.login_required @auth.login_required
def create_status(): def create_status():
# At least try to clean up user-submitted data # At least try to clean up user-submitted data
@ -52,11 +52,11 @@ def create_status():
return jsonify({'status': status}), 201 return jsonify({'status': status}), 201
@app.route('/raw', methods=['GET']) @app.route('/log/raw', methods=['GET'])
def get_statuses_raw(): def get_statuses_raw():
return jsonify({'statuses': statuses[::-1]}) return jsonify({'statuses': statuses[::-1]})
@app.route('/', methods=['GET']) @app.route('/log', methods=['GET'])
def get_statuses(): def get_statuses():
long_agos = [humanfriendly.format_timespan(datetime.datetime.now().timestamp() - status['timestamp']) for status in statuses[::-1]] long_agos = [humanfriendly.format_timespan(datetime.datetime.now().timestamp() - status['timestamp']) for status in statuses[::-1]]
return render_template("index.html", return render_template("index.html",