Package: sag-py-web-common
sag_py_web_commonThis contains samhammer specific and internally used helper functions for web projects.Requirements for code to be added here:It's sag/project specific and not of general/public use, so that it does not make sense to create a individual libIt's nothing private either, that should not be publically on the internetIt has no, or very little dependencies
(because the deps are in all projects using the lib, even if the feaute isn't required)Note: See this as last option and try to create individual libs as much as possible.Installationpip install sag-py-web-commonHow to useDefault routingAll requests to the main route / are redirected to /swagger if nothing specified.fromsag_py_web_common.default_routeimportbuild_default_routeapp:FastAPI=FastAPI(...)app.include_router(build_default_route(ingress_base_path=config.ingress_base_path))ingress_base_path: Empty or a path starting with / if proxy hosting like kubernetes is useddefault_redirect_path: Per default /swagger but can be configured to an alternative routeFiltered access loggingExtends the asgi-logger and adds a log entry for received requests.
Furthermore the requests are filtered, so that health checks don't spam the logs.For requests to be filtered, they need to have the header "healthcheck" with one of these values:
"livenessprobe", "readinessprobe", "startupprobe", "prtg"fromsag_py_web_common.filtered_access_loggerimportFilteredAccessLoggerMiddlewareapp:FastAPI=FastAPI(...)app.add_middleware(FilteredAccessLoggerMiddleware,format="Completed:%(R)s-%(st)s-%(L)s",logger=logging.getLogger("access"),)Also see this page for further configuration details:https://github.com/Kludex/asgi-loggerJson exception handlerPer default fastapi falls back to text responses if there are unknown exceptions.
That's not the desired behaviour for json api's.This handler ensures that a json is returned. It contains the field "detail" with the exception message.fromsag_py_web_common.json_exception_handlerimporthandle_unknown_exceptionapp.add_exception_handler(Exception,handle_unknown_exception)Fo logging any HHTP-Exception use log_exception funktion.fromstarlette.exceptionsimportHTTPExceptionasStarletteHTTPExceptionfromsag_py_web_common.json_exception_handlerimportlog_exceptionapp.add_exception_handler(StarletteHTTPException,log_exception)Json Exception handler uses logger "http_error_logger", what could be use for reporting conceptsHow to start developingWith vscodeJust install vscode with dev containers extension. All required extensions and configurations are prepared automatically.With pycharmInstall latest pycharmInstall pycharm plugin BlackConnectInstall pycharm plugin MypyConfigure the python interpreter/venvpip install requirements-dev.txtpip install black[d]Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed filesCtl+Alt+S => Check Tools => BlackConnect => Trigger on code reformatCtl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)Ctl+Alt+S => Click Tools => Actions on save => Reformat codeRestart pycharmHow to publishUpdate the version in setup.py and commit your changeCreate a tag with the same version numberLet github do the rest