All checks were successful
Build Container / Build Container (push) Successful in 1m32s
105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
import os
|
|
import logging
|
|
from contextvars import ContextVar
|
|
from argparse import ArgumentParser, Namespace
|
|
|
|
|
|
app_config: ContextVar[Namespace] = ContextVar("app_config")
|
|
|
|
|
|
def main():
|
|
argp = ArgumentParser(
|
|
"dooris-api",
|
|
description="Server to interact with CCCHH doors via locks over CCUJACK over HomeMatic over WiFi",
|
|
)
|
|
argp.add_argument(
|
|
"--openid-issuer",
|
|
required="DOORIS_OPENID_ISSUER" not in os.environ,
|
|
default=os.environ.get("DOORIS_OPENID_ISSUER", None),
|
|
help="The Keycloak OpenID isser to use for authentication",
|
|
)
|
|
argp.add_argument(
|
|
"--openid-scope",
|
|
default=os.environ.get("DOORIS_OPENID_SCOPE", "openid profile"),
|
|
help="The Keycloak OpenID isser to use for authentication",
|
|
)
|
|
argp.add_argument(
|
|
"--openid-client-id",
|
|
required="DOORIS_OPENID_CLIENT_ID" not in os.environ,
|
|
default=os.environ.get("DOORIS_OPENID_CLIENT_ID", None),
|
|
help="The Keycloak OpenID client-id to use for authentication",
|
|
)
|
|
argp.add_argument(
|
|
"--openid-client-secret",
|
|
required="DOORIS_OPENID_CLIENT_SECRET" not in os.environ,
|
|
default=os.environ.get("DOORIS_OPENID_CLIENT_SECRET", None),
|
|
help="The Keycloak OpenID client secret to use for authentication",
|
|
)
|
|
argp.add_argument(
|
|
"--base-url",
|
|
required="DOORIS_BASE_URL" not in os.environ,
|
|
default=os.environ.get("DOORIS_BASE_URL", None),
|
|
help="The Base-URL that this application is reachable under",
|
|
)
|
|
argp.add_argument(
|
|
"--serve-static",
|
|
required=False,
|
|
default=os.environ.get("DOORIS_SERVE_STATIC", None),
|
|
help="In addition to the API functionality, serve static files from this path",
|
|
)
|
|
argp.add_argument(
|
|
"--ccujack-url",
|
|
required=False,
|
|
default=os.environ.get(
|
|
"DOORIS_CCUJACK_URL", "https://hmdooris-ccu.ccchh.net:2122"
|
|
),
|
|
help="The URL under which a CCUJACK instance is hosted that actually operates the locks",
|
|
)
|
|
argp.add_argument(
|
|
"--ccujack-mqtt",
|
|
required=False,
|
|
default=os.environ.get("DOORIS_CCUJACK_MQTT", "hmdooris-ccu.ccchh.net:1883"),
|
|
help="The $HOSTNAME:$PORT of the CCUJack embedded MQTT server",
|
|
)
|
|
argp.add_argument(
|
|
"--ccujack-user",
|
|
required="DOORIS_CCUJACK_USER" not in os.environ,
|
|
default=os.environ.get("DOORIS_CCUJACK_USER", None),
|
|
help="The username used to authenticate against the CCUJACK",
|
|
)
|
|
argp.add_argument(
|
|
"--ccujack-password",
|
|
required="DOORIS_CCUJACK_PASSWORD" not in os.environ,
|
|
default=os.environ.get("DOORIS_CCUJACK_PASSWORD", None),
|
|
help="The password used to authenticate against the CCUJACK",
|
|
)
|
|
argp.add_argument(
|
|
"--static-api-tokens",
|
|
required=False,
|
|
nargs=1,
|
|
action="append",
|
|
default=[i for i in os.environ.get("DOORIS_STATIC_API_TOKENS", "").split(",") if bool(i)],
|
|
)
|
|
args = argp.parse_args()
|
|
|
|
# setup logging
|
|
logging.basicConfig(
|
|
level=logging.DEBUG, format="[%(levelname)s] %(filename)s: %(message)s"
|
|
)
|
|
|
|
# setup app
|
|
app_config.set(args)
|
|
import uvicorn
|
|
from dooris_api.app import app
|
|
|
|
if args.serve_static:
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
app.mount(
|
|
"/", StaticFiles(directory=args.serve_static, html=True), name="static"
|
|
)
|
|
|
|
# start webserver
|
|
config = uvicorn.Config(app, port=8000, log_level="debug")
|
|
server = uvicorn.Server(config)
|
|
server.run()
|