Handle database connection vars

This commit is contained in:
Zoe Roux 2025-05-12 12:43:17 +02:00
parent 99b93f0f78
commit c2ca1361fe
No known key found for this signature in database
6 changed files with 17 additions and 194 deletions

View File

@ -20,7 +20,7 @@ IMAGES_PATH=./images
# https://www.postgresql.org/docs/current/libpq-envars.html # https://www.postgresql.org/docs/current/libpq-envars.html
PGUSER=kyoo PGUSER=kyoo
PGPASSWORD=password PGPASSWORD=password
PGDATABASE=kyooDB PGDATABASE=kyoo
PGHOST=postgres PGHOST=postgres
PGPORT=5432 PGPORT=5432
# PGOPTIONS=-c search_path=kyoo,public # PGOPTIONS=-c search_path=kyoo,public

View File

@ -1,188 +0,0 @@
x-transcoder: &transcoder-base
build:
context: ./transcoder
dockerfile: Dockerfile.dev
networks:
default:
aliases:
- transcoder
ports:
- "7666:7666"
restart: on-failure
cpus: 1
env_file:
- ./.env
environment:
- GOCODER_PREFIX=/video
volumes:
- ./transcoder:/app
- ${LIBRARY_ROOT}:/video:ro
- ${CACHE_ROOT}:/cache
- transcoder_metadata:/metadata
services:
front:
build:
context: ./front
dockerfile: Dockerfile.dev
volumes:
- ./front:/app
- /app/.yarn
- /app/node_modules
- /app/apps/mobile/node_modules
- /app/apps/web/.next/
- /app/apps/mobile/.expo/
ports:
- "3000:3000"
- "8081:8081"
restart: on-failure
environment:
- KYOO_URL=${KYOO_URL:-http://api:5000/api}
labels:
- "traefik.enable=true"
- "traefik.http.routers.front.rule=PathPrefix(`/`)"
auth:
build:
context: ./auth
dockerfile: Dockerfile.dev
restart: on-failure
depends_on:
postgres:
condition: service_healthy
ports:
- "4568:4568"
env_file:
- ./.env
environment:
- KEIBI_PREFIX=/auth
volumes:
- ./auth:/app
labels:
- "traefik.enable=true"
- "traefik.http.routers.auth.rule=PathPrefix(`/auth/`) || PathPrefix(`/.well-known/`)"
api:
build:
context: ./api
dockerfile: Dockerfile.dev
restart: on-failure
depends_on:
postgres:
condition: service_healthy
volumes:
- ./api:/app
- /app/node_modules
ports:
- "3567:3567"
environment:
- KYOO_PREFIX=/api
- JWT_ISSUER=${PUBLIC_URL}
env_file:
- ./.env
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=PathPrefix(`/api/`) || PathPrefix(`/swagger`)"
- "traefik.http.routers.api.middlewares=phantom-token"
- "traefik.http.middlewares.phantom-token.forwardauth.address=http://auth:4568/auth/jwt"
- "traefik.http.middlewares.phantom-token.forwardauth.authRequestHeaders=Authorization,X-Api-Key"
- "traefik.http.middlewares.phantom-token.forwardauth.authResponseHeaders=Authorization"
# scanner:
# build: ./scanner
# restart: on-failure
# depends_on:
# back:
# condition: service_healthy
# env_file:
# - ./.env
# environment:
# - KYOO_URL=${KYOO_URL:-http://back:5000/api}
# volumes:
# - ${LIBRARY_ROOT}:/video:ro
#
# matcher:
# build: ./scanner
# command: matcher
# restart: on-failure
# depends_on:
# back:
# condition: service_healthy
# env_file:
# - ./.env
# environment:
# - KYOO_URL=${KYOO_URL:-http://back:5000/api}
transcoder:
<<: *transcoder-base
profiles: ['', 'cpu']
transcoder-nvidia:
<<: *transcoder-base
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]
driver: cdi
device_ids:
- nvidia.com/gpu=all
environment:
- GOCODER_PREFIX=/video
- GOCODER_HWACCEL=nvidia
profiles: ['nvidia']
transcoder-vaapi:
<<: *transcoder-base
devices:
- /dev/dri:/dev/dri
environment:
- GOCODER_PREFIX=/video
- GOCODER_HWACCEL=vaapi
- GOCODER_VAAPI_RENDERER=${GOCODER_VAAPI_RENDERER:-/dev/dri/renderD128}
profiles: ['vaapi']
# qsv is the same setup as vaapi but with the hwaccel env var different
transcoder-qsv:
<<: *transcoder-base
devices:
- /dev/dri:/dev/dri
environment:
- GOCODER_PREFIX=/video
- GOCODER_HWACCEL=qsv
- GOCODER_VAAPI_RENDERER=${GOCODER_VAAPI_RENDERER:-/dev/dri/renderD128}
profiles: ['qsv']
traefik:
image: traefik:v3.4
restart: on-failure
command:
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entryPoints.web.address=:8901"
- "--accesslog=true"
ports:
- "8901:8901"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
postgres:
image: postgres:15
restart: on-failure
env_file:
- ./.env
volumes:
- db:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
command: ["postgres", "-c", "log_statement=all"]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 5
volumes:
db:
transcoder_metadata:

