generated from jCloud/repository-template
Add Gitea API router
This commit is contained in:
@@ -12,16 +12,18 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Header
|
||||
import os
|
||||
import uvicorn
|
||||
import ipaddress
|
||||
import jcloud_config_parser
|
||||
import logging
|
||||
from .arguments import parse_args
|
||||
from .config import load_config, process_host_and_port
|
||||
from .config import load_config, process_host_and_port, process_gitea_config
|
||||
from .logging import setup_logging
|
||||
from ..integrations.gitea.middlewares.signature import GiteaSignatureMiddleware
|
||||
from ..integrations.gitea.middlewares.check_enabled import GiteaCheckEnabledMiddleware
|
||||
from ..integrations.gitea.api.webhooks import make_router
|
||||
import sys
|
||||
|
||||
def main():
|
||||
@@ -38,6 +40,8 @@ def main():
|
||||
logger_info.logger
|
||||
)
|
||||
|
||||
gitea_config = process_gitea_config(configuration, logger_info.logger)
|
||||
|
||||
# Initialize FastAPI
|
||||
app = FastAPI(
|
||||
docs_url=configuration.docs.swagger,
|
||||
@@ -45,6 +49,20 @@ def main():
|
||||
openapi_url=configuration.docs.openapi
|
||||
)
|
||||
|
||||
if gitea_config.webhook_secret_file_path is not None:
|
||||
app.add_middleware(
|
||||
GiteaSignatureMiddleware,
|
||||
secret = gitea_config.webhook_secret_file_path.read_bytes(),
|
||||
logger = logger_info.logger
|
||||
) # empty secret is an example value!
|
||||
app.add_middleware(
|
||||
GiteaCheckEnabledMiddleware,
|
||||
configuration = configuration,
|
||||
logger = logger_info.logger
|
||||
)
|
||||
|
||||
app.include_router(make_router(gitea_config.webhook_secret_file_path is not None))
|
||||
|
||||
uvicorn.run(
|
||||
app,
|
||||
host = host,
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
# 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.
|
||||
|
||||
from fastapi import APIRouter, Header
|
||||
from ..models.release import Release
|
||||
|
||||
def make_router(secret_enabled: bool = False) -> APIRouter:
|
||||
'''
|
||||
Returns the Gitea webhook router.
|
||||
|
||||
:param secret_enabled: Whether the secret is enabled.
|
||||
:type secret_enabled: bool
|
||||
|
||||
:return: The router.
|
||||
:rtype: APIRouter
|
||||
'''
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
if secret_enabled:
|
||||
x_gitea_signature_header_default = ...
|
||||
else:
|
||||
x_gitea_signature_header_default = None
|
||||
|
||||
@router.post('/gitea/webhook')
|
||||
async def gitea_webhook(release: Release, x_gitea_signature = Header(x_gitea_signature_header_default)):
|
||||
'''
|
||||
Processes a Gitea webhook.
|
||||
|
||||
Works only if Gitea is enabled.
|
||||
'''
|
||||
|
||||
raise NotImplementedError()
|
||||
|
||||
return router
|
||||
Reference in New Issue
Block a user