feat: major rewrite of the webserver
gets rid of __init__ and runserver in favor of new modular design also introduces db model, first api endpoints, as well as their wrappers
This commit is contained in:
44
FlaskWebProject/FlaskWebProject/lewy_api_v1.py
Normal file
44
FlaskWebProject/FlaskWebProject/lewy_api_v1.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# API is expected to return:
|
||||
# - HTTP status code,
|
||||
# - human-readable status message,
|
||||
# - json with appropriate data
|
||||
import flask, json, time
|
||||
import lewy_globals
|
||||
|
||||
def incrementBadRequests():
|
||||
lewy_globals.apiFailedRequests += 1
|
||||
|
||||
def notImplemented(data):
|
||||
# TODO: change list to string -> data, not data[0]
|
||||
return 501, f"not recognised/implemented: {data[0]}", []
|
||||
|
||||
def stub_hello():
|
||||
return 200, 'hello from v1! stats are at /api/v1/stats', []
|
||||
|
||||
def stats():
|
||||
data_to_send = {
|
||||
"start_time": lewy_globals.starttime,
|
||||
"uptime": lewy_globals.getUptime(),
|
||||
"real_uptime": lewy_globals.realUptime,
|
||||
"total_api_requests": lewy_globals.apiRequests,
|
||||
"failed_api_requests": lewy_globals.apiFailedRequests,
|
||||
"outside_api_requests": lewy_globals.outsideApiHits,
|
||||
"local_api_requests": lewy_globals.apiRequests - lewy_globals.outsideApiHits
|
||||
}
|
||||
return 200, "OK", data_to_send
|
||||
|
||||
|
||||
|
||||
def lookup(data):
|
||||
if data == []:
|
||||
return stub_hello()
|
||||
match data[0].lower():
|
||||
case 'stats' | '':
|
||||
return stats()
|
||||
case 'user':
|
||||
return stub_hello()
|
||||
case 'info':
|
||||
return stub_hello()
|
||||
case _:
|
||||
incrementBadRequests()
|
||||
return notImplemented(data)
|
||||
Reference in New Issue
Block a user