Kdoor functional, split web api into seperate file

This commit is contained in:
Arti Zirk
2020-10-04 17:24:44 +03:00
parent 93fad4f97f
commit d006ba2915
3 changed files with 68 additions and 15 deletions

34
kdoorweb/kdoorweb/api.py Normal file
View File

@@ -0,0 +1,34 @@
from bottle import Bottle, request, response
api = Bottle()
# FIXME: Fix door api auth
def check_api_auth(callback):
def wrapper(*args, **kwargs):
print("check api auth")
if "db" not in kwargs:
request.current_user = None
return callback(*args, **kwargs)
user = None
request.current_user = user
if user:
print(f"logged in as {user['user']}")
print(request.current_user)
return callback(*args, **kwargs)
else:
print("not logged in")
return "Invalid authentication"
return wrapper
# FIXME: db plugin not available yet
api.install(check_api_auth)
@api.route("/")
def index():
return "api v1"
@api.route("/cards")
def api_list_cards(db):
return {"keycards":[dict(card) for card in db.list_all_keycards()]}

View File

@@ -5,6 +5,7 @@ from bottle import Bottle, view, TEMPLATE_PATH, static_file, \
request, redirect, response, HTTPError
from .db import SQLitePlugin
from .api import api
application = app = Bottle()
@@ -49,9 +50,14 @@ def check_auth(callback):
return wrapper
app.install(SQLitePlugin(SQLITE_PATH))
db_plugin = SQLitePlugin(SQLITE_PATH)
app.install(db_plugin)
app.install(check_auth)
api.install(db_plugin)
app.mount("/api/v1", api)
@app.route('/static/<path:path>', skip=[check_auth])
def callback(path):
@@ -152,9 +158,3 @@ def log(db):
@view("doors.html")
def doors(db):
return {"doors":[]}
# FIXME: Add door api auth
@app.route("/api/v1/cards", skip=[check_auth])
def api_list_cards(db):
return {"keycards":[dict(card) for card in db.list_all_keycards()]}