hmdooris/hmdooris/__main__.py
Stefan Bethke f49cb3d6f3
All checks were successful
docker-image / docker (push) Successful in 9m31s
Fix ip checks
2025-05-29 15:44:21 +02:00

92 lines
2.6 KiB
Python

"""
FastAPI main entry point
"""
import json
import logging
from typing import Callable
from BottleOIDC import BottleOIDC
from BottleOIDC.bottle_utils import UnauthorizedError
from BottleSessions import BottleSessions
from bottle import route, run, Bottle, static_file, TEMPLATE_PATH, jinja2_view, post, get, request
from bottle_log import LoggingPlugin
from bottle_websocket import websocket, GeventWebSocketServer
from geventwebsocket.websocket import WebSocket
from hmdooris.AppConfig import AppConfig
from hmdooris.BottleHelpers import BottleHelpers
from hmdooris.ccujack import CCUJack
from hmdooris.updatepoller import UpdatePoller
from hmdooris.websocketcomm import WebSocketClients
config = AppConfig()
if config.debug:
logging.basicConfig(level=logging.DEBUG)
ccujack = CCUJack(config.ccujack_url, config.ccujack_certificate_path, config.ccujack_username, config.ccujack_password)
app = Bottle()
if config.debug:
app.config.update({"logging.level": "DEBUG"})
app.install(LoggingPlugin(app.config))
TEMPLATE_PATH.insert(0, config.templatepath)
app.install(BottleSessions())
auth = BottleOIDC(app, config={
"discovery_url": config.discovery_url,
"client_id": config.client_id,
"client_secret": config.client_secret,
"client_scope": config.oidc_scope,
"user_attr": config.oidc_user_attr,
})
websocket_clients = WebSocketClients()
bottle_helpers = BottleHelpers(auth, group=config.requires_group, allowed=config.allowed)
update_poller = UpdatePoller(websocket_clients, ccujack, 1 if config.debug else 0.1)
@app.route("/static/<filepath>")
def server_static(filepath):
return static_file(filepath, root=config.staticpath)
@app.get("/")
@jinja2_view("home.html.j2")
def root():
return {}
@app.get("/operate")
@bottle_helpers.require_login
@bottle_helpers.require_sourceip
@jinja2_view("operate.html.j2")
def root():
return {}
@app.get('/ws', apply=[websocket])
def websocket_endpoint(ws: WebSocket):
try:
websocket_clients.add(ws)
ws.send(json.dumps(update_poller.get_locks(True)))
while True:
m = ws.receive()
except Exception as e:
logging.debug("error in websocket", exc_info=e)
pass
finally:
websocket_clients.remove(ws)
@app.get('/api/lock')
def get_api_lock():
return update_poller.get_locks(True)
@app.get('/api/lock/<id>')
def get_api_lock(id):
return update_poller.get_lock(id)
@app.post('/api/lock/<id>')
@bottle_helpers.require_authz
def post_api_lock(id):
return ccujack.lock_unlock(id, request.json["locking"])
if __name__ == '__main__':
app.run(host='localhost', port=3000, server=GeventWebSocketServer, debug=config.debug, quiet=not config.debug)