diff --git a/.github/workflows/build_and_publish.yml b/.github/workflows/build_and_publish.yml index 6694f033..97eb29a0 100644 --- a/.github/workflows/build_and_publish.yml +++ b/.github/workflows/build_and_publish.yml @@ -47,6 +47,8 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + build-args: | + APP_VERSION=${{ github.ref_name }} deploy-testing: name: Deploy Testing @@ -54,7 +56,7 @@ jobs: runs-on: [self-hosted, Linux] environment: name: Testing - url: https://timetable.api.test.profcomff.com/ + url: https://api.test.profcomff.com/timetable env: CONTAITER_NAME: com_profcomff_api_timetable_test permissions: @@ -85,6 +87,7 @@ jobs: --network=web \ --volume com_profcomff_api_timetable_test_static:/app/static \ --env DB_DSN=${{ secrets.DB_DSN }} \ + --env ROOT_PATH='/timetable' \ --env GUNICORN_CMD_ARGS='--log-config logging_test.conf' \ --env GOOGLE_CLIENT_SECRET='${{ secrets.GOOGLE_CLIENT_SECRET }}' \ --env STATIC_PATH=static \ @@ -98,7 +101,7 @@ jobs: runs-on: [self-hosted, Linux] environment: name: Production - url: https://timetable.api.profcomff.com/ + url: https://api.profcomff.com/timetable env: CONTAITER_NAME: com_profcomff_api_timetable permissions: @@ -129,6 +132,7 @@ jobs: --network=web \ --volume com_profcomff_api_timetable_static:/app/static \ --env DB_DSN='${{ secrets.DB_DSN }}' \ + --env ROOT_PATH='/timetable' \ --env ADMIN_SECRET='${{ secrets.ADMIN_SECRET }}' \ --env REDIRECT_URL='https://www.profcomff.com/timetable/google' \ --env GOOGLE_CLIENT_SECRET='${{ secrets.GOOGLE_CLIENT_SECRET }}' \ diff --git a/Dockerfile b/Dockerfile index 50b31378..59809c20 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11 +ARG APP_VERSION=dev +ENV APP_VERSION=${APP_VERSION} ENV APP_NAME=calendar_backend ENV APP_MODULE=${APP_NAME}.routes.base:app diff --git a/calendar_backend/__init__.py b/calendar_backend/__init__.py index e69de29b..fa9c0989 100644 --- a/calendar_backend/__init__.py +++ b/calendar_backend/__init__.py @@ -0,0 +1,4 @@ +import os + + +__version__ = os.getenv('APP_VERSION', 'dev') diff --git a/calendar_backend/routes/base.py b/calendar_backend/routes/base.py index f2f3b5ce..df78e1cd 100644 --- a/calendar_backend/routes/base.py +++ b/calendar_backend/routes/base.py @@ -1,19 +1,22 @@ import logging +from textwrap import dedent import starlette.requests from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from fastapi_sqlalchemy import DBSessionMiddleware +from fastapi.staticfiles import StaticFiles from starlette import status from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint from starlette.requests import Request from starlette.responses import Response -from fastapi.staticfiles import StaticFiles from starlette.types import ASGIApp +from calendar_backend import __version__ from calendar_backend.exceptions import ObjectNotFound, ForbiddenAction, NotEnoughCriteria from calendar_backend.settings import get_settings + from .auth import auth_router from .gcal import gcal from .lecturer import ( @@ -27,30 +30,38 @@ from .room import room_router from .event import event_router, event_comment_router, event_comment_review_router + settings = get_settings() logger = logging.getLogger(__name__) app = FastAPI( - description="""API для работы с календарем физфака. -Пример работы на питоне(Создание Room): -```python -import reqests, json -url=f"https://timetable.api.test.profcomff.com" - -# Авторизация -beaver = requests.post(f"{url}/token", {"username": "...", "password": "..."}) - -# Парсинг ответа -auth_data=json.loads(beaver.content) - -# Создание -create_room = requests.post( - f"{url}/timetable/room", - json={"name": "test", "direction": "South"}, - headers={"Authorization": f"Bearer {auth_data.get('access_token')}"} -) - -``` -""" + title='Сервис мониторинга активности', + description=dedent(""" + API для работы с календарем физфака. + Пример работы на питоне(Создание Room): + ```python + import reqests, json + url=f"https://timetable.api.test.profcomff.com" + + # Авторизация + beaver = requests.post(f"{url}/token", {"username": "...", "password": "..."}) + + # Парсинг ответа + auth_data=json.loads(beaver.content) + + # Создание + create_room = requests.post( + f"{url}/timetable/room", + json={"name": "test", "direction": "South"}, + headers={"Authorization": f"Bearer {auth_data.get('access_token')}"} + ) + ``` + """), + version=__version__, + + # Настраиваем интернет документацию + root_path=settings.ROOT_PATH if __version__ != 'dev' else '/', + docs_url=None if __version__ != 'dev' else '/docs', + redoc_url=None, ) diff --git a/calendar_backend/settings.py b/calendar_backend/settings.py index 392b4d06..5fb4bb38 100644 --- a/calendar_backend/settings.py +++ b/calendar_backend/settings.py @@ -1,3 +1,4 @@ +import os from functools import lru_cache from pydantic import BaseSettings, PostgresDsn, AnyHttpUrl, DirectoryPath, Json @@ -7,6 +8,8 @@ class Settings(BaseSettings): """Application settings""" DB_DSN: PostgresDsn = 'postgresql://postgres@localhost:5432/postgres' + ROOT_PATH: str = '/' + os.getenv('APP_NAME', '') + REDIRECT_URL: AnyHttpUrl = "https://www.profcomff.com" SCOPES: list[str] = [ "https://www.googleapis.com/auth/calendar",