generated from jCloud/repository-template
Add middleware to check whether Gitea is enabled
This commit is contained in:
@@ -0,0 +1,60 @@
|
|||||||
|
# Copyright 2026 jCloud
|
||||||
|
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import jcloud_config_parser
|
||||||
|
from typing import Optional
|
||||||
|
from fastapi import FastAPI, Request
|
||||||
|
from starlette.middleware.base import BaseHTTPMiddleware
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'GiteaCheckEnabledMiddleware'
|
||||||
|
]
|
||||||
|
|
||||||
|
class GiteaCheckEnabledMiddleware(BaseHTTPMiddleware):
|
||||||
|
'''
|
||||||
|
A middleware to check whether Gitea webhooks are enabled.
|
||||||
|
|
||||||
|
:param app: The (FastAPI) app.
|
||||||
|
:param configuration: The configuration.
|
||||||
|
:type configuration: jcloud_config_parser.ini.INIConfiguration
|
||||||
|
:param logger: The logger.
|
||||||
|
:type logger: Optional[logging.Logger]
|
||||||
|
'''
|
||||||
|
def __init__(self, app, configuration: jcloud_config_parser.ini.INIConfiguration, logger: Optional[logging.Logger] = None) -> None:
|
||||||
|
super().__init__(app)
|
||||||
|
self.fastapi_app = app
|
||||||
|
self.configuration = configuration
|
||||||
|
self.logger = logger
|
||||||
|
|
||||||
|
async def dispatch(self, request: Request, call_next):
|
||||||
|
'''
|
||||||
|
Dispatch.
|
||||||
|
|
||||||
|
:param request: The request.
|
||||||
|
:type request: Request
|
||||||
|
'''
|
||||||
|
|
||||||
|
if request.url.path.startswith('/gitea/'):
|
||||||
|
|
||||||
|
if self.configuration.gitea.enabled not in ('true', 'yes', 't', 'y'):
|
||||||
|
if self.logger is not None:
|
||||||
|
self.logger.error(f'{request.url.path}: Gitea is disabled')
|
||||||
|
return JSONResponse(
|
||||||
|
status_code = 403,
|
||||||
|
content = {'detail': 'Gitea disabled'}
|
||||||
|
)
|
||||||
|
|
||||||
|
return await call_next(request)
|
||||||
Reference in New Issue
Block a user