View File

@ -104,6 +104,8 @@ services:
- "4389:4389" - "4389:4389"
environment: environment:
- KYOO_URL=${KYOO_URL:-http://api:3567/api} - KYOO_URL=${KYOO_URL:-http://api:3567/api}
- JWKS_URL="http://auth:4568/.well-known/jwks.json"
- JWT_ISSUER=${PUBLIC_URL}
volumes: volumes:
- ./scanner:/app - ./scanner:/app
- ${LIBRARY_ROOT}:/video:ro - ${LIBRARY_ROOT}:/video:ro

View File

@ -20,6 +20,6 @@ JWT_ISSUER=$PUBLIC_URL
# https://www.postgresql.org/docs/current/libpq-envars.html # https://www.postgresql.org/docs/current/libpq-envars.html
PGUSER=kyoo PGUSER=kyoo
PGPASSWORD=password PGPASSWORD=password
PGDATABASE=kyooDB PGDATABASE=kyoo
PGHOST=postgres PGHOST=postgres
PGPORT=5432 PGPORT=5432

View File

@ -17,7 +17,6 @@ logging.getLogger("rebulk").setLevel(logging.WARNING)
@asynccontextmanager @asynccontextmanager
async def lifespan(_): async def lifespan(_):
print("starting lifetime")
async with ( async with (
init_pool(), init_pool(),
get_db() as db, get_db() as db,
@ -34,7 +33,6 @@ async def lifespan(_):
# there's no way someone else used the same id, right? # there's no way someone else used the same id, right?
is_master = await db.fetchval("select pg_try_advisory_lock(198347)") is_master = await db.fetchval("select pg_try_advisory_lock(198347)")
if is_master: if is_master:
print("this is master")
_ = await asyncio.create_task(scanner.scan(remove_deleted=True)) _ = await asyncio.create_task(scanner.scan(remove_deleted=True))
_ = await asyncio.create_task(scanner.monitor()) _ = await asyncio.create_task(scanner.monitor())
yield yield

View File

@ -1,5 +1,6 @@
import os
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import cast from typing import Any, cast
from asyncpg import Connection, Pool, create_pool from asyncpg import Connection, Pool, create_pool
@ -8,7 +9,17 @@ pool: Pool
@asynccontextmanager @asynccontextmanager
async def init_pool(): async def init_pool():
async with await create_pool() as p: url = os.environ.get("POSTGRES_URL")
connection: dict[str, Any] = (
{
"user": os.environ.get("PGUSER", "kyoo"),
"host": os.environ.get("PGHOST", "postgres"),
"password": os.environ.get("PGPASSWORD", "password"),
}
if url is None
else {"dns": url}
)
async with await create_pool(**connection) as p:
global pool global pool
pool = p pool = p
yield yield