Compare commits

..

2 Commits

Author SHA1 Message Date
9deba1956f [rankendpoint] Add invocation instructions 2023-12-24 21:35:35 +01:00
1a3a530c7f [rankendpoint] Get DB path and endpoint route from env vars
Also reverted the use of main() that made it incompatible with uwsgi and
flask run.
2023-12-24 20:57:05 +01:00

View File

@ -1,18 +1,33 @@
# This script serves an HTTP endpoint that provides the racing scores.
#
# Example invocation for testing:
# $ RANKENDPOINT_DBPATH=/path/to/database.db RANKENDPOINT_ROUTE=/getranks flask --app rankendpoint.py run
#
# Example invocation in production (behind a properly configured gateway like nginx):
# $ RANKENDPOINT_DBPATH=/path/to/database.db RANKENDPOINT_ROUTE=/getranks uwsgi \
# -s localhost:3031 --manage-script-name --mount /=rankendpoint:app --plugin python3
from flask import Flask, request from flask import Flask, request
app = Flask(__name__) app = Flask(__name__)
import sqlite3 import sqlite3
import sys import sys
import os
header = "SUCCESS" header = "SUCCESS"
def main(db_path): db_path = os.environ.get('RANKENDPOINT_DBPATH')
# Opens database in read-only mode route = os.environ.get('RANKENDPOINT_ROUTE')
# Checking same thread disabled for now, which is fine since we never modify anything
try: if None in (db_path, route):
sys.exit('must set RANKENDPOINT_DBPATH and RANKENDPOINT_ROUTE environment variables')
# Opens database in read-only mode
# Checking same thread disabled for now, which is fine since we never modify anything
try:
db = sqlite3.connect('file:{}?mode=ro'.format(db_path), uri=True, check_same_thread=False) db = sqlite3.connect('file:{}?mode=ro'.format(db_path), uri=True, check_same_thread=False)
cur = db.cursor() cur = db.cursor()
except Exception as ex: except Exception as ex:
print(ex) print(ex)
sys.exit() sys.exit()
@ -85,7 +100,8 @@ def get_score_entries(data, name):
return scores return scores
@app.route('/getranks', methods=['POST']) # route should be something like /getranks
@app.route(f'{route}', methods=['POST'])
def rankings(): def rankings():
#print("PCUID:", request.form['PCUID']) #print("PCUID:", request.form['PCUID'])
#print("EP_ID:", request.form['EP_ID']) #print("EP_ID:", request.form['EP_ID'])
@ -128,9 +144,3 @@ def rankings():
# and send it off! # and send it off!
return header + xmlbody return header + xmlbody
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {} <db file>".format(sys.argv[0]))
else:
main(sys.argv[1])