Merge branch 'k8s-integration' into develop
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
app_name: carrramba-encore-rate
|
||||
clear_static_data: false
|
||||
|
||||
http:
|
||||
host: 127.0.0.1
|
||||
port: 8080
|
||||
cert: ./config/cert.pem
|
||||
|
||||
db:
|
||||
name: carrramba-encore-rate
|
||||
host: 127.0.0.1
|
||||
port: 5432
|
||||
driver: postgresql+psycopg
|
||||
user: cer
|
||||
password: cer_password
|
||||
|
||||
cache:
|
||||
enable: true
|
||||
|
||||
tracing:
|
||||
enable: false
|
@@ -1,21 +0,0 @@
|
||||
app_name: carrramba-encore-rate
|
||||
clear_static_data: false
|
||||
|
||||
http:
|
||||
host: 0.0.0.0
|
||||
port: 8080
|
||||
# cert: ./config/cert.pem
|
||||
|
||||
db:
|
||||
name: carrramba-encore-rate
|
||||
host: postgres
|
||||
port: 5432
|
||||
user: cer
|
||||
|
||||
cache:
|
||||
enable: true
|
||||
host: redis
|
||||
# TODO: Add user credentials
|
||||
|
||||
tracing:
|
||||
enable: false
|
@@ -14,7 +14,7 @@ from sqlalchemy.ext.asyncio import (
|
||||
)
|
||||
|
||||
from .base_class import Base
|
||||
from settings import DatabaseSettings
|
||||
from ..settings import DatabaseSettings
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
|
@@ -4,9 +4,9 @@ from fastapi_cache.backends.redis import RedisBackend
|
||||
from redis import asyncio as aioredis
|
||||
from yaml import safe_load
|
||||
|
||||
from db import db
|
||||
from idfm_interface.idfm_interface import IdfmInterface
|
||||
from settings import CacheSettings, Settings
|
||||
from .db import db
|
||||
from .idfm_interface.idfm_interface import IdfmInterface
|
||||
from .settings import CacheSettings, Settings
|
||||
|
||||
|
||||
CONFIG_PATH = environ.get("CONFIG_PATH", "./config.sample.yaml")
|
||||
|
@@ -8,8 +8,8 @@ from msgspec import ValidationError
|
||||
from msgspec.json import Decoder
|
||||
|
||||
from .idfm_types import Destinations as IdfmDestinations, IdfmResponse, IdfmState
|
||||
from db import Database
|
||||
from models import Line, Stop, StopArea
|
||||
from ..db import Database
|
||||
from ..models import Line, Stop, StopArea
|
||||
|
||||
|
||||
class IdfmInterface:
|
||||
|
@@ -1,89 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import uvicorn
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi_cache import FastAPICache
|
||||
from opentelemetry import trace
|
||||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
||||
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
||||
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
|
||||
from opentelemetry.sdk.trace import TracerProvider
|
||||
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
||||
|
||||
from db import db
|
||||
from dependencies import idfm_interface, redis_backend, settings
|
||||
from routers import line, stop
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
FastAPICache.init(redis_backend, prefix="api", enable=settings.cache.enable)
|
||||
|
||||
await db.connect(settings.db, settings.clear_static_data)
|
||||
if settings.clear_static_data:
|
||||
await idfm_interface.startup()
|
||||
|
||||
yield
|
||||
|
||||
await db.disconnect()
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["http://carrramba.adrien.run", "https://carrramba.adrien.run"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["OPTIONS", "GET"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# The cache-control header entry is not managed properly by fastapi-cache:
|
||||
# For now, a request with a cache-control set to no-cache
|
||||
# is interpreted as disabling the use of the server cache.
|
||||
# Cf. Improve Cache-Control header parsing and handling
|
||||
# https://github.com/long2ice/fastapi-cache/issues/144 workaround
|
||||
@app.middleware("http")
|
||||
async def fastapi_cache_issue_144_workaround(request: Request, call_next):
|
||||
entries = request.headers.__dict__["_list"]
|
||||
new_entries = [
|
||||
entry for entry in entries if entry[0].decode().lower() != "cache-control"
|
||||
]
|
||||
|
||||
request.headers.__dict__["_list"] = new_entries
|
||||
|
||||
return await call_next(request)
|
||||
|
||||
|
||||
app.include_router(line.router)
|
||||
app.include_router(stop.router)
|
||||
|
||||
|
||||
if settings.tracing.enable:
|
||||
FastAPIInstrumentor.instrument_app(app)
|
||||
|
||||
trace.set_tracer_provider(
|
||||
TracerProvider(resource=Resource.create({SERVICE_NAME: settings.app_name}))
|
||||
)
|
||||
trace.get_tracer_provider().add_span_processor(
|
||||
BatchSpanProcessor(OTLPSpanExporter())
|
||||
)
|
||||
tracer = trace.get_tracer(settings.app_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
http_settings = settings.http
|
||||
|
||||
config = uvicorn.Config(
|
||||
app=app,
|
||||
host=http_settings.host,
|
||||
port=http_settings.port,
|
||||
ssl_certfile=http_settings.cert,
|
||||
proxy_headers=True,
|
||||
)
|
||||
|
||||
server = uvicorn.Server(config)
|
||||
|
||||
server.run()
|
@@ -14,8 +14,8 @@ from sqlalchemy import (
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship, selectinload
|
||||
from sqlalchemy.sql.expression import tuple_
|
||||
|
||||
from db import Base, db
|
||||
from idfm_interface.idfm_types import (
|
||||
from ..db import Base, db
|
||||
from ..idfm_interface.idfm_types import (
|
||||
IdfmState,
|
||||
IdfmLineState,
|
||||
TransportMode,
|
||||
|
@@ -26,8 +26,8 @@ from sqlalchemy.orm import (
|
||||
from sqlalchemy.schema import Index
|
||||
from sqlalchemy_utils.types.ts_vector import TSVectorType
|
||||
|
||||
from db import Base, db
|
||||
from idfm_interface.idfm_types import TransportMode, IdfmState, StopAreaType
|
||||
from ..db import Base, db
|
||||
from ..idfm_interface.idfm_types import TransportMode, IdfmState, StopAreaType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .line import Line
|
||||
|
@@ -1,7 +1,7 @@
|
||||
from sqlalchemy import BigInteger, ForeignKey, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from db import Base, db
|
||||
from ..db import Base, db
|
||||
from .stop import _Stop
|
||||
|
||||
|
||||
|
@@ -1,8 +1,8 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
from models import Line
|
||||
from schemas import Line as LineSchema, TransportMode
|
||||
from ..models import Line
|
||||
from ..schemas import Line as LineSchema, TransportMode
|
||||
|
||||
|
||||
router = APIRouter(prefix="/line", tags=["line"])
|
||||
|
@@ -5,16 +5,16 @@ from typing import Sequence
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
from idfm_interface import Destinations as IdfmDestinations, TrainStatus
|
||||
from models import Stop, StopArea, StopShape
|
||||
from schemas import (
|
||||
from ..idfm_interface import Destinations as IdfmDestinations, TrainStatus
|
||||
from ..models import Stop, StopArea, StopShape
|
||||
from ..schemas import (
|
||||
NextPassage as NextPassageSchema,
|
||||
NextPassages as NextPassagesSchema,
|
||||
Stop as StopSchema,
|
||||
StopArea as StopAreaSchema,
|
||||
StopShape as StopShapeSchema,
|
||||
)
|
||||
from dependencies import idfm_interface
|
||||
from ..dependencies import idfm_interface
|
||||
|
||||
|
||||
router = APIRouter(prefix="/stop", tags=["stop"])
|
||||
|
@@ -2,7 +2,7 @@ from enum import StrEnum
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from idfm_interface import (
|
||||
from ..idfm_interface import (
|
||||
IdfmLineState,
|
||||
IdfmState,
|
||||
TransportMode as IdfmTransportMode,
|
||||
|
@@ -1,6 +1,6 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
from idfm_interface.idfm_types import TrainStatus
|
||||
from ..idfm_interface.idfm_types import TrainStatus
|
||||
|
||||
|
||||
class NextPassage(BaseModel):
|
||||
|
@@ -1,6 +1,6 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
from idfm_interface import StopAreaType
|
||||
from ..idfm_interface import StopAreaType
|
||||
|
||||
|
||||
class Stop(BaseModel):
|
||||
|
Reference in New Issue
Block a user