Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
994c433fc8 | ||
|
|
6632df0bc7 | ||
|
|
e4ffa0b356 | ||
|
|
e8a43b8a79 | ||
|
|
b3a10ba3ef | ||
|
|
46e878bef6 | ||
|
|
39cca5d235 | ||
|
|
2ed89760d6 | ||
|
|
a8df164cc2 | ||
|
|
0a8221cf83 | ||
|
|
e7fc921d6a | ||
|
|
04bb783a12 | ||
|
|
68bd241f2c | ||
|
|
4c02ab9d55 | ||
|
|
74bf7e19db | ||
|
|
92e074b6d0 | ||
|
|
a70bad1528 | ||
|
|
b47f2621e1 | ||
|
|
7244c1d658 | ||
|
|
d991d6b6c7 | ||
|
|
db3bf96847 | ||
|
|
0f81a81029 | ||
|
|
b5710548b1 | ||
|
|
d2e5b64ece | ||
|
|
5b5cdc4cd8 | ||
|
|
17ac04856b | ||
|
|
69df0e51b8 | ||
|
|
9a06dc389d | ||
|
|
51f3e98816 | ||
|
|
68ba3a25b9 | ||
|
|
7e9308c48a | ||
|
|
21a34a4bd5 | ||
|
|
ee16441b79 | ||
|
|
74133ec589 | ||
|
|
f650a3a94e | ||
|
|
c10d98f3b4 | ||
|
|
207401b5fb | ||
|
|
d4d7806189 | ||
|
|
497f7743f2 | ||
|
|
2c06265d11 | ||
|
|
930cdfdff9 | ||
|
|
ab3fab3e68 | ||
|
|
dd5789fcd9 | ||
|
|
fca4286d87 | ||
|
|
7cfddfc7ef | ||
|
|
47740e3f4a | ||
|
|
693b4a42f6 |
No files matched your search
@@ -0,0 +1,55 @@
|
||||
# EditorConfig is awesome: https://editorconfig.org
|
||||
root = true
|
||||
|
||||
# -------------------------------------------------
|
||||
# General settings – apply to all files
|
||||
# -------------------------------------------------
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
# -------------------------------------------------
|
||||
# Markdown (*.md)
|
||||
# -------------------------------------------------
|
||||
[*.md]
|
||||
max_line_length = off # Markdown lines can be long
|
||||
trim_trailing_whitespace = false # Preserve intentional spaces
|
||||
|
||||
# -------------------------------------------------
|
||||
# Go (*.go)
|
||||
# -------------------------------------------------
|
||||
[*.go]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
trim_trailing_whitespace = true
|
||||
max_line_length = 100
|
||||
|
||||
# -------------------------------------------------
|
||||
# JavaScript & TypeScript (*.js, *.jsx, *.ts, *.tsx)
|
||||
# -------------------------------------------------
|
||||
[*.{js,jsx,ts,tsx}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
max_line_length = 120
|
||||
|
||||
# -------------------------------------------------
|
||||
# HTML (*.html, *.htm)
|
||||
# -------------------------------------------------
|
||||
[*.{html,htm}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
max_line_length = 120
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
# -------------------------------------------------
|
||||
# CSS & SCSS (*.css, *.scss)
|
||||
# -------------------------------------------------
|
||||
[*.{css,scss}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
max_line_length = 120
|
||||
trim_trailing_whitespace = true
|
||||
+192
-31
@@ -1,18 +1,18 @@
|
||||
# CD workflow — TEST LAB DEPLOYMENT
|
||||
# Deploys to test lab host 192.168.88.40:2375.
|
||||
# Production Hetzner target TBD — switch DOCKER_HOST when provisioned.
|
||||
# See DEPLOYMENT.md Production Deployment section for cutover plan.
|
||||
|
||||
name: CD
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["CI"]
|
||||
types:
|
||||
- completed
|
||||
push:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: [ubuntu-latest]
|
||||
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
|
||||
environment: production
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
@@ -22,12 +22,11 @@ jobs:
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to Gitea Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: git.allucanget.biz
|
||||
username: ${{ secrets.CI_REGISTRY_USER }}
|
||||
password: ${{ secrets.CI_REGISTRY_TOKEN }}
|
||||
- name: Login to registry on remote host
|
||||
env:
|
||||
DOCKER_HOST: tcp://192.168.88.40:2375
|
||||
run: |
|
||||
echo "${{ secrets.CI_REGISTRY_TOKEN }}" | docker login git.allucanget.biz -u "${{ secrets.CI_REGISTRY_USER }}" --password-stdin
|
||||
|
||||
- name: Prepare environment
|
||||
run: |
|
||||
@@ -44,52 +43,214 @@ jobs:
|
||||
NGINX_PORT=80
|
||||
EOF
|
||||
|
||||
- name: Seed Prometheus config
|
||||
- name: Seed Prometheus config on remote host
|
||||
env:
|
||||
DOCKER_HOST: tcp://192.168.88.40:2375
|
||||
run: |
|
||||
mkdir -p monitoring/prometheus
|
||||
cat > monitoring/prometheus/prometheus.yml << 'EOF'
|
||||
docker run --rm \
|
||||
-v linearvoid_prometheus-config:/etc/prometheus \
|
||||
alpine:latest sh -c '
|
||||
cat > /etc/prometheus/prometheus.yml << "PROMEOF"
|
||||
global:
|
||||
scrape_interval: 15s
|
||||
evaluation_interval: 15s
|
||||
|
||||
scrape_configs:
|
||||
- job_name: 'prometheus'
|
||||
static_configs:
|
||||
- targets: ['localhost:9090']
|
||||
rule_files:
|
||||
- "/etc/prometheus/alerts.yml"
|
||||
|
||||
- job_name: 'linearvoid'
|
||||
scrape_configs:
|
||||
- job_name: "chat"
|
||||
static_configs:
|
||||
- targets: ['playout:8081', 'chat:3000', 'nginx:80']
|
||||
EOF
|
||||
- targets: ["chat:3000"]
|
||||
- job_name: "playout"
|
||||
static_configs:
|
||||
- targets: ["playout:8081"]
|
||||
- job_name: "nginx"
|
||||
static_configs:
|
||||
- targets: ["nginx-exporter:9113"]
|
||||
- job_name: "redis"
|
||||
static_configs:
|
||||
- targets: ["redis-exporter:9121"]
|
||||
- job_name: "postgres"
|
||||
static_configs:
|
||||
- targets: ["postgres-exporter:9187"]
|
||||
- job_name: "node"
|
||||
static_configs:
|
||||
- targets: ["node-exporter:9100"]
|
||||
- job_name: "prometheus"
|
||||
static_configs:
|
||||
- targets: ["localhost:9090"]
|
||||
PROMEOF
|
||||
cat > /etc/prometheus/alerts.yml << "ALERTSEOF"
|
||||
groups:
|
||||
- name: linearvoid
|
||||
rules:
|
||||
- alert: ChatDown
|
||||
expr: up{job="chat"} == 0
|
||||
for: 2m
|
||||
labels:
|
||||
severity: critical
|
||||
annotations:
|
||||
summary: "Chat service is down"
|
||||
- alert: PlayoutDown
|
||||
expr: up{job="playout"} == 0
|
||||
for: 2m
|
||||
labels:
|
||||
severity: critical
|
||||
annotations:
|
||||
summary: "Playout service is down"
|
||||
- alert: RedisDown
|
||||
expr: linearvoid_chat_redis_up == 0
|
||||
for: 2m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Redis is unreachable"
|
||||
- alert: PlayoutSlateActive
|
||||
expr: linearvoid_playout_mode == 1
|
||||
for: 1m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Playout in slate fallback"
|
||||
- alert: HighChatLatency
|
||||
expr: rate(linearvoid_chat_messages_total[5m]) < 0.01
|
||||
for: 5m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Unusually low chat message rate"
|
||||
- alert: PrometheusWALFailing
|
||||
expr: increase(prometheus_tsdb_wal_writes_failed_total[5m]) > 0
|
||||
for: 5m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Prometheus WAL writes failing"
|
||||
- alert: HighCPU
|
||||
expr: 100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
|
||||
for: 10m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "High CPU usage"
|
||||
- alert: LowDiskSpace
|
||||
expr: (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100 < 10
|
||||
for: 5m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Low disk space"
|
||||
ALERTSEOF
|
||||
'
|
||||
|
||||
- name: Seed Grafana provisioning on remote host
|
||||
env:
|
||||
DOCKER_HOST: tcp://192.168.88.40:2375
|
||||
run: |
|
||||
docker run --rm \
|
||||
-v linearvoid_grafana-provisioning:/etc/grafana/provisioning \
|
||||
alpine:latest sh -c '
|
||||
mkdir -p /etc/grafana/provisioning/datasources /etc/grafana/provisioning/dashboards
|
||||
cat > /etc/grafana/provisioning/datasources/datasource.yml << "DSEOF"
|
||||
apiVersion: 1
|
||||
datasources:
|
||||
- name: Prometheus
|
||||
type: prometheus
|
||||
access: proxy
|
||||
url: http://prometheus:9090
|
||||
isDefault: true
|
||||
editable: true
|
||||
DSEOF
|
||||
cat > /etc/grafana/provisioning/dashboards/dashboard.yml << "PROVEOF"
|
||||
apiVersion: 1
|
||||
providers:
|
||||
- name: "default"
|
||||
orgId: 1
|
||||
folder: ""
|
||||
type: file
|
||||
disableDeletion: false
|
||||
updateIntervalSeconds: 10
|
||||
allowUiUpdates: true
|
||||
options:
|
||||
path: /etc/grafana/provisioning/dashboards
|
||||
PROVEOF
|
||||
cat > /etc/grafana/provisioning/dashboards/linearvoid-app.json << "DBEOF"
|
||||
{
|
||||
"title": "Linear Void Monitoring",
|
||||
"panels": [
|
||||
{
|
||||
"title": "Concurrent Viewers",
|
||||
"type": "timeseries",
|
||||
"targets": [{ "expr": "linearvoid_chat_connected_clients" }]
|
||||
},
|
||||
{
|
||||
"title": "Stored Messages",
|
||||
"type": "stat",
|
||||
"targets": [{ "expr": "linearvoid_chat_messages_total" }]
|
||||
},
|
||||
{
|
||||
"title": "Redis Reachable",
|
||||
"type": "stat",
|
||||
"targets": [{ "expr": "linearvoid_chat_redis_up" }]
|
||||
},
|
||||
{
|
||||
"title": "Playout Mode",
|
||||
"type": "stat",
|
||||
"targets": [{ "expr": "linearvoid_playout_mode" }]
|
||||
}
|
||||
]
|
||||
}
|
||||
DBEOF
|
||||
'
|
||||
|
||||
- name: Deploy with docker-compose
|
||||
env:
|
||||
DOCKER_HOST: tcp://192.168.88.40:2375
|
||||
run: |
|
||||
docker compose -f docker-compose.prod.yml pull
|
||||
docker compose -f docker-compose.prod.yml up -d --remove-orphans
|
||||
docker compose -f docker-compose.prod.yml up -d --remove-orphans --force-recreate
|
||||
|
||||
- name: Health check
|
||||
env:
|
||||
DOCKER_HOST: tcp://192.168.88.40:2375
|
||||
run: |
|
||||
# Wait for playout to stabilize (it may restart during init)
|
||||
# Wait for services to stabilize
|
||||
sleep 20
|
||||
curl -f http://192.168.88.40:3000/health || exit 1
|
||||
|
||||
# Check chat health (via docker exec — IP-agnostic, works on any DOCKER_HOST)
|
||||
echo "Checking chat health..."
|
||||
if ! docker exec linearvoid-chat curl -sf http://localhost:3000/health; then
|
||||
echo "Chat health check failed"
|
||||
docker logs linearvoid-chat --tail 50
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check nginx health
|
||||
echo "Checking nginx health..."
|
||||
if ! docker exec linearvoid-nginx curl -sf http://localhost/health; then
|
||||
echo "Nginx health check failed"
|
||||
docker logs linearvoid-nginx --tail 50
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Retry playout health check since it may still be initializing
|
||||
echo "Checking playout health..."
|
||||
for i in $(seq 1 6); do
|
||||
if docker exec linearvoid-playout curl -sf http://localhost:8081/health; then
|
||||
echo "Playout is healthy"
|
||||
exit 0
|
||||
break
|
||||
fi
|
||||
echo "Playout not ready yet (attempt $i/6), waiting 5s..."
|
||||
sleep 5
|
||||
done
|
||||
echo "Playout failed to become healthy after 6 attempts"
|
||||
docker logs linearvoid-playout --tail 50
|
||||
exit 1
|
||||
curl -f http://192.168.88.40:3000/health || exit 1
|
||||
docker exec linearvoid-playout curl -sf http://localhost:8081/health || exit 1
|
||||
if ! docker exec linearvoid-playout curl -sf http://localhost:8081/health; then
|
||||
echo "Playout failed to become healthy after 6 attempts"
|
||||
docker logs linearvoid-playout --tail 50
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "All services healthy"
|
||||
|
||||
- name: Cleanup old images
|
||||
env:
|
||||
|
||||
+31
-43
@@ -19,14 +19,12 @@ jobs:
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "20"
|
||||
cache: "npm"
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
|
||||
- name: Install Frontend Dependencies
|
||||
run: cd frontend && npm ci
|
||||
|
||||
- name: Lint Frontend
|
||||
run: cd frontend && npm run lint || echo "Linting not configured yet"
|
||||
run: cd frontend && npm run lint
|
||||
|
||||
- name: Build Frontend
|
||||
run: cd frontend && npm run build
|
||||
@@ -34,17 +32,28 @@ jobs:
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.22"
|
||||
cache: true
|
||||
go-version: "1.25"
|
||||
env:
|
||||
GOTOOLCHAIN: local
|
||||
|
||||
- name: Validate Playlist Normalization
|
||||
run: cd backend/playout && go run src/playlist.go playlist.txt || echo "Validation script not yet complete"
|
||||
- name: Download Go modules
|
||||
run: cd backend/chat && GOTOOLCHAIN=local go mod download
|
||||
|
||||
- name: Build Docker Images
|
||||
run: docker compose build --no-cache
|
||||
- name: Validate Go
|
||||
run: cd backend/chat && GOTOOLCHAIN=local go vet ./...
|
||||
|
||||
- name: Run Tests
|
||||
run: echo "Tests will be added in future CI steps (per TODO.md)"
|
||||
run: cd backend/chat && GOTOOLCHAIN=local go test ./... -v -cover -coverprofile=coverage.out -race
|
||||
|
||||
- name: Check Coverage
|
||||
run: |
|
||||
cd backend/chat
|
||||
COVERAGE=$(GOTOOLCHAIN=local go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
|
||||
echo "Coverage: $COVERAGE%"
|
||||
if [ "$(echo "$COVERAGE < 50" | bc)" -eq 1 ]; then
|
||||
echo "Coverage $COVERAGE% below 50% threshold"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker:
|
||||
needs: test
|
||||
@@ -86,6 +95,8 @@ jobs:
|
||||
push: true
|
||||
tags: ${{ steps.meta-playout.outputs.tags }}
|
||||
labels: ${{ steps.meta-playout.outputs.labels }}
|
||||
cache-from: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:playout
|
||||
cache-to: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:playout,mode=max
|
||||
|
||||
- name: Extract metadata for Chat
|
||||
id: meta-chat
|
||||
@@ -106,6 +117,8 @@ jobs:
|
||||
push: true
|
||||
tags: ${{ steps.meta-chat.outputs.tags }}
|
||||
labels: ${{ steps.meta-chat.outputs.labels }}
|
||||
cache-from: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:chat
|
||||
cache-to: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:chat,mode=max
|
||||
|
||||
- name: Extract metadata for Nginx
|
||||
id: meta-nginx
|
||||
@@ -120,37 +133,12 @@ jobs:
|
||||
type=raw,value=latest,enable={{is_default_branch}}
|
||||
|
||||
- name: Build and push Nginx image
|
||||
run: |
|
||||
TAGS="${{ steps.meta-nginx.outputs.tags }}"
|
||||
LABELS="${{ steps.meta-nginx.outputs.labels }}"
|
||||
TAG_ARGS=""
|
||||
for tag in $TAGS; do
|
||||
TAG_ARGS="$TAG_ARGS --tag $tag"
|
||||
done
|
||||
LABEL_ARGS=""
|
||||
while IFS= read -r label; do
|
||||
[ -n "$label" ] && LABEL_ARGS="$LABEL_ARGS --label $label"
|
||||
done <<< "$LABELS"
|
||||
docker buildx build \
|
||||
--push \
|
||||
--file infrastructure/nginx/Dockerfile \
|
||||
$TAG_ARGS \
|
||||
$LABEL_ARGS \
|
||||
.
|
||||
|
||||
release:
|
||||
needs: docker
|
||||
runs-on: [ubuntu-latest]
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Create Release
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
generate_release_notes: true
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.CI_REGISTRY_TOKEN }}
|
||||
context: .
|
||||
file: infrastructure/nginx/Dockerfile
|
||||
push: true
|
||||
tags: ${{ steps.meta-nginx.outputs.tags }}
|
||||
labels: ${{ steps.meta-nginx.outputs.labels }}
|
||||
cache-from: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:nginx
|
||||
cache-to: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:nginx,mode=max
|
||||
@@ -13,7 +13,6 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
token: ${{ secrets.CI_REGISTRY_TOKEN }}
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
@@ -31,28 +30,48 @@ jobs:
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.22"
|
||||
go-version: "1.25"
|
||||
cache: true
|
||||
env:
|
||||
GOTOOLCHAIN: local
|
||||
|
||||
- name: Update Go Dependencies
|
||||
run: |
|
||||
cd backend/playout && go get -u ./... && go mod tidy
|
||||
cd ../chat && go get -u ./... && go mod tidy
|
||||
cd backend/playout && GOTOOLCHAIN=local go get -u ./... && go mod tidy
|
||||
cd ../chat && GOTOOLCHAIN=local go get -u ./... && go mod tidy
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v5
|
||||
with:
|
||||
token: ${{ secrets.CI_REGISTRY_TOKEN }}
|
||||
commit-message: "chore: update dependencies"
|
||||
title: "chore: weekly dependency updates"
|
||||
body: |
|
||||
Automated weekly dependency updates.
|
||||
- name: Commit and push changes
|
||||
run: |
|
||||
git config user.name "linearvoid-bot"
|
||||
git config user.email "bot@linearvoid.local"
|
||||
git add frontend/package*.json backend/playout/go.* backend/chat/go.*
|
||||
if git diff --cached --quiet; then
|
||||
echo "No dependency changes detected."
|
||||
exit 0
|
||||
fi
|
||||
git commit -m "chore: update dependencies"
|
||||
git push origin HEAD:dependency-updates --force
|
||||
|
||||
This PR updates:
|
||||
- Frontend npm dependencies
|
||||
- Go module dependencies
|
||||
|
||||
Please review the changes and ensure tests pass before merging.
|
||||
branch: dependency-updates
|
||||
delete-branch: true
|
||||
labels: dependencies, automated
|
||||
- name: Create Pull Request via Gitea API
|
||||
run: |
|
||||
RESPONSE=$(curl -s -o /tmp/pr-response.json -w "%{http_code}" \
|
||||
-X POST "$CI_SERVER_URL/api/v1/repos/zwitschi/linearvoid/pulls" \
|
||||
-H "Authorization: token ${{ secrets.CI_REGISTRY_TOKEN }}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"title": "chore: weekly dependency updates",
|
||||
"head": "dependency-updates",
|
||||
"base": "main",
|
||||
"body": "Automated weekly dependency updates.\n\nThis PR updates:\n- Frontend npm dependencies\n- Go module dependencies\n\nPlease review the changes and ensure tests pass before merging."
|
||||
}')
|
||||
if [ "$RESPONSE" -eq 201 ]; then
|
||||
PR_URL=$(cat /tmp/pr-response.json | jq -r '.html_url')
|
||||
echo "PR created: $PR_URL"
|
||||
elif [ "$RESPONSE" -eq 409 ]; then
|
||||
echo "PR already exists. Pushing updated commits..."
|
||||
git push origin dependency-updates --force
|
||||
else
|
||||
echo "Error creating PR (HTTP $RESPONSE):"
|
||||
cat /tmp/pr-response.json
|
||||
exit 1
|
||||
fi
|
||||
@@ -3,11 +3,11 @@ name: Release
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
- "*.*.*" # Trigger on any semantic version tag (e.g., 1.0.0, 2.1.3)
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: "Version to release (e.g., v1.0.0)"
|
||||
description: "Version to release (e.g., 1.0.0)"
|
||||
required: true
|
||||
type: string
|
||||
|
||||
@@ -34,10 +34,25 @@ jobs:
|
||||
id: version
|
||||
run: |
|
||||
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
|
||||
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
|
||||
VERSION="${{ github.event.inputs.version }}"
|
||||
else
|
||||
echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
|
||||
VERSION="${GITHUB_REF#refs/tags/}"
|
||||
fi
|
||||
if [[ -z "$VERSION" ]]; then
|
||||
echo "::error::VERSION is empty. Provide a version via workflow_dispatch input or push a tag."
|
||||
exit 1
|
||||
fi
|
||||
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
||||
echo "Releasing version: $VERSION"
|
||||
|
||||
- name: Print version to gitea runner log
|
||||
run: |
|
||||
echo "Releasing version: ${{ steps.version.outputs.version }}"
|
||||
echo "===DEBUG==="
|
||||
echo "VERSION: $VERSION"
|
||||
echo "GITHUB_REF: $GITHUB_REF"
|
||||
echo "steps.version.outputs.version: ${{ steps.version.outputs.version }}"
|
||||
echo "github.event_name: ${{ github.event_name }}"
|
||||
|
||||
- name: Extract metadata for Playout
|
||||
id: meta-playout
|
||||
@@ -56,6 +71,8 @@ jobs:
|
||||
push: true
|
||||
tags: ${{ steps.meta-playout.outputs.tags }}
|
||||
labels: ${{ steps.meta-playout.outputs.labels }}
|
||||
cache-from: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:playout
|
||||
cache-to: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:playout,mode=max
|
||||
|
||||
- name: Extract metadata for Chat
|
||||
id: meta-chat
|
||||
@@ -74,6 +91,8 @@ jobs:
|
||||
push: true
|
||||
tags: ${{ steps.meta-chat.outputs.tags }}
|
||||
labels: ${{ steps.meta-chat.outputs.labels }}
|
||||
cache-from: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:chat
|
||||
cache-to: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:chat,mode=max
|
||||
|
||||
- name: Extract metadata for Nginx
|
||||
id: meta-nginx
|
||||
@@ -93,10 +112,12 @@ jobs:
|
||||
push: true
|
||||
tags: ${{ steps.meta-nginx.outputs.tags }}
|
||||
labels: ${{ steps.meta-nginx.outputs.labels }}
|
||||
cache-from: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:nginx
|
||||
cache-to: type=registry,ref=git.allucanget.biz/zwitschi/linearvoid/buildcache:nginx,mode=max
|
||||
|
||||
- name: Create Release on Gitea
|
||||
run: |
|
||||
curl -X POST "$CI_SERVER_URL/api/v1/repos/$CI_REPO_OWNER/$CI_REPO_NAME/releases" \
|
||||
curl -X POST "https://git.allucanget.biz/api/v1/repos/zwitschi/linearvoid/releases" \
|
||||
-H "Authorization: token ${{ secrets.CI_REGISTRY_TOKEN }}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"tag_name\":\"${{ steps.version.outputs.version }}\",\"name\":\"${{ steps.version.outputs.version }}\",\"body\":\"Release ${{ steps.version.outputs.version }}\n\nSee [CHANGELOG.md](https://git.allucanget.biz/zwitschi/linearvoid/src/branch/main/CHANGELOG.md) for details.\",\"draft\":false,\"prerelease\":false}"
|
||||
@@ -2,11 +2,18 @@
|
||||
|
||||
Agents instructions for the linearvoid project.
|
||||
|
||||
## Output
|
||||
|
||||
- use /caveman skill, latest version from <https://github.com/JuliusBrussee/caveman> is <https://github.com/JuliusBrussee/caveman/tree/main/skills/caveman>
|
||||
|
||||
## General Guidelines
|
||||
|
||||
- Use the architecture documentation (arc42 template and ADRs) as the source of truth for design decisions.
|
||||
- Follow the project folder structure defined in ADR-002 and reflected in the root README.
|
||||
- Use the architecture documentation (arc42 template and ADRs in `docs`) as the source of truth for design decisions.
|
||||
- Follow the project folder structure in the root README.
|
||||
- Update documentation first before implementing changes to ensure traceability of decisions.
|
||||
|
||||
## Task Management
|
||||
|
||||
- Plan any task in the `TODO.md` file with clear steps and mark them as completed when done. Move completed sections to DONE.md. Write entry in root `CHANGELOG.md` for any significant changes. Avoid using in-memory todo lists or update them frequently to sync from the `TODO.md` file.
|
||||
- Before taking any action except for planning and writing `TODO.md`, always ask user for confirmation, accept `go`, `ok`, `yes`, `next`, or `continue` to proceed. If the user says `no`, `stop`, or `cancel`, abort the action and return to planning.
|
||||
|
||||
+8
-3
@@ -7,6 +7,10 @@
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Test coverage
|
||||
coverage
|
||||
coverage.out
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
@@ -41,6 +45,7 @@ out/
|
||||
# FFmpeg temporary files
|
||||
*.ts.tmp
|
||||
*.m3u8.tmp
|
||||
*.m4s.tmp
|
||||
hls/
|
||||
temp/
|
||||
|
||||
@@ -79,9 +84,9 @@ Thumbs.db
|
||||
.terraform/
|
||||
|
||||
# Agent / Tool files
|
||||
.github/instructions/TODO.md
|
||||
.github/instructions/DONE.md
|
||||
# (keep AGENTS.md, MEMORY.md, and other instruction files)
|
||||
.github/agents/
|
||||
.github/instructions/
|
||||
.github/MEMORY.md
|
||||
|
||||
# Temporary architecture artifacts
|
||||
**/*.tmp
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
# =============================================
|
||||
# Linear Void .prettierignore
|
||||
# =============================================
|
||||
|
||||
# Dependencies
|
||||
node_modules/
|
||||
|
||||
# Build outputs
|
||||
dist/
|
||||
build/
|
||||
out/
|
||||
*.min.js
|
||||
|
||||
# Test coverage
|
||||
coverage/
|
||||
coverage.out
|
||||
|
||||
# Go source (handled by gofmt, not prettier)
|
||||
*.go
|
||||
go.mod
|
||||
go.sum
|
||||
|
||||
# Binary / compiled
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Media files
|
||||
*.m4s
|
||||
*.m3u8
|
||||
*.ts.tmp # FFmpeg temp, not TypeScript
|
||||
*.m3u8.tmp
|
||||
*.png
|
||||
*.jpg
|
||||
*.jpeg
|
||||
*.gif
|
||||
*.svg
|
||||
*.ico
|
||||
*.webp
|
||||
*.mp4
|
||||
*.webm
|
||||
*.mp3
|
||||
*.ogg
|
||||
|
||||
# Runtime data
|
||||
data/
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Databases
|
||||
*.db
|
||||
*.sqlite
|
||||
*.sqlite3
|
||||
|
||||
# Lockfile (auto-generated, don't reformat)
|
||||
package-lock.json
|
||||
|
||||
# Shell scripts (prettier doesn't handle sh)
|
||||
*.sh
|
||||
|
||||
# Environment & secrets
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Editor & IDE
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Git
|
||||
.git/
|
||||
|
||||
# Terraform / IaC
|
||||
*.tfstate
|
||||
*.tfstate.backup
|
||||
.terraform/
|
||||
|
||||
# Temp & backup files
|
||||
**/*.tmp
|
||||
**/*.bak
|
||||
|
||||
# Docker
|
||||
.dockerignore
|
||||
@@ -0,0 +1,307 @@
|
||||
# API Reference
|
||||
|
||||
This document describes the currently implemented API surfaces in Linear Void.
|
||||
|
||||
## Base URLs
|
||||
|
||||
- Public app and admin: <https://linearvoid.tv>
|
||||
- WebSocket endpoint: <wss://linearvoid.tv/ws>
|
||||
- Local fallback: <http://localhost> and <ws://localhost/ws>
|
||||
|
||||
## Authentication Model
|
||||
|
||||
Admin REST endpoints use a cookie session.
|
||||
|
||||
- Cookie name: linearvoid_admin_session
|
||||
- Cookie attributes: HttpOnly, SameSite=Strict, Secure when request is HTTPS (or X-Forwarded-Proto=https)
|
||||
- Session TTL: controlled by ADMIN_SESSION_TTL_HOURS (default 12h)
|
||||
|
||||
## Chat WebSocket API
|
||||
|
||||
### Connect
|
||||
|
||||
Endpoint:
|
||||
|
||||
- GET /ws?username={name}&room={room}
|
||||
|
||||
Client sends JSON messages over the socket.
|
||||
|
||||
### Message Envelope (client -> server)
|
||||
|
||||
```json
|
||||
{
|
||||
"room": "main",
|
||||
"username": "viewer123",
|
||||
"text": "hello world",
|
||||
"type": "public"
|
||||
}
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- type is usually public for room chat and private for DM room routing.
|
||||
- Private room names are formatted like dm:userA-userB (sorted usernames).
|
||||
- Moderation/system events are sent as type=system messages.
|
||||
|
||||
### Message Envelope (server -> client)
|
||||
|
||||
```json
|
||||
{
|
||||
"room": "main",
|
||||
"username": "viewer123",
|
||||
"text": "hello world",
|
||||
"type": "public",
|
||||
"timestamp": "2026-08-04T12:34:56Z"
|
||||
}
|
||||
```
|
||||
|
||||
## Admin Session API
|
||||
|
||||
### Login
|
||||
|
||||
- POST /api/admin/login
|
||||
- Auth: none
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "your-password"
|
||||
}
|
||||
```
|
||||
|
||||
Success response:
|
||||
|
||||
```json
|
||||
{
|
||||
"authenticated": true,
|
||||
"username": "admin"
|
||||
}
|
||||
```
|
||||
|
||||
### Logout
|
||||
|
||||
- POST /api/admin/logout
|
||||
- Auth: optional (clears cookie either way)
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"authenticated": false
|
||||
}
|
||||
```
|
||||
|
||||
### Session Check
|
||||
|
||||
- GET /api/admin/session
|
||||
- Auth: cookie required
|
||||
|
||||
Success response:
|
||||
|
||||
```json
|
||||
{
|
||||
"authenticated": true,
|
||||
"username": "admin"
|
||||
}
|
||||
```
|
||||
|
||||
Failure:
|
||||
|
||||
- 401 unauthorized
|
||||
|
||||
## Admin Data API
|
||||
|
||||
All endpoints below require admin authentication.
|
||||
|
||||
### Overview
|
||||
|
||||
- GET /api/admin/overview
|
||||
|
||||
Returns aggregate counters and runtime/system snapshot fields.
|
||||
|
||||
### Playlist
|
||||
|
||||
- GET /api/admin/playlist
|
||||
- PUT /api/admin/playlist
|
||||
|
||||
PUT request:
|
||||
|
||||
```json
|
||||
{
|
||||
"content": "file '/app/media/video1.mp4'\nfile '/app/media/video2.mp4'\n"
|
||||
}
|
||||
```
|
||||
|
||||
### Schedule
|
||||
|
||||
- GET /api/admin/schedule
|
||||
|
||||
Returns parsed playlist entries plus matched media metadata/runtime.
|
||||
|
||||
### Moderation
|
||||
|
||||
- GET /api/admin/moderation/logs?limit=50
|
||||
- POST /api/admin/moderation/actions
|
||||
|
||||
Action request:
|
||||
|
||||
```json
|
||||
{
|
||||
"action": "mute",
|
||||
"target": "username-or-id",
|
||||
"moderator": "admin",
|
||||
"reason": "spam",
|
||||
"room": "main"
|
||||
}
|
||||
```
|
||||
|
||||
### Analytics
|
||||
|
||||
- GET /api/admin/analytics
|
||||
|
||||
Returns totals (messages, moderation actions, rooms) and top room summary.
|
||||
|
||||
### System
|
||||
|
||||
- GET /api/admin/system
|
||||
|
||||
Returns service health snapshot for chat/playout/redis/database and config metadata.
|
||||
|
||||
### Active Users
|
||||
|
||||
- GET /api/admin/users
|
||||
|
||||
Returns currently discovered presence users (room + username) when Redis is available.
|
||||
|
||||
## Media Library API
|
||||
|
||||
### Media Collection
|
||||
|
||||
- GET /api/admin/media
|
||||
- POST /api/admin/media (multipart upload, legacy path)
|
||||
|
||||
Multipart form field name: file
|
||||
|
||||
### Media Item
|
||||
|
||||
- GET /api/admin/media/{id}
|
||||
- PUT /api/admin/media/{id}
|
||||
- DELETE /api/admin/media/{id}
|
||||
|
||||
PUT request:
|
||||
|
||||
```json
|
||||
{
|
||||
"status": "ready",
|
||||
"title": "My Clip",
|
||||
"tags": "music,live"
|
||||
}
|
||||
```
|
||||
|
||||
## Resumable Upload API
|
||||
|
||||
Maximum file size: 2GB
|
||||
|
||||
Default chunk size: 8MB
|
||||
|
||||
### Initialize or Resume Session
|
||||
|
||||
- POST /api/admin/media/uploads
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"uploadId": "optional-existing-id",
|
||||
"fileName": "clip.mp4",
|
||||
"fileSize": 123456789,
|
||||
"mimeType": "video/mp4",
|
||||
"chunkSize": 8388608,
|
||||
"totalChunks": 15
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"uploadId": "hex-token",
|
||||
"fileName": "clip.mp4",
|
||||
"fileSize": 123456789,
|
||||
"chunkSize": 8388608,
|
||||
"totalChunks": 15,
|
||||
"uploadedChunks": 3,
|
||||
"progress": 20
|
||||
}
|
||||
```
|
||||
|
||||
### Check Upload Status
|
||||
|
||||
- GET /api/admin/media/uploads/{uploadId}
|
||||
|
||||
Returns same status envelope as initialize.
|
||||
|
||||
### Upload Chunk
|
||||
|
||||
- PUT /api/admin/media/uploads/{uploadId}/chunk?index={chunkIndex}
|
||||
- Content-Type: application/octet-stream
|
||||
|
||||
Response returns updated upload status envelope.
|
||||
|
||||
### Complete Upload
|
||||
|
||||
- POST /api/admin/media/uploads/{uploadId}/complete
|
||||
|
||||
On success returns admin media listing payload (files + generatedAt).
|
||||
|
||||
## Metrics and Health
|
||||
|
||||
### Chat metrics
|
||||
|
||||
- GET /metrics
|
||||
|
||||
Prometheus exposition includes connected clients, stored messages, moderation actions, Redis indicators, active admin sessions, and build info.
|
||||
|
||||
### Health
|
||||
|
||||
- GET /health on chat service
|
||||
- GET /health on playout service
|
||||
- GET /health on nginx reverse proxy
|
||||
|
||||
## HTTP Status Conventions
|
||||
|
||||
| Code | Meaning | When |
|
||||
| ---- | --------------------- | ----------------------------------------------------------- |
|
||||
| 200 | OK | Successful GET, PUT, or action |
|
||||
| 201 | Created | Successful POST (upload init, override created) |
|
||||
| 204 | No Content | Successful DELETE, CORS preflight |
|
||||
| 400 | Bad Request | Malformed JSON, missing required fields, invalid input |
|
||||
| 401 | Unauthorized | Missing or expired admin session |
|
||||
| 403 | Forbidden | Valid session but insufficient permissions |
|
||||
| 404 | Not Found | Resource doesn't exist |
|
||||
| 405 | Method Not Allowed | Wrong HTTP method for endpoint |
|
||||
| 409 | Conflict | Schedule override overlap, upload ID mismatch |
|
||||
| 413 | Payload Too Large | Upload exceeds 2 GB limit |
|
||||
| 422 | Unprocessable | File fails normalization check (codec, fps, resolution) |
|
||||
| 429 | Too Many Requests | Rate limit exceeded (chat messages, WS connections, login) |
|
||||
| 500 | Internal Server Error | Unexpected server failure |
|
||||
| 503 | Service Unavailable | Redis unavailable for user actions, playout health degraded |
|
||||
|
||||
## Rate Limiting
|
||||
|
||||
All limits are per-IP (from `X-Forwarded-For` or `RemoteAddr`) unless noted.
|
||||
|
||||
| Resource | Limit | Window | Response |
|
||||
| --------------------- | ---------------------------------- | ---------------------------------------- | ------------------------------------------------------ |
|
||||
| Chat messages | `CHAT_RATE_LIMIT_MAX` (default 10) | `CHAT_RATE_LIMIT_WINDOW_S` (default 10s) | 429 + `{"type":"system","text":"Rate limit exceeded"}` |
|
||||
| WebSocket connections | 5 | 60s | 429 + `"too many connections from this IP"` |
|
||||
| Admin login | 5 | 5min | 429 + `"too many login attempts, try again later"` |
|
||||
|
||||
Rate limit headers on applicable responses:
|
||||
|
||||
- `X-RateLimit-Limit`: maximum requests per window
|
||||
- `Retry-After`: seconds until window resets
|
||||
- 413: payload too large
|
||||
- 500: internal server error
|
||||
+122
-1
@@ -2,6 +2,127 @@
|
||||
|
||||
<!-- markdownlint-disable MD024 -->
|
||||
|
||||
## [1.0.9] - 2026-08-06
|
||||
|
||||
### Added
|
||||
|
||||
- **Prometheus alerts mount**: `alerts.yml` mounted into prometheus container in both dev and prod compose files.
|
||||
- **Grafana provisioning**: Prometheus datasource + dashboard provider YAML configs in `monitoring/grafana/`.
|
||||
|
||||
### Changed
|
||||
|
||||
- **Prod prometheus**: Switched from empty named volume to bind mounts for config files (consistent with dev).
|
||||
- **CD pipeline**: Trigger on `push` (parallel with CI), removed `environment: production` (was causing silent abort).
|
||||
- **CI pipeline**: Removed redundant `release` job (handled by `release.yml`).
|
||||
- **Tag format**: Standardized on `*.*.*` (e.g. `1.0.9`), removed `v` prefix from existing tags.
|
||||
- **DEPLOYMENT.md**: Documented parallel CI/CD pipeline flow with workflow table.
|
||||
- **Runtime checks**: Executed normalization edge-case tests (MOV, MKV, VFR, 5.1 input) and concat continuity verification in running playout environment.
|
||||
- **Incident runbook**: Added quarterly disaster recovery drill procedure with prerequisites, outage simulation, restore validation, and evidence template.
|
||||
- **Manual ops checklist**: Added `docs/operations/cloudflare-uptimerobot-checklist.md` to execute and record Cloudflare + UptimeRobot dashboard tasks with evidence capture.
|
||||
- **Scaling playbook**: Added `docs/operations/scaling-performance-playbook.md` with benchmark methods, trigger thresholds, and Phase 2/3 scaling decision flow.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Prometheus config mount**: Switched prod from bind mounts to named volume `prometheus-config`, populated via `docker run` on remote host. Fixes `not a directory` error when bind source path doesn't exist on deployment host.
|
||||
- **Viewer mobile overflow**: Replaced `100vw` with `100%` on full-screen viewer containers to remove horizontal overflow on mobile viewports.
|
||||
|
||||
### Removed
|
||||
|
||||
- `.github/MEMORY.md`, `.github/agents/`, `.github/instructions/` — tracking files migrated out of repo.
|
||||
|
||||
## [1.0.8] - 2026-08-06
|
||||
|
||||
### Changed
|
||||
|
||||
- **API.md**: Added full HTTP status code matrix (table with 12 codes) and rate limit headers documentation.
|
||||
- **DEPLOYMENT.md**: Added troubleshooting table (8 common symptoms with check/fix).
|
||||
- **MONITORING.md**: Full rewrite with architecture diagram, 7 scrape targets, 8 alert rules, key metrics, troubleshooting.
|
||||
- **SECURITY.md**: Full rewrite with threat model diagram, TLS/HSTS config, network isolation, secrets management, incident response.
|
||||
- **Architecture docs**: Filled Level 2 building blocks (Mermaid diagram, 5 subsystems with code locations) and Level 3 module breakdowns (chat: 9 modules, playout: 6 modules, frontend: 10 modules).
|
||||
|
||||
## [1.0.7] - 2026-08-06
|
||||
|
||||
### Added
|
||||
|
||||
- **Emergency slate fallback**: `scripts/generate-slate.sh` creates "We'll Be Right Back" slate video + pre-rendered HLS. Playout detects FFmpeg failures (>5 restarts/60s) and auto-switches to slate manifest. Health endpoint reports `degraded` mode. Auto-recovers when FFmpeg restarts successfully.
|
||||
- **Prometheus metrics**: `linearvoid_playout_restarts_total`, `linearvoid_playout_mode` (0=live, 1=slate), `linearvoid_chat_dms_total` exposed on playout + chat `/metrics`.
|
||||
- **Video normalization pipeline**: `scripts/normalize.sh` with env-configurable CRF/preset/audio-bitrate wrapping reference FFmpeg command.
|
||||
- **Strict ffprobe verification gate**: `ValidateVideo()` in playout parses ffprobe JSON, checks codec (h264/aac), framerate (30/1), resolution (1280x720 or 1920x1080), pix_fmt (yuv420p), sample_rate (48000), keyframe interval (≤2s). `ValidateVideoStrict()` returns `{valid, errors[]}`.
|
||||
- **Upload normalization enforcement**: `validateMediaNormalization()` runs on every admin upload (single + chunked). Non-conforming files get `needs_normalization` status. Visible in media library + playlist validation warnings.
|
||||
- **Chat history on room join**: `pushChatHistory()` sends last 50 messages via WebSocket on connect.
|
||||
- **DM offline delivery**: Private messages persisted to DB, appear in history when recipient reconnects.
|
||||
- **Online presence indicators**: Green dot per user in chat sidebar, updated via join/leave system messages.
|
||||
- **Schedule metadata overlay**: "Now Playing" + "Up Next" below viewer player, polling `GET /api/schedule` every 30s. Public `/api/schedule` endpoint added.
|
||||
- **Playlist drag-to-reorder**: Sortable list with drag-and-drop, per-entry remove buttons, validation warnings (missing files, needs_normalization).
|
||||
- **Schedule overrides**: `ScheduleOverride` model + `GET/POST/DELETE /api/admin/schedule/overrides` with conflict detection (409 on overlap). Admin UI with form + list.
|
||||
- **User management actions**: `POST /api/admin/users` with mute/unmute/ban/unban actions. Per-user action buttons in admin users table.
|
||||
- **Inline moderation**: Mute/Ban buttons on each chat log entry row in admin.
|
||||
- **CSV export**: `GET /api/admin/chat-log/export` returns CSV download.
|
||||
- **Analytics time range selector**: 1h, 6h, 24h, 7d dropdown on admin analytics page.
|
||||
- **Runtime config editor**: `GET/PUT /api/admin/config` for rate limit max + window. Admin system page with config form + restart buttons.
|
||||
- **Production target documentation**: `DEPLOYMENT.md` updated with two-environment model (test lab 192.168.88.0/24 vs Hetzner production TBD), CX22 specs, firewall requirements, DNS cutover plan.
|
||||
- **Network diagram**: Mermaid diagram in `SETUP.md` distinguishing test lab from planned production.
|
||||
- **E2E test script**: `scripts/test-playout-e2e.sh` for runtime validation in test lab.
|
||||
- **Redis pub/sub stub**: Architecture ready for multi-instance chat (pending implementation).
|
||||
|
||||
### Changed
|
||||
|
||||
- `docker-compose.yml` + `.prod.yml`: added slate volume mount (`./data/slate:/app/slate:ro`), Redis `--maxmemory 256mb --maxmemory-policy allkeys-lru`.
|
||||
- `backend/playout/Dockerfile`: added `/app/slate` directory.
|
||||
- `handleConnection`: added 30s presence heartbeat + WebSocket ping, banned/muted user checks, env-configurable rate limits (`CHAT_RATE_LIMIT_MAX`, `CHAT_RATE_LIMIT_WINDOW_S`).
|
||||
- `handleModerationCommand`: added moderator role check, mute duration parsing, Redis key enforcement.
|
||||
- `handlePrivateDM`: targeted delivery via `clientMeta` map instead of broadcast.
|
||||
- `initRedis`: added `redisReconnectLoop` with exponential backoff (2s→60s).
|
||||
- `handleAdminMedia` + chunked upload: normalization validation runs on every upload, sets `needs_normalization` status.
|
||||
- nginx: OPTIONS preflight → 204 on all 4 HLS locations. `/health` returns JSON. `/nginx-status` with stub_status (internal-network restricted).
|
||||
- CD workflow: health checks now IP-agnostic (`docker exec` instead of hardcoded IP curls).
|
||||
- `docker-compose.yml` + `.prod.yml`, `.gitea/workflows/cd.yml`, `DEPLOYMENT.md`, `SETUP.md`, `docs/architecture/README.md`, `README.md`: all annotated with test-lab vs production distinction, `192.168.88.0/24` flagged as internal test network.
|
||||
- `docs/architecture/README.md`: CI/CD deployment view updated to note test lab target, production pending.
|
||||
|
||||
### Fixed
|
||||
|
||||
- CD health check no longer hardcodes `192.168.88.40` in curl targets — uses `docker exec` for portability.
|
||||
- Playlist page no longer single-textarea — now shows sortable list with validation warnings.
|
||||
- Schedule page shows overrides alongside playlist timeline.
|
||||
|
||||
## [1.0.6] - 2026-08-05
|
||||
|
||||
### Added
|
||||
|
||||
- `GET /api/admin/chat-log` endpoint with `?room=`, `?limit=`, `?before=`, `?username=`, `?text=` filter/pagination support.
|
||||
- `GET /api/admin/chat-log/:id` endpoint returning a single chat log entry as JSON.
|
||||
- `DELETE /api/admin/chat-log/:id` endpoint returning 204 No Content on success.
|
||||
- `DELETE /api/admin/chat-log/bulk` endpoint accepting `{"ids": [...]}` for bulk deletion (max 500).
|
||||
- Chat Log admin tab with table, individual delete with confirmation dialog, pagination (prev/next), search/filter by room/username/text, select-all checkbox, and bulk delete button.
|
||||
- Added `"lint": "tsc --noEmit"` script to `frontend/package.json`.
|
||||
- `/api/admin/media/{id}` GET/PUT/DELETE handlers for media metadata retrieval, updates, and file+record deletion.
|
||||
- Resumable chunked admin media upload flow with upload-session endpoints and client progress indicators.
|
||||
- Prometheus-format `/metrics` endpoint for chat runtime and admin-session monitoring.
|
||||
- Dedicated operational documentation: `API.md`, `DEPLOYMENT.md`, `MONITORING.md`, `SECURITY.md`.
|
||||
|
||||
### Changed
|
||||
|
||||
- Split `frontend/admin/index.html` into a template shell plus 9 per-tab page HTML files loaded dynamically via `loadPageContent` / `switchTab` / `bindPageEvents`.
|
||||
- Rebuilt viewer, admin, and admin-login pages around custom semantic class structures with accessibility-focused landmarks, tab semantics, and keyboard-focus behavior.
|
||||
- Replaced legacy and Tailwind-based stylesheet rules with a custom design-token CSS system covering responsive layouts, forms, tables, and reduced-motion behavior.
|
||||
- Switched frontend theme to dark-mode-first token defaults with light-scheme override.
|
||||
- Applied new color palette (`#32292f` dark, `#b4c4ae` light, `#587792` brand, `#2ebfa5` focus, `#04650d` ok) across all 22 CSS custom properties in dark and light themes.
|
||||
- Replaced hardcoded admin sidebar and tab-trigger colors with CSS custom properties.
|
||||
- Optimized CI pipeline: removed duplicate `docker compose build --no-cache` from test job, removed broken playlist validation step, fixed Go module cache path, added Docker Buildx registry cache to docker/release jobs.
|
||||
- Bumped chat healthcheck `start_period` from 10s to 30s in `docker-compose.prod.yml`.
|
||||
- CD workflow now verifies nginx (port 8080) + chat (3000) + playout (8081); dumps container logs for all failing services.
|
||||
- Updated Prometheus scrape targets and Grafana dashboard queries to use live chat metrics.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Go module cache in CI now correctly references only `backend/chat/go.sum` (playout go.sum did not exist); added `go mod download` step to seed cache.
|
||||
- Removed broken playlist validation step (`go run src/playlist.go` without go.mod) from CI test job.
|
||||
- Converted nginx CI build from custom shell `docker buildx` to `docker/build-push-action@v5` for cache consistency.
|
||||
- Enforced 2GB upload ceiling plus server-side media file type/size validation for admin uploads.
|
||||
- Increased nginx `client_max_body_size` to allow resumable admin upload chunks without 413 errors.
|
||||
- Removed unused `data-admin-*` Vite env var attributes from admin HTML template.
|
||||
- Removed Tailwind and Alpine runtime/build dependencies; eliminated Tailwind directives/warnings during production build.
|
||||
|
||||
## [1.0.3] - 2026-08-03
|
||||
|
||||
### Added
|
||||
@@ -46,7 +167,7 @@ Full backend and frontend implementation complete.
|
||||
- **Frontend** (`frontend/`): Vite + TypeScript + Tailwind + Alpine.js, HLS player with hls.js, chat sidebar with room switching and private messaging, "Now Playing" overlay, and modern UI (ADR-002, ADR-003).
|
||||
- Complete `docker-compose.yml` for local development with all services, volumes, and healthchecks.
|
||||
- All ADRs (001-007) created and linked in documentation.
|
||||
- `TODO.md`, `DONE.md`, and `CHANGELOG.md` updated with full traceability.
|
||||
- `CHANGELOG.md` updated with full traceability.
|
||||
- Git repository initialized with comprehensive initial commit.
|
||||
|
||||
The project is a fully functional, self-hosted 24/7 linear video channel with synchronized playback, custom chat, R2 delivery, moderation, and admin-ready structure. It matches the architecture defined in the arc42 template and all ADRs.
|
||||
|
||||
+120
-5
@@ -7,9 +7,8 @@ Thank you for considering contributing to Linear Void!
|
||||
1. **Read the Architecture First**: All design decisions are documented in `docs/architecture/README.md` (arc42 template) and the ADRs in `docs/architecture/adr/` (ADR-001 to ADR-007).
|
||||
2. **Follow the Structure**: Use the folder layout defined in ADR-002 (`frontend/`, `backend/playout/`, `backend/chat/`, `infrastructure/`, etc.).
|
||||
3. **Update Documentation Before Code**: Create or update an ADR before implementing significant changes.
|
||||
4. **Update TODO.md**: Plan tasks in `.github/instructions/TODO.md`, mark as completed, and move finished sections to `DONE.md`.
|
||||
5. **Commit Messages**: Reference the relevant ADR (e.g., "feat(playout): add R2 sync per ADR-001").
|
||||
6. **Test with Docker**: Run `docker compose up --build` to test changes.
|
||||
4. **Commit Messages**: Reference the relevant ADR (e.g., "feat(playout): add R2 sync per ADR-001").
|
||||
5. **Test with Docker**: Run `docker compose up --build` to test changes.
|
||||
|
||||
## Repository
|
||||
|
||||
@@ -20,8 +19,8 @@ Thank you for considering contributing to Linear Void!
|
||||
## Code Style
|
||||
|
||||
- Go: Use standard formatting (`go fmt`).
|
||||
- TypeScript: ESLint and Prettier.
|
||||
- Tailwind: Follow the utility-first approach.
|
||||
- TypeScript: `tsc --noEmit` for type-checking (see `frontend/package.json`).
|
||||
- CSS: Design-token system via CSS custom properties in `frontend/src/style.css`. No utility framework.
|
||||
|
||||
## Pull Requests
|
||||
|
||||
@@ -30,4 +29,120 @@ Thank you for considering contributing to Linear Void!
|
||||
- Ensure all tests pass and documentation is updated.
|
||||
- The CI workflow will validate normalization, linting, and Docker builds.
|
||||
|
||||
## Versioning and Releases
|
||||
|
||||
### Version Scheme
|
||||
|
||||
Linear Void follows **Semantic Versioning** ([SemVer](https://semver.org)): `MAJOR.MINOR.PATCH`.
|
||||
|
||||
- **MAJOR** — breaking changes to the viewer, API, or deployment model.
|
||||
- **MINOR** — new features (endpoints, admin tabs, UI capabilities) that are backward-compatible.
|
||||
- **PATCH** — bug fixes, optimizations, documentation polish.
|
||||
|
||||
Git tags use no prefix: `1.0.2`, `1.1.0`, `2.0.0`.
|
||||
|
||||
### Where Versions Are Declared
|
||||
|
||||
| File | Token | Example |
|
||||
| ----------------------- | ------------- | ------------------------- |
|
||||
| `frontend/package.json` | `"version"` | `"1.0.2"` |
|
||||
| `CHANGELOG.md` | `## [X.Y.Z]` | `## [1.0.2] - 2026-08-05` |
|
||||
| `README.md` | Version badge | `version-1.0.2-blue` |
|
||||
|
||||
### Tag Triggers
|
||||
|
||||
| Tag Pattern | Workflow | What Happens |
|
||||
| ---------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
||||
| `*.*.*` (e.g. `1.0.2`) | `release.yml` | Builds all 3 images, pushes to registry with semver tags (`1.0.2`, `1.0`, `latest`), creates Gitea Release via API |
|
||||
| Push to `main` | `ci.yml` → `cd.yml` | CI builds + pushes images with `main`/`latest` tags, CD deploys to production |
|
||||
|
||||
### Full Release Procedure
|
||||
|
||||
#### Step 1: Bump version numbers
|
||||
|
||||
Update these files to the new version (replace `X.Y.Z`):
|
||||
|
||||
- [frontend/package.json](frontend/package.json)
|
||||
- [CHANGELOG.md](CHANGELOG.md) — add new section at top
|
||||
- [README.md](README.md) — version badge
|
||||
|
||||
```bash
|
||||
# frontend/package.json
|
||||
"version": "X.Y.Z"
|
||||
|
||||
# CHANGELOG.md — add new section at top
|
||||
## [X.Y.Z] - YYYY-MM-DD
|
||||
|
||||
# README.md — version badge
|
||||
version-X.Y.Z-blue
|
||||
```
|
||||
|
||||
#### Step 2: Write changelog
|
||||
|
||||
Add entries under `## [X.Y.Z]` in `CHANGELOG.md` following [Keep a Changelog](https://keepachangelog.com) format (`Added`, `Changed`, `Fixed`, `Removed`).
|
||||
|
||||
#### Step 3: Commit and tag
|
||||
|
||||
```bash
|
||||
# Stage all version bumps + changelog
|
||||
git add frontend/package.json CHANGELOG.md README.md
|
||||
|
||||
# Commit
|
||||
git commit -m "chore: bump version to X.Y.Z"
|
||||
|
||||
# Create annotated tag
|
||||
git tag -a X.Y.Z -m "Release X.Y.Z"
|
||||
|
||||
# Push commit and tag
|
||||
git push origin main
|
||||
git push origin X.Y.Z
|
||||
```
|
||||
|
||||
#### Step 4: Verify CI/CD
|
||||
|
||||
- CI workflow (`ci.yml`) runs on push to `main` → builds & pushes `main`/`latest` images.
|
||||
- Release workflow (`release.yml`) triggers on `X.Y.Z` tag → builds images with semver tags (`X.Y.Z`, `X.Y`, `latest`), creates Gitea Release.
|
||||
- CD workflow (`cd.yml`) triggers after CI completes → deploys to production.
|
||||
|
||||
Monitor at: [git.allucanget.biz/zwitschi/linearvoid/actions](https://git.allucanget.biz/zwitschi/linearvoid/actions)
|
||||
|
||||
#### Step 5: Verify release
|
||||
|
||||
Check the Gitea Releases page for the new tag:
|
||||
[git.allucanget.biz/zwitschi/linearvoid/releases](https://git.allucanget.biz/zwitschi/linearvoid/releases)
|
||||
|
||||
### Manual Release (workflow_dispatch)
|
||||
|
||||
If a tag push fails or a re-release is needed:
|
||||
|
||||
1. Go to [Actions → Release workflow](https://git.allucanget.biz/zwitschi/linearvoid/actions?workflow=release.yml)
|
||||
2. Click "Run workflow"
|
||||
3. Enter version (e.g. `1.0.2`)
|
||||
4. The workflow builds images, pushes semver tags, and creates the Gitea Release
|
||||
|
||||
### Docker Image Tags
|
||||
|
||||
Every release produces these tags in the Gitea Container Registry:
|
||||
|
||||
| Image | Tags |
|
||||
| --------- | ------------------------ |
|
||||
| `playout` | `X.Y.Z`, `X.Y`, `latest` |
|
||||
| `chat` | `X.Y.Z`, `X.Y`, `latest` |
|
||||
| `nginx` | `X.Y.Z`, `X.Y`, `latest` |
|
||||
|
||||
Example for `1.0.2`:
|
||||
|
||||
```text
|
||||
git.allucanget.biz/zwitschi/linearvoid/chat:1.0.2
|
||||
git.allucanget.biz/zwitschi/linearvoid/chat:1.0
|
||||
git.allucanget.biz/zwitschi/linearvoid/chat:latest
|
||||
git.allucanget.biz/zwitschi/linearvoid/playout:1.0.2
|
||||
git.allucanget.biz/zwitschi/linearvoid/playout:1.0
|
||||
git.allucanget.biz/zwitschi/linearvoid/playout:latest
|
||||
```
|
||||
|
||||
### Moving Completed Tasks
|
||||
|
||||
After release, update `CHANGELOG.md` if needed.
|
||||
|
||||
See the ADRs for architectural decisions and `docs/architecture/README.md` for the big picture. Thank you for contributing!
|
||||
+284
@@ -0,0 +1,284 @@
|
||||
# Deployment Guide
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Deployment Guide](#deployment-guide)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Environments](#environments)
|
||||
- [Production Target (Hetzner)](#production-target-hetzner)
|
||||
- [Planned Infrastructure](#planned-infrastructure)
|
||||
- [Cutover Plan](#cutover-plan)
|
||||
- [Firewall / Security Group](#firewall--security-group)
|
||||
- [Pipeline Readiness](#pipeline-readiness)
|
||||
- [Target Topology](#target-topology)
|
||||
- [Prerequisites](#prerequisites)
|
||||
- [Environment File](#environment-file)
|
||||
- [Pull-and-Run Deployment](#pull-and-run-deployment)
|
||||
- [First-Time or Recovery Provisioning](#first-time-or-recovery-provisioning)
|
||||
- [Health Verification](#health-verification)
|
||||
- [Zero/Low-Downtime Update Pattern](#zerolow-downtime-update-pattern)
|
||||
- [Rollback](#rollback)
|
||||
- [Operational Tasks](#operational-tasks)
|
||||
- [View logs](#view-logs)
|
||||
- [Restart one service](#restart-one-service)
|
||||
- [Stop stack](#stop-stack)
|
||||
- [Keep volumes (recommended) vs remove volumes (destructive)](#keep-volumes-recommended-vs-remove-volumes-destructive)
|
||||
- [Nginx and Proxy Considerations](#nginx-and-proxy-considerations)
|
||||
- [Security Baseline](#security-baseline)
|
||||
- [Common Deployment Pitfalls](#common-deployment-pitfalls)
|
||||
- [Troubleshooting](#troubleshooting)
|
||||
|
||||
## Environments
|
||||
|
||||
Linear Void runs in two separate environments:
|
||||
|
||||
| Environment | Host | Docker Target | Purpose |
|
||||
| -------------- | -------------------------- | -------------------------- | ---------------------------------------------- |
|
||||
| **Test Lab** | `192.168.88.40` (internal) | `tcp://192.168.88.40:2375` | Integration testing, staging, CI/CD validation |
|
||||
| **Production** | Hetzner VPS (TBD) | `tcp://<hetzner-ip>:2375` | Public linearvoid.tv serving real viewers |
|
||||
|
||||
**Current state (2026-08-06):** Test lab is active and receives automated CD deploys. Production Hetzner VPS is **not yet provisioned**. The CD workflow (`cd.yml`) and `docker-compose.prod.yml` currently target the test lab. Switching to production requires only changing `DOCKER_HOST` — see [Production Deployment](#production-target-hetzner) below.
|
||||
|
||||
## Production Target (Hetzner)
|
||||
|
||||
**Status: Not provisioned.** The production deployment plan is documented and the pipeline is ready — only the VPS needs to be created.
|
||||
|
||||
### Planned Infrastructure
|
||||
|
||||
| Resource | Spec |
|
||||
| -------------- | ------------------------------------------------ |
|
||||
| Provider | Hetzner Cloud |
|
||||
| Region | Ashburn, VA (us-east) |
|
||||
| Instance | CX22 (2 vCPU, 4 GB RAM, 40 GB SSD) or equivalent |
|
||||
| OS | Ubuntu 24.04 LTS or Debian 12 |
|
||||
| Bandwidth | 20 TB included traffic/month |
|
||||
| Estimated cost | ~€7–10/month base |
|
||||
|
||||
### Cutover Plan
|
||||
|
||||
1. Provision Hetzner VPS (see [Production Deployment](#production-target-hetzner) section)
|
||||
2. Add production secrets to Gitea (`CI_PROD_*` prefix)
|
||||
3. Update `DOCKER_HOST` in CD workflow to Hetzner public IP
|
||||
4. Trigger deploy → validate health checks
|
||||
5. Update Cloudflare A record `linearvoid.tv` → Hetzner IP
|
||||
6. Enable HTTPS, WAF, cache rules on production zone
|
||||
7. Decommission test lab as public target
|
||||
|
||||
### Firewall / Security Group
|
||||
|
||||
| Port | Source | Purpose |
|
||||
| ---- | --------------------- | ------------------------------ |
|
||||
| 22 | Admin IPs | SSH management |
|
||||
| 80 | 0.0.0.0/0 | HTTP (redirect to HTTPS) |
|
||||
| 443 | 0.0.0.0/0 | HTTPS (nginx) |
|
||||
| 2375 | Gitea runner IP only | Docker remote API (TLS) |
|
||||
| 9090 | Admin IPs or internal | Prometheus (or tunnel via SSH) |
|
||||
| 3001 | Admin IPs or internal | Grafana (or tunnel via SSH) |
|
||||
|
||||
### Pipeline Readiness
|
||||
|
||||
Three Gitea Actions workflows drive the full pipeline:
|
||||
|
||||
| Workflow | Trigger | Purpose |
|
||||
| ------------- | ---------------- | ---------------------------------------------------------------------------------------- |
|
||||
| `ci.yml` | Push to `main` | Lint, build frontend, vet Go, build & push Docker images (`:latest`, `:main`) |
|
||||
| `cd.yml` | Push to `main` | Deploy to test lab (`192.168.88.40:2375`) — runs **in parallel** with CI |
|
||||
| `release.yml` | Tag push `*.*.*` | Build & push versioned Docker images (`:X.Y.Z`, `:X.Y`, `:latest`), create Gitea Release |
|
||||
|
||||
CI and CD run simultaneously on every push to `main`. The deploy pulls whatever `:latest` images are available in the registry at that moment.
|
||||
|
||||
The CD workflow (`cd.yml`) uses `DOCKER_HOST` to target the deployment host. To switch targets:
|
||||
|
||||
```bash
|
||||
# Test lab (current)
|
||||
DOCKER_HOST=tcp://192.168.88.40:2375
|
||||
|
||||
# Production (when provisioned)
|
||||
DOCKER_HOST=tcp://<hetzner-public-ip>:2375
|
||||
```
|
||||
|
||||
No code changes needed in the pipeline. See `DEPLOYMENT.md` Production Deployment section for full checklist.
|
||||
|
||||
## Target Topology
|
||||
|
||||
- Host: Linux server with Docker Engine
|
||||
- Reverse proxy: nginx container (linearvoid-nginx)
|
||||
- Core services: playout, chat, redis, postgres
|
||||
- Observability: prometheus, grafana
|
||||
- CDN/edge: Cloudflare
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Docker Engine + Docker Compose plugin
|
||||
- Network access to git.allucanget.biz
|
||||
- A valid .env file with production values
|
||||
- DNS configured for linearvoid.tv (and media.linearvoid.tv if split media domain is used)
|
||||
|
||||
## Environment File
|
||||
|
||||
Use .env with at least:
|
||||
|
||||
- CI_REGISTRY_URL
|
||||
- CI_REGISTRY_NAMESPACE
|
||||
- CHAT_PORT
|
||||
- NGINX_PORT
|
||||
- DATABASE_URL (if overridden)
|
||||
- REDIS_URL (if overridden)
|
||||
- ADMIN_USERNAME
|
||||
- ADMIN_PASSWORD
|
||||
- VITE_PUBLIC_APP_URL
|
||||
- VITE_PUBLIC_WS_URL
|
||||
- VITE_PUBLIC_HLS_URL
|
||||
- Cloudflare/R2 variables when media sync is enabled
|
||||
|
||||
See .env.example and SETUP.md for full variable reference.
|
||||
|
||||
## Pull-and-Run Deployment
|
||||
|
||||
Images are built and pushed automatically by the CI and release workflows (see [CONTRIBUTING.md](CONTRIBUTING.md#versioning-and-releases) for the release procedure).
|
||||
|
||||
Every push to `main` triggers CI (builds `:main` and `:latest` tags) and CD (deploys `:latest` to test lab) in parallel. Semantic version tags (`*.*.*`, e.g. `1.0.9`) trigger the release workflow which builds `:X.Y.Z`, `:X.Y`, and `:latest` tags and creates a Gitea Release.
|
||||
|
||||
From repository root:
|
||||
|
||||
```bash
|
||||
docker login git.allucanget.biz
|
||||
|
||||
docker compose -f docker-compose.prod.yml pull
|
||||
docker compose -f docker-compose.prod.yml up -d --remove-orphans
|
||||
```
|
||||
|
||||
## First-Time or Recovery Provisioning
|
||||
|
||||
1. Ensure host paths exist:
|
||||
|
||||
```bash
|
||||
mkdir -p data/chat data/playlist data/media data/hls
|
||||
```
|
||||
|
||||
1. Confirm volumes and networks:
|
||||
|
||||
```bash
|
||||
docker volume ls
|
||||
docker network ls
|
||||
```
|
||||
|
||||
1. Start stack:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.prod.yml up -d
|
||||
```
|
||||
|
||||
## Health Verification
|
||||
|
||||
After startup, check service status:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.prod.yml ps
|
||||
```
|
||||
|
||||
Endpoint checks:
|
||||
|
||||
- App: <http://{host}:8080/>
|
||||
- Nginx health: <http://{host}:8080/health>
|
||||
- Chat health: <http://{host}:3000/health>
|
||||
- Playout health: <http://{host}:8081/health>
|
||||
- Prometheus: <http://{host}:9090>
|
||||
- Grafana: <http://{host}:3001>
|
||||
|
||||
Domain checks:
|
||||
|
||||
- <https://linearvoid.tv>
|
||||
- <https://linearvoid.tv/admin>
|
||||
- <https://linearvoid.tv/metrics>
|
||||
|
||||
## Zero/Low-Downtime Update Pattern
|
||||
|
||||
1. Pull newest images.
|
||||
1. Recreate changed services only:
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.prod.yml pull
|
||||
|
||||
docker compose -f docker-compose.prod.yml up -d nginx chat playout
|
||||
```
|
||||
|
||||
1. Re-run health checks.
|
||||
|
||||
## Rollback
|
||||
|
||||
If new images are unhealthy:
|
||||
|
||||
1. Pin previous tags in docker-compose.prod.yml.
|
||||
2. Pull pinned tags.
|
||||
3. Restart impacted services.
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.prod.yml pull
|
||||
|
||||
docker compose -f docker-compose.prod.yml up -d nginx chat playout
|
||||
```
|
||||
|
||||
## Operational Tasks
|
||||
|
||||
### View logs
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.prod.yml logs -f nginx chat playout
|
||||
```
|
||||
|
||||
### Restart one service
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.prod.yml restart chat
|
||||
```
|
||||
|
||||
### Stop stack
|
||||
|
||||
```bash
|
||||
docker compose -f docker-compose.prod.yml down
|
||||
```
|
||||
|
||||
### Keep volumes (recommended) vs remove volumes (destructive)
|
||||
|
||||
```bash
|
||||
# keep data
|
||||
docker compose -f docker-compose.prod.yml down
|
||||
|
||||
# destructive (remove data)
|
||||
docker compose -f docker-compose.prod.yml down -v
|
||||
```
|
||||
|
||||
## Nginx and Proxy Considerations
|
||||
|
||||
- If an external proxy (for example Nginx Proxy Manager) fronts the stack, align request body limits with internal nginx.
|
||||
- For resumable admin uploads, ensure client_max_body_size supports chunk requests in both proxy layers.
|
||||
|
||||
## Security Baseline
|
||||
|
||||
- Use strong ADMIN_PASSWORD and rotate regularly.
|
||||
- Keep Docker host access restricted.
|
||||
- Prefer HTTPS/WSS through Cloudflare.
|
||||
- Restrict exposed ports if only proxy access is needed.
|
||||
|
||||
## Common Deployment Pitfalls
|
||||
|
||||
- Missing frontend build artifacts in local-only compose workflows.
|
||||
- Wrong registry credentials or missing login.
|
||||
- Misaligned domain env vars (app/ws/hls) causing mixed-origin errors.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
| Symptom | Check | Fix |
|
||||
| -------------------------- | --------------------------------------------- | ------------------------------------------------------------- |
|
||||
| Service not starting | `docker logs linearvoid-<svc> --tail 50` | Check `.env` vars, volume mounts, port conflicts |
|
||||
| Playout stall / slate mode | `curl http://<host>:8081/health` — mode=slate | Check playlist file, media paths, disk space |
|
||||
| Chat not connecting | `curl http://<host>:3000/health` | Check Redis: `docker exec linearvoid-redis redis-cli ping` |
|
||||
| Admin 401 loop | Cookie not sent | Verify `X-Forwarded-Proto: https`, nginx auth_request headers |
|
||||
| Upload fails (413) | File too large | Check nginx `client_max_body_size` (should be 64m+) |
|
||||
| HLS not playing | `curl http://<host>/hls/playlist.m3u8` | Check playout health, HLS dir permissions |
|
||||
| Prometheus no targets | `curl http://<host>:9090/targets` | Verify container network, scrape config |
|
||||
| Grafana no data | Check datasource | Verify Prometheus URL `http://prometheus:9090` provisioned |
|
||||
|
||||
- Proxy-layer body size limits returning 413 on upload.
|
||||
- Health checks failing before service warmup is complete.
|
||||
@@ -0,0 +1,93 @@
|
||||
# DEVELOPMENT.md — Linear Void Remaining Tasks
|
||||
|
||||
> Auto-generated 2026-08-07. All completed items moved to `.github/instructions/DONE.md`.
|
||||
|
||||
## Test Fixtures (BLOCKED — requires ffmpeg)
|
||||
|
||||
- [ ] Create test media fixtures in `backend/playout/testdata/`:
|
||||
- `valid_720p30.mp4` — small (1-2s) H.264/AAC compliant file
|
||||
- `invalid_h265.mp4` — wrong codec
|
||||
- `invalid_24fps.mp4` — wrong framerate
|
||||
- Generate via `ffmpeg` commands documented in test file headers
|
||||
|
||||
- [ ] Create test playlist fixtures in `backend/playout/testdata/`:
|
||||
- `valid_playlist.txt`, `empty_playlist.txt`, `comments_only.txt`, `mixed_valid_invalid.txt`
|
||||
|
||||
## User Frontend Tests
|
||||
|
||||
- [ ] Video player behavior (play, pause, seek, buffering, error handling)
|
||||
- [ ] Chat functionality (send/receive messages, presence, rate limiting, moderation)
|
||||
- [ ] State management and data flow
|
||||
- [ ] Responsive design and layout across different screen sizes
|
||||
- [ ] Accessibility testing (keyboard navigation, screen reader support)
|
||||
- [ ] Integration with backend APIs (mocking API responses for testing)
|
||||
- [ ] Test coverage for all critical frontend components and interactions
|
||||
- [ ] Add code coverage reporting and enforce minimum coverage threshold in CI/CD pipeline
|
||||
- [ ] Document test cases and expected outcomes
|
||||
|
||||
## Admin Frontend Tests
|
||||
|
||||
- [ ] Playlist management UI (add/remove videos, reorder, normalize)
|
||||
- [ ] User moderation tools (mute/ban/report users, view moderation logs)
|
||||
- [ ] Metrics and monitoring dashboards (Prometheus/Grafana integration)
|
||||
- [ ] State management and data flow
|
||||
- [ ] Responsive design and layout across different screen sizes
|
||||
- [ ] Accessibility testing (keyboard navigation, screen reader support)
|
||||
- [ ] Integration with backend APIs (mocking API responses for testing)
|
||||
- [ ] Test coverage for all critical admin UI components and interactions
|
||||
- [ ] Add code coverage reporting and enforce minimum coverage threshold in CI/CD pipeline
|
||||
- [ ] Document test cases and expected outcomes
|
||||
|
||||
## Cloudflare R2 Integration Test
|
||||
|
||||
- [ ] Upload test HLS segments and manifests to R2 bucket
|
||||
- [ ] Verify correct cache headers are applied (immutable segments, TTLs)
|
||||
- [ ] Test retrieval of segments and manifests from R2 via Cloudflare edge
|
||||
- [ ] Validate cache hit/miss behavior and performance metrics
|
||||
- [ ] Ensure integration test can run in CI/CD pipeline with proper mocking/stubbing of R2 API
|
||||
- [ ] Document test cases and expected outcomes
|
||||
|
||||
## Cloudflare Configuration (manual — Cloudflare dashboard)
|
||||
|
||||
> Evidence tracker: `docs/operations/cloudflare-uptimerobot-checklist.md`
|
||||
|
||||
- [ ] Cloudflare Pages: link Gitea repo, build `cd frontend && npm run build`, output `frontend/dist`
|
||||
- [ ] Tiered Cache: enable Smart Tiered Cache, validate `Cf-Cache-Status: HIT` from upper tier
|
||||
- [ ] WAF: enable OWASP Managed Ruleset, set to Simulate → review 48h → Block, enable Bot Fight Mode
|
||||
- [ ] Rate limiting: `/api/admin/login` + `/api/admin/*` → 10 req/10s per IP, Challenge action
|
||||
- [ ] HTTPS/WSS: Always Use HTTPS, HSTS max-age=31536000 includeSubDomains preload, min TLS 1.2
|
||||
- [ ] HTTPS/WSS: enforce at edge (Cloudflare) and origin (nginx), redirect HTTP → HTTPS
|
||||
|
||||
## External Services
|
||||
|
||||
> Evidence tracker: `docs/operations/cloudflare-uptimerobot-checklist.md`
|
||||
|
||||
- [ ] UptimeRobot: add HTTP monitor for `https://linearvoid.tv/health` (5min interval)
|
||||
- [ ] UptimeRobot: add keyword monitor for `https://linearvoid.tv/hls/playlist.m3u8` (keyword: `#EXTM3U`, 5min)
|
||||
- [ ] Alert on 2 consecutive failures, notify via email + Discord/Slack webhook
|
||||
|
||||
## Production Deployment (Hetzner — not provisioned)
|
||||
|
||||
- [ ] Provision Hetzner CX22 (2 vCPU, 4 GB, 40 GB) in Ashburn VA, Ubuntu 24.04
|
||||
- [ ] Install Docker Engine + Compose plugin, open firewall: 80/443 (public), 2375 (runner IP only)
|
||||
- [ ] Configure Docker daemon for remote TLS access (`-H tcp://0.0.0.0:2376` with certs)
|
||||
- [ ] Create `/srv/linearvoid/data/{playlist,media,hls,chat,slate}` directories
|
||||
- [ ] Add `CI_PROD_*` secrets to Gitea: DOCKER_HOST, R2 keys, admin password, DB password, Grafana password
|
||||
- [ ] Trigger CD workflow with `DOCKER_HOST` pointing to Hetzner IP
|
||||
- [ ] Verify all 11 containers start healthy, check `/health`, `/hls/playlist.m3u8`, WebSocket
|
||||
- [ ] Update Cloudflare A record `linearvoid.tv` → Hetzner IP, SSL Full (strict), test HTTPS + WSS
|
||||
- [ ] Smoke test: viewer SPA loads, HLS plays, chat connects, admin login → upload → playlist → playout
|
||||
- [ ] Set up Prometheus alerting notifications + UptimeRobot on production URL
|
||||
- [ ] Run `scripts/backup-db.sh` + `scripts/verify-backups.sh` on production host
|
||||
- [ ] Document production IP, SSH access, rollback procedure, scaling thresholds
|
||||
|
||||
## Scaling & Performance (planning only)
|
||||
|
||||
- [ ] Tune manifest TTL: measure `Cf-Cache-Status` distribution, adjust to 0.5× segment duration
|
||||
- [ ] Measure TTFB: WebPageTest from 3+ US regions, target <4s warm cache, document baseline
|
||||
- [ ] Load-test WebSocket: `artillery` or Go script, ramp 10→100→500→1000 connections, measure p95 latency + errors
|
||||
- [ ] Benchmark admin API: `wrk`/`k6` against `/api/admin/*`, target p95 <1s, identify slow queries
|
||||
- [ ] Track cache hit ratio: Cloudflare GraphQL → Grafana panel, alert <90%
|
||||
- [ ] Capacity triggers: Prometheus rules for cache-miss >10%/15min, chat p95 >1s/5min, CPU >80%/10min
|
||||
- [ ] Phase 2 plan: document boundaries (playout CPU, chat latency, DB state, monitoring), estimate per-service resources
|
||||
- [ ] Phase 3 plan: research second Hetzner location, design warm standby + DNS failover + DB/Redis replication
|
||||
@@ -0,0 +1,91 @@
|
||||
# Monitoring Guide
|
||||
|
||||
Prometheus + Grafana monitoring for Linear Void.
|
||||
|
||||
## Architecture
|
||||
|
||||
```text
|
||||
┌──────────────┐ ┌──────────────┐ ┌───────────┐
|
||||
│ Exporters │───> │ Prometheus │───>│ Grafana │
|
||||
│ (7 targets) │ │ :9090 │ │ :3001 │
|
||||
└──────────────┘ └───────┬──────┘ └───────────┘
|
||||
│
|
||||
alerts.yml (8 rules)
|
||||
```
|
||||
|
||||
## Scrape Targets
|
||||
|
||||
| Job | Target | Port | Metrics |
|
||||
| ---------- | ----------------- | ---- | ------------------------------------------------------------- |
|
||||
| chat | chat | 3000 | WebSocket clients, messages, DMs, moderation, Redis, sessions |
|
||||
| playout | playout | 8081 | Restarts, mode (live/slate) |
|
||||
| nginx | nginx-exporter | 9113 | Requests, connections |
|
||||
| redis | redis-exporter | 9121 | Memory, clients, commands |
|
||||
| postgres | postgres-exporter | 9187 | Connections, queries, locks |
|
||||
| node | node-exporter | 9100 | CPU, memory, disk, network |
|
||||
| prometheus | prometheus | 9090 | Self-monitoring |
|
||||
|
||||
Config: `monitoring/prometheus/prometheus.yml` (15s scrape interval)
|
||||
|
||||
## Alerting Rules
|
||||
|
||||
8 rules in `monitoring/prometheus/alerts.yml`:
|
||||
|
||||
| Alert | Severity | Expression |
|
||||
| -------------------- | -------- | -------------------------------------- |
|
||||
| ChatDown | critical | `up{job="chat"} == 0` for 2m |
|
||||
| PlayoutDown | critical | `up{job="playout"} == 0` for 2m |
|
||||
| RedisDown | warning | `linearvoid_chat_redis_up == 0` for 2m |
|
||||
| PlayoutSlateActive | warning | `linearvoid_playout_mode == 1` for 1m |
|
||||
| HighChatLatency | warning | message rate <0.01/s for 5m |
|
||||
| PrometheusWALFailing | warning | WAL write failures >0 for 5m |
|
||||
| HighCPU | warning | CPU >80% for 10m |
|
||||
| LowDiskSpace | warning | Disk <10% free for 5m |
|
||||
|
||||
## Access
|
||||
|
||||
- Prometheus: `http://<host>:9090` (targets, graph, alerts)
|
||||
- Grafana: `http://<host>:3001` (default: admin/admin, change via `GRAFANA_PASSWORD`)
|
||||
|
||||
## Dashboards
|
||||
|
||||
- **Linear Void app**: `monitoring/grafana/dashboard.json` — clients, messages, moderation, Redis, sessions, scrape health
|
||||
- **Prometheus ops**: `monitoring/grafana/dashboards/dashboard.json` — TSDB, CPU/memory, WAL, compactions, query latency
|
||||
|
||||
## Key Metrics
|
||||
|
||||
- `linearvoid_chat_connected_clients` — active WebSocket connections
|
||||
- `linearvoid_chat_messages_total` — total stored messages
|
||||
- `linearvoid_chat_dms_total` — private messages sent
|
||||
- `linearvoid_chat_redis_up` — Redis reachable (0/1)
|
||||
- `linearvoid_playout_restarts_total` — FFmpeg restart counter
|
||||
- `linearvoid_playout_mode` — 0=live, 1=slate, 2=degraded
|
||||
- `linearvoid_chat_admin_sessions_active` — active admin logins
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
| Symptom | Check |
|
||||
| ----------------------- | -------------------------------------------------------------------- |
|
||||
| No chat metrics | `curl http://chat:3000/metrics` from Prometheus container |
|
||||
| No exporter metrics | Verify exporter containers running: `docker ps \| grep exporter` |
|
||||
| Grafana no data | Datasource URL must be `http://prometheus:9090` |
|
||||
| Prometheus targets down | Check container network: all must be on `linearvoid-net` |
|
||||
| Alert not firing | Check Prometheus → Alerts tab, verify `rule_files` in prometheus.yml |
|
||||
|
||||
### Grafana shows no data
|
||||
|
||||
- Ensure datasource points to <http://prometheus:9090>.
|
||||
- Confirm Prometheus is healthy.
|
||||
- Check dashboard query labels match active jobs.
|
||||
|
||||
### High cardinality or query slowness
|
||||
|
||||
- Reduce unneeded labels.
|
||||
- Limit expensive dashboard panels.
|
||||
- Tune retention and query windows.
|
||||
|
||||
## Operational Recommendations
|
||||
|
||||
- Keep alert thresholds tied to actual user impact.
|
||||
- Review dashboards after each release that changes endpoints or metrics names.
|
||||
- Periodically test alert routing and incident response flow.
|
||||
@@ -7,13 +7,15 @@
|
||||
[](https://git.allucanget.biz/zwitschi/linearvoid/actions)
|
||||
[](LICENSE)
|
||||
[](https://git.allucanget.biz)
|
||||
[](https://git.allucanget.biz/zwitschi/linearvoid/releases)
|
||||
[](CHANGELOG.md)
|
||||
|
||||
## Architecture
|
||||
|
||||
The architecture (detailed in [`docs/architecture/README.md`](docs/architecture/README.md)) uses:
|
||||
|
||||
- Hetzner Ashburn VPS for playout (FFmpeg + MediaMTX/SRS) and chat.
|
||||
- Cloudflare R2 + CDN with aggressive immutable caching of HLS `.ts` segments ($0 egress).
|
||||
- Hetzner Ashburn VPS for playout (FFmpeg + MediaMTX/SRS) and chat _(planned production; currently test lab at 192.168.88.40)_.
|
||||
- Cloudflare R2 + CDN with aggressive immutable caching of `.m4s` segments ($0 egress).
|
||||
- Lightweight static frontend and WebSocket chat backend.
|
||||
- Gitea self-hosted CI/CD (Actions) and Container Registry.
|
||||
|
||||
@@ -27,6 +29,23 @@ See [`docs/architecture/adr/`](docs/architecture/adr/) for the full set of decis
|
||||
- Video normalization, monitoring, fallback slate, and easy playlist management.
|
||||
- US-centric performance (Ashburn origin + Cloudflare POPs).
|
||||
|
||||
## Architecture Diagram
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
V[US Viewers] -->|HTTPS| CF[Cloudflare CDN]
|
||||
CF -->|Cache HIT| R2[Cloudflare R2]
|
||||
CF -->|Cache MISS| N[Nginx :80]
|
||||
N -->|/ws| CH[Chat :3000]
|
||||
N -->|/admin| CH
|
||||
N -->|/hls| P[Playout :8081]
|
||||
N -->|/*| FE[Frontend SPA]
|
||||
CH --> R[(Redis)]
|
||||
CH --> DB[(PostgreSQL)]
|
||||
P -->|ffmpeg| HLS[/data/hls/]
|
||||
P -->|sync| R2
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```text
|
||||
@@ -93,13 +112,22 @@ This structure directly reflects the building blocks, runtime flows, deployment
|
||||
|
||||
## Getting Started
|
||||
|
||||
> **Note:** The `docker compose up` below targets local development. The test lab (192.168.88.40) receives automated CD deploys. Production Hetzner VPS is not yet provisioned — see `DEPLOYMENT.md`.
|
||||
|
||||
```bash
|
||||
git clone https://git.allucanget.biz/zwitschi/linearvoid.git
|
||||
cd linearvoid
|
||||
cp .env.example .env
|
||||
# Edit .env with your R2 credentials and domain settings
|
||||
docker compose up --build
|
||||
```
|
||||
|
||||
Then open:
|
||||
|
||||
- **Viewer**: [https://linearvoid.tv](https://linearvoid.tv) (or `http://localhost`)
|
||||
- **Admin**: [https://linearvoid.tv/admin](https://linearvoid.tv/admin) (or `http://localhost/admin`)
|
||||
- **Metrics**: [https://linearvoid.tv/metrics](https://linearvoid.tv/metrics)
|
||||
|
||||
Full setup guide: [`SETUP.md`](SETUP.md).
|
||||
|
||||
See the [architecture docs](docs/architecture/README.md) for detailed architecture and ADRs.
|
||||
@@ -107,10 +135,55 @@ See the [architecture docs](docs/architecture/README.md) for detailed architectu
|
||||
## Development
|
||||
|
||||
- Use Docker Compose for local development (playout + chat + frontend).
|
||||
- Test lab host: `192.168.88.40` (internal network, receives CD deploys). Production Hetzner VPS TBD.
|
||||
- Public production host model: `https://linearvoid.tv` for viewer/admin/chat and `https://media.linearvoid.tv` for HLS delivery.
|
||||
- Normalize all source videos before adding to playlist (see ADR-001 for exact codec/fps/keyframe requirements).
|
||||
- Monitor cache hit ratio and R2 operations closely — they determine real cost.
|
||||
|
||||
### Environment Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
| ---------------------- | ----------------------------------------------- | -------------------------------- |
|
||||
| `R2_ACCESS_KEY_ID` | — | Cloudflare R2 access key |
|
||||
| `R2_SECRET_ACCESS_KEY` | — | Cloudflare R2 secret key |
|
||||
| `R2_BUCKET_NAME` | `linearvoid-media` | R2 bucket for HLS segments |
|
||||
| `DATABASE_URL` | `postgres://...@db:5432/...` | Chat DB (Postgres or SQLite) |
|
||||
| `REDIS_URL` | `redis://redis:6379/0` | Redis for presence/rate limiting |
|
||||
| `VITE_PUBLIC_APP_URL` | `https://linearvoid.tv` | Public viewer URL |
|
||||
| `VITE_PUBLIC_WS_URL` | `wss://linearvoid.tv/ws` | WebSocket endpoint |
|
||||
| `VITE_PUBLIC_HLS_URL` | `https://media.linearvoid.tv/hls/playlist.m3u8` | HLS playlist URL |
|
||||
| `ADMIN_USERNAME` | `admin` | Admin panel username |
|
||||
| `ADMIN_PASSWORD` | — | Admin panel password |
|
||||
| `HLS_SEGMENT_DURATION` | `6` | HLS segment duration in seconds |
|
||||
| `HLS_WINDOW_SIZE` | `24` | HLS playlist window size |
|
||||
|
||||
Full reference: [`.env.example`](.env.example).
|
||||
|
||||
### Troubleshooting
|
||||
|
||||
| Problem | Check |
|
||||
| ------------------------ | ------------------------------------------------------------------------------------- |
|
||||
| **Playout not starting** | Verify playlist exists in `data/playlist/playlist.txt` and media files are accessible |
|
||||
| **HLS not playing** | Check `docker compose logs playout` for ffmpeg errors; verify `data/hls/` permissions |
|
||||
| **Chat not connecting** | Ensure Redis is running: `docker compose logs redis`; check `DATABASE_URL` |
|
||||
| **Admin panel 401** | Verify `ADMIN_USERNAME`/`ADMIN_PASSWORD` in `.env` match login credentials |
|
||||
| **Docker build fails** | Run `docker compose build --no-cache`; check `backend/playout/.dockerignore` |
|
||||
| **R2 upload failures** | Verify `R2_ACCESS_KEY_ID` and `R2_SECRET_ACCESS_KEY`; check bucket CORS settings |
|
||||
|
||||
### API Endpoints
|
||||
|
||||
| Service | Endpoint | Description |
|
||||
| ----------- | ------------------------ | -------------------------- |
|
||||
| **Chat** | `GET /health` | Health check |
|
||||
| **Chat** | `GET /metrics` | Prometheus metrics |
|
||||
| **Chat** | `GET /ws` | WebSocket upgrade for chat |
|
||||
| **Chat** | `GET /api/admin/*` | Admin API (auth required) |
|
||||
| **Chat** | `POST /api/admin/login` | Admin login |
|
||||
| **Playout** | `GET /health` | Health check (port 8081) |
|
||||
| **Nginx** | `GET /` | Viewer SPA |
|
||||
| **Nginx** | `GET /admin/` | Admin panel |
|
||||
| **Nginx** | `GET /hls/playlist.m3u8` | HLS manifest |
|
||||
|
||||
## Architecture Documentation
|
||||
|
||||
The architecture is fully documented using the [arc42 template](docs/architecture/README.md) (with populated sections, Mermaid context and building-block diagrams, deployment view, cross-cutting concepts, quality goals, risks, and glossary).
|
||||
@@ -134,6 +207,11 @@ Contributions, issues, and further evolution (LL-HLS, ABR, multi-channel, E2EE)
|
||||
- [CHANGELOG.md](CHANGELOG.md) - Version history and completed features.
|
||||
- [CONTRIBUTING.md](CONTRIBUTING.md) - Guidelines for contributing to the project.
|
||||
- [SETUP.md](SETUP.md) - Complete setup guide with prerequisites, environment configuration, running the application, and troubleshooting.
|
||||
- [API.md](API.md) - Implemented WebSocket and REST API reference.
|
||||
- [DEPLOYMENT.md](DEPLOYMENT.md) - Production deployment and operations guide.
|
||||
- [MONITORING.md](MONITORING.md) - Prometheus and Grafana setup and troubleshooting.
|
||||
- [SECURITY.md](SECURITY.md) - Security controls, hardening checklist, and incident baseline.
|
||||
- [Scaling Playbook](docs/operations/scaling-performance-playbook.md) - Performance test method, thresholds, and scaling decision flow.
|
||||
- [Architecture Documentation](docs/architecture/README.md) - Full arc42 template with diagrams and decisions.
|
||||
- [ADRs](docs/architecture/adr/) - Detailed decision records (ADR-001 to ADR-007).
|
||||
|
||||
@@ -141,4 +219,17 @@ Contributions, issues, and further evolution (LL-HLS, ABR, multi-channel, E2EE)
|
||||
|
||||
**Container Registry**: `git.allucanget.biz/zwitschi/linearvoid/<service>:latest`
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! See [`CONTRIBUTING.md`](CONTRIBUTING.md) for guidelines. Quick overview:
|
||||
|
||||
1. Read the [architecture docs](docs/architecture/README.md) and ADRs first.
|
||||
2. Code against the defined module structure (frontend, backend/playout, backend/chat, infrastructure, scripts, monitoring).
|
||||
3. Open PRs on [Gitea](https://git.allucanget.biz/zwitschi/linearvoid).
|
||||
4. CI validates builds and tests automatically.
|
||||
|
||||
Since this is a linear project, we also rely on a linear development and release model: only `main` branch is used for releases, and all features are merged into `main` after review and CI validation. Releases use semantic version tags in `*.*.*` format (for example `1.0.9`) and are automatically built/pushed to the container registry. Version numbers are declared in `frontend/package.json`, `CHANGELOG.md`, and the version badge in this README.
|
||||
|
||||
[MIT License](LICENSE)
|
||||
|
||||
The project is ready for use. See [`SETUP.md`](SETUP.md) for detailed installation instructions.
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
# Security Guide
|
||||
|
||||
Layered security model for Linear Void.
|
||||
|
||||
## Threat Model
|
||||
|
||||
```
|
||||
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
|
||||
│ Client │───▶│Cloudflare│───▶│ nginx │───▶│ Backend │
|
||||
│ (public) │ │ WAF+CDN │ │ proxy │ │ chat │
|
||||
└──────────┘ └──────────┘ └────┬─────┘ └────┬─────┘
|
||||
│ │
|
||||
┌──────┴──────┐ ┌─────┴──────┐
|
||||
│ /health │ │ /api/admin │
|
||||
│ /metrics │ │ /ws (chat) │
|
||||
│ /hls/* │ │ /api/chat/* │
|
||||
│ (public) │ │ (auth req) │
|
||||
└─────────────┘ └────────────┘
|
||||
```
|
||||
|
||||
## Authentication & Sessions
|
||||
|
||||
- Login: `POST /api/admin/login` → cookie `linearvoid_admin_session`
|
||||
- Cookie flags: `HttpOnly`, `SameSite=Strict`, `Secure` (over HTTPS)
|
||||
- Server-side expiry via `ADMIN_SESSION_TTL_HOURS`
|
||||
- Rate limit: 5 attempts/IP/5min via Redis
|
||||
|
||||
## Transport Security (TLS/HSTS)
|
||||
|
||||
- Cloudflare TLS mode: **Full (strict)** — valid origin cert required
|
||||
- nginx listens on 80 internally; Cloudflare handles TLS termination
|
||||
- HSTS header set by Cloudflare: `max-age=31536000; includeSubDomains; preload`
|
||||
- WSS upgrade path: `/ws` through nginx → chat:3000
|
||||
|
||||
## Network Isolation
|
||||
|
||||
- All backend services on internal Docker network (`linearvoid-net`)
|
||||
- Only nginx port exposed to host (443/80)
|
||||
- Database, Redis, exporters inaccessible from outside
|
||||
- `docker-compose.prod.yml` keeps ports internal via `expose:` not `ports:`
|
||||
|
||||
## Reverse Proxy Controls
|
||||
|
||||
- `auth_request /api/admin/auth` gate on all `/admin` and `/api/admin` paths
|
||||
- Explicit `client_max_body_size 64m` for resumable upload chunks
|
||||
- WebSocket upgrade headers: `Upgrade`, `Connection` preserved
|
||||
- CORS headers on HLS: `Access-Control-Allow-Origin *`
|
||||
- XSS protection: CSP headers served by Cloudflare
|
||||
|
||||
## Rate Limiting
|
||||
|
||||
| Resource | Limit | Window |
|
||||
| ----------------- | ----- | ------ |
|
||||
| WebSocket connect | 5/IP | 60s |
|
||||
| Chat messages | 10/IP | 10s |
|
||||
| Admin login | 5/IP | 5min |
|
||||
|
||||
Implemented via Redis `INCR` + `EXPIRE`.
|
||||
|
||||
## Secrets Management
|
||||
|
||||
- `.env` — local dev only, **gitignored**
|
||||
- Gitea Actions secrets (`CI_*` prefix) for CI/CD
|
||||
- Never commit: passwords, tokens, API keys, R2 credentials
|
||||
- Production `.env` provisioned manually on host, never checked in
|
||||
- Rotate admin password quarterly
|
||||
|
||||
## Data Protection
|
||||
|
||||
- PostgreSQL: chat history, moderation logs, media metadata
|
||||
- Redis: append-only persistence for presence, rate limits, sessions
|
||||
- HLS segments: ephemeral, regenerated by playout
|
||||
- Backups: `scripts/backup-db.sh` (daily), `scripts/backup-weekly.sh`, `scripts/verify-backups.sh`
|
||||
|
||||
## Hardening Checklist
|
||||
|
||||
- [ ] HTTPS/WSS enforced end-to-end (Cloudflare Full strict)
|
||||
- [ ] Strong admin credentials configured
|
||||
- [ ] Secrets only in env/secrets stores (never committed)
|
||||
- [ ] Proxy auth and websocket headers validated
|
||||
- [ ] Upload limits and MIME validation active
|
||||
- [ ] Rate limiting active (WS, messages, login)
|
||||
- [ ] Cloudflare WAF/rate-limit rules active
|
||||
- [ ] Backup/restore drills completed
|
||||
- [ ] Security alerts routed (Prometheus alertmanager)
|
||||
|
||||
## Incident Response
|
||||
|
||||
1. Detect — alerts (ChatDown, RedisDown, HighChatLatency) or user reports
|
||||
2. Triage — edge (Cloudflare), proxy (nginx), app (chat/playout), data (Redis/Postgres)
|
||||
3. Contain — rate limit, credential rotate, service restart, slate mode activation
|
||||
4. Recover — verify health endpoints, check Prometheus targets, validate HLS output
|
||||
5. Record — timeline + corrective actions in `docs/incident-runbook.md`
|
||||
@@ -21,6 +21,42 @@ This guide covers prerequisites, environment setup, running the application loca
|
||||
- `DATABASE_URL`: The production database connection string (e.g., `postgres://linearvoid:<password>@db:5432/linearvoid?sslmode=disable`)
|
||||
- **Hetzner Cloud Account** (optional for production): For the US-East VPS (Ashburn, VA recommended per ADR-001).
|
||||
|
||||
## Network Environments
|
||||
|
||||
Linear Void operates in two distinct environments. Know which one you're targeting.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph Test_Lab["Test Lab (192.168.88.0/24)"]
|
||||
TEST_HOST["192.168.88.40<br/>Docker Engine :2375"]
|
||||
end
|
||||
|
||||
subgraph Prod["Production (Hetzner — NOT provisioned)"]
|
||||
PROD_HOST["Hetzner CX22 Ashburn, VA<br/>Public IP TBD"]
|
||||
end
|
||||
|
||||
subgraph Gitea["Gitea CI/CD"]
|
||||
CI["CI: build + push images"]
|
||||
CD["CD: deploy via DOCKER_HOST"]
|
||||
end
|
||||
|
||||
subgraph CF["Cloudflare"]
|
||||
DNS["linearvoid.tv"]
|
||||
R2["R2 Bucket"]
|
||||
end
|
||||
|
||||
CD -->|"DOCKER_HOST=tcp://192.168.88.40:2375"| TEST_HOST
|
||||
CD -.->|"DOCKER_HOST=tcp://<hetzner-ip>:2375<br/>(when provisioned)"| PROD_HOST
|
||||
TEST_HOST -->|"test traffic"| DNS
|
||||
PROD_HOST -.->|"production traffic"| DNS
|
||||
TEST_HOST --> R2
|
||||
PROD_HOST -.-> R2
|
||||
```
|
||||
|
||||
**Test Lab (active):** Internal network `192.168.88.0/24`, host `192.168.88.40`. Receives automated CD deploys from Gitea Actions. Used for integration testing and staging validation.
|
||||
|
||||
**Production (pending):** Hetzner Cloud VPS, Ashburn VA. Not yet provisioned. CD workflow and compose files are ready — only `DOCKER_HOST` needs changing. See Production Deployment section in [DEPLOYMENT.md](./DEPLOYMENT.md) for cutover plan.
|
||||
|
||||
## Development Prerequisites
|
||||
|
||||
- **Frontend dependencies**: Run `cd frontend && npm install` before `npm run dev` or `npm run build`.
|
||||
@@ -126,39 +162,6 @@ This guide covers prerequisites, environment setup, running the application loca
|
||||
- Place normalized MP4 files in `data/media/`.
|
||||
- Create a `data/playlist/playlist.txt` with paths (see ADR-001 for normalization requirements).
|
||||
|
||||
## Cloudflare R2 Setup
|
||||
|
||||
1. **Create an R2 bucket**:
|
||||
- Create a bucket such as `linearvoid-media` in Cloudflare R2.
|
||||
- Keep the bucket name aligned with `R2_BUCKET_NAME` in `.env`.
|
||||
|
||||
2. **Generate R2 API credentials**:
|
||||
- Create an access key pair with read/write permissions for the bucket.
|
||||
- Set `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, and `R2_REGION=auto` in `.env`.
|
||||
|
||||
3. **Find the S3 endpoint**:
|
||||
- Use the account-scoped endpoint format `https://<account-id>.r2.cloudflarestorage.com`.
|
||||
- Store the account ID in both `R2_ENDPOINT` and `CLOUDFLARE_ACCOUNT_ID` where applicable.
|
||||
|
||||
4. **Bind a custom media domain**:
|
||||
- Attach a domain such as `media.linearvoid.tv` to the bucket.
|
||||
- Set `MEDIA_DOMAIN` to that hostname.
|
||||
|
||||
5. **Configure app-domain DNS and TLS**:
|
||||
- Point `linearvoid.tv` to the public entrypoint serving nginx.
|
||||
- Point `www.linearvoid.tv` to the same target and redirect it to `https://linearvoid.tv`.
|
||||
- Keep `media.linearvoid.tv` on the R2 custom domain path so HLS delivery stays split from the app origin.
|
||||
- Use Cloudflare proxied DNS with Full (strict) TLS so browser traffic uses `https://linearvoid.tv`, `wss://linearvoid.tv/ws`, and `https://media.linearvoid.tv`.
|
||||
|
||||
6. **Apply cache behavior**:
|
||||
- Configure the `.ts` and `.m3u8` cache rules from `infrastructure/cloudflare/cache-rules.md`.
|
||||
- Enable Smart Tiered Cache for the R2-backed hostname.
|
||||
- If you need header normalization for immutable segment caching, deploy `infrastructure/cloudflare/worker.js` to the same zone.
|
||||
|
||||
7. **Verify access**:
|
||||
- Confirm uploads with an S3-compatible client such as `aws s3 ls --endpoint-url <R2_ENDPOINT>`.
|
||||
- After deployment, check Cloudflare analytics for cache hit ratio and origin request volume.
|
||||
|
||||
## Gitea Setup
|
||||
|
||||
1. **Create or verify a Gitea user**:
|
||||
@@ -195,6 +198,160 @@ This guide covers prerequisites, environment setup, running the application loca
|
||||
7. **Validate image availability**:
|
||||
- Before a production rollout, verify that the expected `:latest` tags can be pulled from the registry on the target server.
|
||||
|
||||
## Cloudflare R2 Setup
|
||||
|
||||
1. **Create an R2 bucket**:
|
||||
- Create a bucket such as `linearvoid-media` in Cloudflare R2.
|
||||
- Keep the bucket name aligned with `R2_BUCKET_NAME` in `.env`.
|
||||
|
||||
2. **Generate R2 API credentials**:
|
||||
- Create an access key pair with read/write permissions for the bucket.
|
||||
- Set `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, and `R2_REGION=auto` in `.env`.
|
||||
|
||||
3. **Find the S3 endpoint**:
|
||||
- Use the account-scoped endpoint format `https://<account-id>.r2.cloudflarestorage.com`.
|
||||
- Store the account ID in both `R2_ENDPOINT` and `CLOUDFLARE_ACCOUNT_ID` where applicable.
|
||||
|
||||
4. **Bind a custom media domain**:
|
||||
- Attach a domain such as `media.linearvoid.tv` to the bucket.
|
||||
- Set `MEDIA_DOMAIN` to that hostname.
|
||||
|
||||
5. **Configure app-domain DNS and TLS**:
|
||||
- Point `linearvoid.tv` to the public entrypoint serving nginx.
|
||||
- Point `www.linearvoid.tv` to the same target and redirect it to `https://linearvoid.tv`.
|
||||
- Keep `media.linearvoid.tv` on the R2 custom domain path so HLS delivery stays split from the app origin.
|
||||
- Use Cloudflare proxied DNS with Full (strict) TLS so browser traffic uses `https://linearvoid.tv`, `wss://linearvoid.tv/ws`, and `https://media.linearvoid.tv`.
|
||||
|
||||
6. **Apply cache behavior**:
|
||||
- Configure the `.m4s` and `.m3u8` cache rules from `infrastructure/cloudflare/cache-rules.md`.
|
||||
- Enable Smart Tiered Cache for the R2-backed hostname.
|
||||
- If you need header normalization for immutable segment caching, deploy `infrastructure/cloudflare/worker.js` to the same zone.
|
||||
|
||||
7. **Verify access**:
|
||||
- Confirm uploads with an S3-compatible client such as `aws s3 ls --endpoint-url <R2_ENDPOINT>`.
|
||||
- After deployment, check Cloudflare analytics for cache hit ratio and origin request volume.
|
||||
|
||||
## Cloudflare
|
||||
|
||||
Use this section for full edge setup (DNS, Pages, cache, security, and monitoring).
|
||||
|
||||
Execution tracker: `docs/operations/cloudflare-uptimerobot-checklist.md`.
|
||||
|
||||
### 1. DNS Setup
|
||||
|
||||
1. Add apex record for `linearvoid.tv` pointing to Cloudflare Pages target.
|
||||
2. Add `www` CNAME to Cloudflare Pages and configure redirect to `https://linearvoid.tv`.
|
||||
3. Add `media.linearvoid.tv` for split-domain HLS delivery.
|
||||
4. Keep all records proxied (orange cloud) unless troubleshooting requires DNS-only mode.
|
||||
|
||||
### 2. R2 Bucket
|
||||
|
||||
1. Create bucket `linearvoid-media`.
|
||||
2. Set location hint to `enam` (US-East).
|
||||
3. Attach custom domain `media.linearvoid.tv` to the bucket.
|
||||
4. Verify object read works from the custom domain.
|
||||
|
||||
### 3. Cache Rules
|
||||
|
||||
Configure Cloudflare Cache Rules:
|
||||
|
||||
1. HLS segment files (`*.ts`, `*.m4s`):
|
||||
- Cache: eligible
|
||||
- Edge TTL: `31536000`
|
||||
- Browser TTL: long-lived
|
||||
- Cache key: include full path
|
||||
2. HLS manifests (`*.m3u8`):
|
||||
- Cache: eligible
|
||||
- Edge TTL: `2s`
|
||||
- Revalidation: enabled
|
||||
3. Static assets (`*.js`, `*.css`):
|
||||
- Cache: eligible
|
||||
- Edge TTL: `1y`
|
||||
- Browser TTL: long-lived
|
||||
- Cache key: include full path
|
||||
4. Validate behavior using response headers:
|
||||
- `Cf-Cache-Status` should move from `MISS` to `HIT` for hot objects
|
||||
|
||||
Reference: `infrastructure/cloudflare/cache-rules.md`.
|
||||
|
||||
### 4. Cloudflare Pages
|
||||
|
||||
1. Connect Gitea repository to Cloudflare Pages.
|
||||
2. Build command:
|
||||
|
||||
```bash
|
||||
cd frontend && npm run build
|
||||
```
|
||||
|
||||
3. Output directory:
|
||||
|
||||
```text
|
||||
frontend/dist
|
||||
```
|
||||
|
||||
4. Configure production domain to `linearvoid.tv`.
|
||||
|
||||
### 5. Tiered Cache
|
||||
|
||||
1. Enable Smart Tiered Cache in Cloudflare dashboard.
|
||||
2. Warm a segment and manifest path.
|
||||
3. Confirm tiered behavior through repeated requests and `Cf-Cache-Status: HIT`.
|
||||
|
||||
### 6. WAF and Bot Protection
|
||||
|
||||
1. Enable OWASP Managed Ruleset.
|
||||
2. Start in `Simulate` mode.
|
||||
3. Review events for 48 hours.
|
||||
4. Switch to `Block` once false positives are addressed.
|
||||
5. Enable Bot Fight Mode.
|
||||
|
||||
### 7. Rate Limiting
|
||||
|
||||
Create rules:
|
||||
|
||||
1. Path: `/api/admin/login`
|
||||
2. Path: `/api/admin/*`
|
||||
3. Threshold: `10 requests / 10 seconds / IP`
|
||||
4. Action: `Managed Challenge`
|
||||
|
||||
### 8. HTTPS and WSS
|
||||
|
||||
1. Enable `Always Use HTTPS`.
|
||||
2. Enable HSTS with:
|
||||
- `max-age=31536000`
|
||||
- `includeSubDomains`
|
||||
- `preload`
|
||||
3. Minimum TLS version: `1.2`.
|
||||
4. Keep SSL mode at `Full (strict)` when origin certs are valid.
|
||||
5. Enforce HTTPS redirect at both layers:
|
||||
- Cloudflare edge
|
||||
- nginx origin
|
||||
6. Validate websocket endpoint uses `wss://linearvoid.tv/ws`.
|
||||
|
||||
### 9. UptimeRobot Monitors
|
||||
|
||||
Create external monitors:
|
||||
|
||||
1. HTTP monitor:
|
||||
- URL: `https://linearvoid.tv/health`
|
||||
- Interval: 5 minutes
|
||||
2. Keyword monitor:
|
||||
- URL: `https://linearvoid.tv/hls/playlist.m3u8`
|
||||
- Keyword: `#EXTM3U`
|
||||
- Interval: 5 minutes
|
||||
3. Alert policy:
|
||||
- Trigger on 2 consecutive failures
|
||||
- Notify via email and Discord/Slack webhook
|
||||
|
||||
### 10. Verification Checklist
|
||||
|
||||
1. DNS resolves for `linearvoid.tv`, `www.linearvoid.tv`, and `media.linearvoid.tv`.
|
||||
2. Pages serves frontend for apex and `www` redirects correctly.
|
||||
3. HLS segments are cacheable long-term, manifests refresh every ~2s.
|
||||
4. Admin paths are rate-limited and WAF events are visible.
|
||||
5. HTTPS and WSS both succeed with valid certificates.
|
||||
6. UptimeRobot alerts fire as expected when endpoints are intentionally made unavailable.
|
||||
|
||||
## Running the Application
|
||||
|
||||
### Local Development
|
||||
@@ -414,7 +571,7 @@ See `docs/architecture/README.md` for the full arc42 documentation and the ADRs
|
||||
- **R2 Upload Fails**: Check credentials in `.env` and ensure the bucket exists. Verify with `aws s3 ls --endpoint-url=$R2_ENDPOINT`.
|
||||
- **Normalization Errors**: Run `backend/playout/start-playout.sh` manually to see ffprobe output. All videos must be H.264/AAC, 30 fps constant, 2s keyframes (ADR-001).
|
||||
- **Chat Not Connecting**: Check Redis connection and WebSocket URL. The health check at `/health` should return 200.
|
||||
- **Cache Not Working**: Verify Cloudflare cache rules (`.ts` = long TTL immutable, `.m3u8` = short TTL). Check cache hit ratio in Cloudflare dashboard.
|
||||
- **Cache Not Working**: Verify Cloudflare cache rules (`.m4s` = long TTL immutable, `.m3u8` = short TTL). Check cache hit ratio in Cloudflare dashboard.
|
||||
- **Docker Issues**: Run `docker compose logs` for service-specific errors. Ensure ports are not in use.
|
||||
- **Admin Interface**: Ensure `ADMIN_USERNAME`, `ADMIN_PASSWORD`, and `ADMIN_SESSION_TTL_HOURS` are set. `/admin/` and `/api/admin/*` are protected by a session-based login flow.
|
||||
- **Production Pull Fails**: Confirm `docker login` to Gitea succeeded on the server and the image tags referenced by `docker-compose.prod.yml` exist.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.23-alpine AS builder
|
||||
FROM golang:1.25-alpine AS builder
|
||||
|
||||
RUN apk add --no-cache gcc musl-dev
|
||||
|
||||
@@ -9,7 +9,7 @@ RUN go mod tidy
|
||||
RUN CGO_ENABLED=1 GOOS=linux go build -o /chat-server ./src/main.go
|
||||
|
||||
FROM alpine:3.20
|
||||
RUN apk --no-cache add ca-certificates curl curl
|
||||
RUN apk --no-cache add ca-certificates curl ffmpeg
|
||||
WORKDIR /app
|
||||
COPY --from=builder /chat-server /usr/local/bin/chat-server
|
||||
|
||||
|
||||
+12
-3
@@ -1,26 +1,35 @@
|
||||
module chat
|
||||
|
||||
go 1.23.0
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/glebarez/sqlite v1.11.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/redis/go-redis/v9 v9.7.0
|
||||
gorm.io/driver/postgres v1.5.11
|
||||
gorm.io/driver/sqlite v1.5.7
|
||||
gorm.io/gorm v1.25.12
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.5 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/crypto v0.37.0 // indirect
|
||||
golang.org/x/sync v0.13.0 // indirect
|
||||
golang.org/x/sys v0.32.0 // indirect
|
||||
golang.org/x/text v0.24.0 // indirect
|
||||
modernc.org/libc v1.22.5 // indirect
|
||||
modernc.org/mathutil v1.5.0 // indirect
|
||||
modernc.org/memory v1.5.0 // indirect
|
||||
modernc.org/sqlite v1.23.1 // indirect
|
||||
)
|
||||
+26
-4
@@ -9,6 +9,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
|
||||
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
||||
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
|
||||
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
@@ -23,12 +33,15 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
||||
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
@@ -38,6 +51,9 @@ golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
||||
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
||||
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
|
||||
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
||||
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
@@ -46,7 +62,13 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
|
||||
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
|
||||
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
|
||||
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
|
||||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
||||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
|
||||
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
|
||||
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
|
||||
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
|
||||
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
|
||||
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
|
||||
modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM=
|
||||
modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
|
||||
@@ -0,0 +1,140 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SessionStore manages admin authentication sessions.
|
||||
type SessionStore struct {
|
||||
mu sync.Mutex
|
||||
sessions map[string]time.Time
|
||||
}
|
||||
|
||||
// NewSessionStore creates a new session store.
|
||||
func NewSessionStore() *SessionStore {
|
||||
return &SessionStore{
|
||||
sessions: make(map[string]time.Time),
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateToken creates a cryptographically random 64-char hex session token.
|
||||
func GenerateToken() (string, error) {
|
||||
buf := make([]byte, 32)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// Validate checks if a session token is valid and not expired.
|
||||
// Returns true if valid, false otherwise. Expired sessions are cleaned up.
|
||||
func (s *SessionStore) Validate(token string) bool {
|
||||
if token == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
expiresAt, ok := s.sessions[token]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if time.Now().After(expiresAt) {
|
||||
delete(s.sessions, token)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Add stores a session token with expiry.
|
||||
func (s *SessionStore) Add(token string, expiresAt time.Time) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.sessions[token] = expiresAt
|
||||
}
|
||||
|
||||
// Remove deletes a session token.
|
||||
func (s *SessionStore) Remove(token string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
delete(s.sessions, token)
|
||||
}
|
||||
|
||||
// CountActive returns the number of unexpired sessions.
|
||||
func (s *SessionStore) CountActive() int {
|
||||
now := time.Now()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
for token, expiresAt := range s.sessions {
|
||||
if now.After(expiresAt) {
|
||||
delete(s.sessions, token)
|
||||
}
|
||||
}
|
||||
return len(s.sessions)
|
||||
}
|
||||
|
||||
// CookieSecure determines if the session cookie should have Secure flag.
|
||||
func CookieSecure(r *http.Request) bool {
|
||||
if forwardedProto := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); forwardedProto != "" {
|
||||
return strings.EqualFold(forwardedProto, "https")
|
||||
}
|
||||
return r.TLS != nil
|
||||
}
|
||||
|
||||
// SetCookie writes the admin session cookie.
|
||||
func SetCookie(w http.ResponseWriter, r *http.Request, cookieName, token string, expiresAt time.Time) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: cookieName,
|
||||
Value: token,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Secure: CookieSecure(r),
|
||||
Expires: expiresAt,
|
||||
MaxAge: int(time.Until(expiresAt).Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
// ClearCookie removes the admin session cookie.
|
||||
func ClearCookie(w http.ResponseWriter, r *http.Request, cookieName string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: cookieName,
|
||||
Value: "",
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Secure: CookieSecure(r),
|
||||
Expires: time.Unix(0, 0),
|
||||
MaxAge: -1,
|
||||
})
|
||||
}
|
||||
|
||||
// GetTokenFromCookie extracts the session token from the request cookie.
|
||||
func GetTokenFromCookie(r *http.Request, cookieName string) (string, bool) {
|
||||
cookie, err := r.Cookie(cookieName)
|
||||
if err != nil || cookie.Value == "" {
|
||||
return "", false
|
||||
}
|
||||
return cookie.Value, true
|
||||
}
|
||||
|
||||
// RequireAdmin returns middleware that checks for a valid admin session.
|
||||
func RequireAdmin(store *SessionStore, cookieName string) func(http.HandlerFunc) http.HandlerFunc {
|
||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
token, ok := GetTokenFromCookie(r, cookieName)
|
||||
if !ok || !store.Validate(token) {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGenerateToken(t *testing.T) {
|
||||
token, err := GenerateToken()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateToken() error = %v", err)
|
||||
}
|
||||
if len(token) != 64 {
|
||||
t.Errorf("GenerateToken() length = %d, want 64", len(token))
|
||||
}
|
||||
|
||||
// Uniqueness check
|
||||
tokens := make(map[string]bool)
|
||||
for i := 0; i < 1000; i++ {
|
||||
tok, err := GenerateToken()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateToken() iteration %d error = %v", i, err)
|
||||
}
|
||||
if tokens[tok] {
|
||||
t.Errorf("GenerateToken() produced duplicate token at iteration %d", i)
|
||||
}
|
||||
tokens[tok] = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionStore_Validate(t *testing.T) {
|
||||
store := NewSessionStore()
|
||||
|
||||
// Empty token
|
||||
if store.Validate("") {
|
||||
t.Error("Validate(\"\") = true, want false")
|
||||
}
|
||||
|
||||
// Missing token
|
||||
if store.Validate("nonexistent") {
|
||||
t.Error("Validate(\"nonexistent\") = true, want false")
|
||||
}
|
||||
|
||||
// Valid token
|
||||
token, _ := GenerateToken()
|
||||
store.Add(token, time.Now().Add(1*time.Hour))
|
||||
if !store.Validate(token) {
|
||||
t.Error("Validate(valid) = false, want true")
|
||||
}
|
||||
|
||||
// Expired token
|
||||
expiredToken, _ := GenerateToken()
|
||||
store.Add(expiredToken, time.Now().Add(-1*time.Hour))
|
||||
if store.Validate(expiredToken) {
|
||||
t.Error("Validate(expired) = true, want false")
|
||||
}
|
||||
|
||||
// Expired token should be cleaned up
|
||||
if store.Validate(expiredToken) {
|
||||
t.Error("Validate(expired) second call = true, want false (should be cleaned)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionStore_AddRemove(t *testing.T) {
|
||||
store := NewSessionStore()
|
||||
token, _ := GenerateToken()
|
||||
|
||||
store.Add(token, time.Now().Add(1*time.Hour))
|
||||
if !store.Validate(token) {
|
||||
t.Error("Validate after Add = false, want true")
|
||||
}
|
||||
|
||||
store.Remove(token)
|
||||
if store.Validate(token) {
|
||||
t.Error("Validate after Remove = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionStore_CountActive(t *testing.T) {
|
||||
store := NewSessionStore()
|
||||
|
||||
if count := store.CountActive(); count != 0 {
|
||||
t.Errorf("CountActive() = %d, want 0", count)
|
||||
}
|
||||
|
||||
t1, _ := GenerateToken()
|
||||
t2, _ := GenerateToken()
|
||||
t3, _ := GenerateToken()
|
||||
|
||||
store.Add(t1, time.Now().Add(1*time.Hour))
|
||||
store.Add(t2, time.Now().Add(1*time.Hour))
|
||||
store.Add(t3, time.Now().Add(-1*time.Hour)) // expired
|
||||
|
||||
if count := store.CountActive(); count != 2 {
|
||||
t.Errorf("CountActive() = %d, want 2 (expired should be cleaned)", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCookieSecure(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
forwardedProto string
|
||||
tls bool
|
||||
expected bool
|
||||
}{
|
||||
{"https forwarded", "https", false, true},
|
||||
{"http forwarded", "http", false, false},
|
||||
{"empty forwarded, no TLS", "", false, false},
|
||||
{"empty forwarded, TLS", "", true, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
if tt.forwardedProto != "" {
|
||||
req.Header.Set("X-Forwarded-Proto", tt.forwardedProto)
|
||||
}
|
||||
if tt.tls {
|
||||
req.TLS = &tls.ConnectionState{}
|
||||
}
|
||||
if got := CookieSecure(req); got != tt.expected {
|
||||
t.Errorf("CookieSecure() = %v, want %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetCookie(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req.Header.Set("X-Forwarded-Proto", "https")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
expiresAt := time.Now().Add(1 * time.Hour)
|
||||
SetCookie(w, req, "test_cookie", "abc123", expiresAt)
|
||||
|
||||
cookies := w.Result().Cookies()
|
||||
if len(cookies) != 1 {
|
||||
t.Fatalf("expected 1 cookie, got %d", len(cookies))
|
||||
}
|
||||
c := cookies[0]
|
||||
if c.Name != "test_cookie" {
|
||||
t.Errorf("cookie name = %q, want test_cookie", c.Name)
|
||||
}
|
||||
if c.Value != "abc123" {
|
||||
t.Errorf("cookie value = %q, want abc123", c.Value)
|
||||
}
|
||||
if !c.HttpOnly {
|
||||
t.Error("cookie HttpOnly = false, want true")
|
||||
}
|
||||
if c.SameSite != http.SameSiteStrictMode {
|
||||
t.Errorf("cookie SameSite = %v, want Strict", c.SameSite)
|
||||
}
|
||||
if !c.Secure {
|
||||
t.Error("cookie Secure = false, want true (X-Forwarded-Proto: https)")
|
||||
}
|
||||
if c.Path != "/" {
|
||||
t.Errorf("cookie Path = %q, want /", c.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearCookie(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ClearCookie(w, req, "test_cookie")
|
||||
|
||||
cookies := w.Result().Cookies()
|
||||
if len(cookies) != 1 {
|
||||
t.Fatalf("expected 1 cookie, got %d", len(cookies))
|
||||
}
|
||||
c := cookies[0]
|
||||
if c.Name != "test_cookie" {
|
||||
t.Errorf("cookie name = %q, want test_cookie", c.Name)
|
||||
}
|
||||
if c.Value != "" {
|
||||
t.Errorf("cookie value = %q, want empty", c.Value)
|
||||
}
|
||||
if c.MaxAge != -1 {
|
||||
t.Errorf("cookie MaxAge = %d, want -1", c.MaxAge)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTokenFromCookie(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "session", Value: "token123"})
|
||||
|
||||
token, ok := GetTokenFromCookie(req, "session")
|
||||
if !ok {
|
||||
t.Error("GetTokenFromCookie() ok = false, want true")
|
||||
}
|
||||
if token != "token123" {
|
||||
t.Errorf("GetTokenFromCookie() = %q, want token123", token)
|
||||
}
|
||||
|
||||
// Missing cookie
|
||||
_, ok = GetTokenFromCookie(req, "nonexistent")
|
||||
if ok {
|
||||
t.Error("GetTokenFromCookie() for missing cookie ok = true, want false")
|
||||
}
|
||||
|
||||
// Empty value
|
||||
req2 := httptest.NewRequest("GET", "/", nil)
|
||||
req2.AddCookie(&http.Cookie{Name: "session", Value: ""})
|
||||
_, ok = GetTokenFromCookie(req2, "session")
|
||||
if ok {
|
||||
t.Error("GetTokenFromCookie() for empty value ok = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireAdmin(t *testing.T) {
|
||||
store := NewSessionStore()
|
||||
token, _ := GenerateToken()
|
||||
store.Add(token, time.Now().Add(1*time.Hour))
|
||||
|
||||
handler := RequireAdmin(store, "session")(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
|
||||
// Valid session
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "session", Value: token})
|
||||
w := httptest.NewRecorder()
|
||||
handler(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
// No cookie
|
||||
req2 := httptest.NewRequest("GET", "/", nil)
|
||||
w2 := httptest.NewRecorder()
|
||||
handler(w2, req2)
|
||||
if w2.Code != http.StatusUnauthorized {
|
||||
t.Errorf("status = %d, want 401", w2.Code)
|
||||
}
|
||||
|
||||
// Invalid token
|
||||
req3 := httptest.NewRequest("GET", "/", nil)
|
||||
req3.AddCookie(&http.Cookie{Name: "session", Value: "invalid"})
|
||||
w3 := httptest.NewRecorder()
|
||||
handler(w3, req3)
|
||||
if w3.Code != http.StatusUnauthorized {
|
||||
t.Errorf("status = %d, want 401", w3.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Defaults and constants
|
||||
const (
|
||||
AdminSessionCookieName = "linearvoid_admin_session"
|
||||
MaxAdminUploadBytes int64 = 2 * 1024 * 1024 * 1024
|
||||
DefaultAdminChunkSize int64 = 8 * 1024 * 1024
|
||||
MaxAdminChunkSize int64 = 32 * 1024 * 1024
|
||||
DefaultRateLimitMax = 10
|
||||
DefaultRateLimitWindow = 10 * time.Second
|
||||
DefaultAdminSessionTTL = 12 * time.Hour
|
||||
DefaultChatPort = "3000"
|
||||
)
|
||||
|
||||
// PlaylistPath returns the playlist file path from env or default.
|
||||
func PlaylistPath() string {
|
||||
p := strings.TrimSpace(os.Getenv("PLAYLIST_PATH"))
|
||||
if p == "" {
|
||||
return "/app/playlist/playlist.txt"
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// MediaDir returns the media directory from env or default.
|
||||
func MediaDir() string {
|
||||
d := strings.TrimSpace(os.Getenv("MEDIA_DIR"))
|
||||
if d == "" {
|
||||
return "/app/media"
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// AdminUsername returns the admin username from env or default.
|
||||
func AdminUsername() string {
|
||||
u := strings.TrimSpace(os.Getenv("ADMIN_USERNAME"))
|
||||
if u == "" {
|
||||
return "admin"
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// AdminPassword returns the admin password from env or default.
|
||||
func AdminPassword() string {
|
||||
p := strings.TrimSpace(os.Getenv("ADMIN_PASSWORD"))
|
||||
if p == "" {
|
||||
return "admin"
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// AdminSessionTTL returns the admin session TTL from env or default.
|
||||
func AdminSessionTTL() time.Duration {
|
||||
raw := strings.TrimSpace(os.Getenv("ADMIN_SESSION_TTL_HOURS"))
|
||||
if raw == "" {
|
||||
return DefaultAdminSessionTTL
|
||||
}
|
||||
hours, err := strconv.Atoi(raw)
|
||||
if err != nil || hours <= 0 {
|
||||
return DefaultAdminSessionTTL
|
||||
}
|
||||
return time.Duration(hours) * time.Hour
|
||||
}
|
||||
|
||||
// RateLimitMax returns the chat rate limit max messages per window.
|
||||
func RateLimitMax() int {
|
||||
raw := strings.TrimSpace(os.Getenv("CHAT_RATE_LIMIT_MAX"))
|
||||
if raw == "" {
|
||||
return DefaultRateLimitMax
|
||||
}
|
||||
v, err := strconv.Atoi(raw)
|
||||
if err != nil || v <= 0 {
|
||||
return DefaultRateLimitMax
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// RateLimitWindow returns the chat rate limit window duration.
|
||||
func RateLimitWindow() time.Duration {
|
||||
raw := strings.TrimSpace(os.Getenv("CHAT_RATE_LIMIT_WINDOW_S"))
|
||||
if raw == "" {
|
||||
return DefaultRateLimitWindow
|
||||
}
|
||||
v, err := strconv.Atoi(raw)
|
||||
if err != nil || v <= 0 {
|
||||
return DefaultRateLimitWindow
|
||||
}
|
||||
return time.Duration(v) * time.Second
|
||||
}
|
||||
|
||||
// ChatPort returns the chat service port from env or default.
|
||||
func ChatPort() string {
|
||||
p := strings.TrimSpace(os.Getenv("CHAT_PORT"))
|
||||
if p == "" {
|
||||
return DefaultChatPort
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// RedisAddress returns the Redis address from env or default.
|
||||
func RedisAddress() string {
|
||||
a := strings.TrimSpace(os.Getenv("REDIS_URL"))
|
||||
if a == "" {
|
||||
return "redis:6379"
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// DatabaseURL returns the database URL from env (empty = SQLite default).
|
||||
func DatabaseURL() string {
|
||||
return strings.TrimSpace(os.Getenv("DATABASE_URL"))
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPlaylistPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{"default", "", "/app/playlist/playlist.txt"},
|
||||
{"custom", "/custom/playlist.txt", "/custom/playlist.txt"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("PLAYLIST_PATH", tt.env)
|
||||
defer os.Unsetenv("PLAYLIST_PATH")
|
||||
} else {
|
||||
os.Unsetenv("PLAYLIST_PATH")
|
||||
}
|
||||
if got := PlaylistPath(); got != tt.expected {
|
||||
t.Errorf("PlaylistPath() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMediaDir(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{"default", "", "/app/media"},
|
||||
{"custom", "/data/media", "/data/media"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("MEDIA_DIR", tt.env)
|
||||
defer os.Unsetenv("MEDIA_DIR")
|
||||
} else {
|
||||
os.Unsetenv("MEDIA_DIR")
|
||||
}
|
||||
if got := MediaDir(); got != tt.expected {
|
||||
t.Errorf("MediaDir() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminUsername(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{"default", "", "admin"},
|
||||
{"custom", "superadmin", "superadmin"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("ADMIN_USERNAME", tt.env)
|
||||
defer os.Unsetenv("ADMIN_USERNAME")
|
||||
} else {
|
||||
os.Unsetenv("ADMIN_USERNAME")
|
||||
}
|
||||
if got := AdminUsername(); got != tt.expected {
|
||||
t.Errorf("AdminUsername() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminPassword(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{"default", "", "admin"},
|
||||
{"custom", "secret123", "secret123"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("ADMIN_PASSWORD", tt.env)
|
||||
defer os.Unsetenv("ADMIN_PASSWORD")
|
||||
} else {
|
||||
os.Unsetenv("ADMIN_PASSWORD")
|
||||
}
|
||||
if got := AdminPassword(); got != tt.expected {
|
||||
t.Errorf("AdminPassword() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminSessionTTL(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected time.Duration
|
||||
}{
|
||||
{"default", "", 12 * time.Hour},
|
||||
{"custom", "24", 24 * time.Hour},
|
||||
{"zero", "0", 12 * time.Hour},
|
||||
{"negative", "-5", 12 * time.Hour},
|
||||
{"invalid", "abc", 12 * time.Hour},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("ADMIN_SESSION_TTL_HOURS", tt.env)
|
||||
defer os.Unsetenv("ADMIN_SESSION_TTL_HOURS")
|
||||
} else {
|
||||
os.Unsetenv("ADMIN_SESSION_TTL_HOURS")
|
||||
}
|
||||
if got := AdminSessionTTL(); got != tt.expected {
|
||||
t.Errorf("AdminSessionTTL() = %v, want %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimitMax(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected int
|
||||
}{
|
||||
{"default", "", 10},
|
||||
{"custom", "20", 20},
|
||||
{"zero", "0", 10},
|
||||
{"negative", "-5", 10},
|
||||
{"invalid", "abc", 10},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("CHAT_RATE_LIMIT_MAX", tt.env)
|
||||
defer os.Unsetenv("CHAT_RATE_LIMIT_MAX")
|
||||
} else {
|
||||
os.Unsetenv("CHAT_RATE_LIMIT_MAX")
|
||||
}
|
||||
if got := RateLimitMax(); got != tt.expected {
|
||||
t.Errorf("RateLimitMax() = %d, want %d", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimitWindow(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected time.Duration
|
||||
}{
|
||||
{"default", "", 10 * time.Second},
|
||||
{"custom", "30", 30 * time.Second},
|
||||
{"zero", "0", 10 * time.Second},
|
||||
{"negative", "-5", 10 * time.Second},
|
||||
{"invalid", "abc", 10 * time.Second},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("CHAT_RATE_LIMIT_WINDOW_S", tt.env)
|
||||
defer os.Unsetenv("CHAT_RATE_LIMIT_WINDOW_S")
|
||||
} else {
|
||||
os.Unsetenv("CHAT_RATE_LIMIT_WINDOW_S")
|
||||
}
|
||||
if got := RateLimitWindow(); got != tt.expected {
|
||||
t.Errorf("RateLimitWindow() = %v, want %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatPort(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{"default", "", "3000"},
|
||||
{"custom", "8080", "8080"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("CHAT_PORT", tt.env)
|
||||
defer os.Unsetenv("CHAT_PORT")
|
||||
} else {
|
||||
os.Unsetenv("CHAT_PORT")
|
||||
}
|
||||
if got := ChatPort(); got != tt.expected {
|
||||
t.Errorf("ChatPort() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedisAddress(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{"default", "", "redis:6379"},
|
||||
{"custom", "redis.example.com:6380", "redis.example.com:6380"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("REDIS_URL", tt.env)
|
||||
defer os.Unsetenv("REDIS_URL")
|
||||
} else {
|
||||
os.Unsetenv("REDIS_URL")
|
||||
}
|
||||
if got := RedisAddress(); got != tt.expected {
|
||||
t.Errorf("RedisAddress() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabaseURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{"default", "", ""},
|
||||
{"sqlite", "sqlite://test.db", "sqlite://test.db"},
|
||||
{"postgres", "postgres://user:pass@localhost/db", "postgres://user:pass@localhost/db"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.env != "" {
|
||||
os.Setenv("DATABASE_URL", tt.env)
|
||||
defer os.Unsetenv("DATABASE_URL")
|
||||
} else {
|
||||
os.Unsetenv("DATABASE_URL")
|
||||
}
|
||||
if got := DatabaseURL(); got != tt.expected {
|
||||
t.Errorf("DatabaseURL() = %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DBClient wraps gorm.DB for testability.
|
||||
type DBClient struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
// InitDB initializes configured SQL storage for chat, moderation, and admin metadata (ADR-006).
|
||||
// Returns the client and the driver name.
|
||||
func InitDB(databaseURL string) (*DBClient, string, error) {
|
||||
var (
|
||||
gormDB *gorm.DB
|
||||
err error
|
||||
driver = "sqlite"
|
||||
)
|
||||
|
||||
switch {
|
||||
case databaseURL == "":
|
||||
gormDB, err = gorm.Open(sqlite.Open("chat.db"), &gorm.Config{})
|
||||
case strings.HasPrefix(databaseURL, "sqlite://"):
|
||||
sqlitePath := strings.TrimPrefix(databaseURL, "sqlite://")
|
||||
if sqlitePath == "" {
|
||||
sqlitePath = "chat.db"
|
||||
}
|
||||
gormDB, err = gorm.Open(sqlite.Open(sqlitePath), &gorm.Config{})
|
||||
case strings.HasPrefix(databaseURL, "postgres://") || strings.HasPrefix(databaseURL, "postgresql://"):
|
||||
driver = "postgres"
|
||||
gormDB, err = gorm.Open(postgres.Open(databaseURL), &gorm.Config{})
|
||||
default:
|
||||
log.Fatalf("Unsupported DATABASE_URL scheme: %s", databaseURL)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, driver, err
|
||||
}
|
||||
|
||||
gormDB.AutoMigrate(&Message{}, &ModerationLog{}, &Media{}, &ScheduleOverride{})
|
||||
log.Printf("%s database initialized (chat history, moderation log, media metadata)", strings.Title(driver))
|
||||
|
||||
return &DBClient{DB: gormDB}, driver, nil
|
||||
}
|
||||
|
||||
// RedisClient wraps the Redis client for testability.
|
||||
// Interface defined for mocking in tests.
|
||||
type RedisClient interface {
|
||||
Ping(ctx context.Context) error
|
||||
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
|
||||
Get(ctx context.Context, key string) (string, error)
|
||||
Del(ctx context.Context, keys ...string) error
|
||||
Incr(ctx context.Context, key string) (int64, error)
|
||||
Decr(ctx context.Context, key string) (int64, error)
|
||||
Expire(ctx context.Context, key string, expiration time.Duration) error
|
||||
Exists(ctx context.Context, keys ...string) (int64, error)
|
||||
Publish(ctx context.Context, channel string, message interface{}) error
|
||||
PSubscribe(ctx context.Context, channels ...string) PubSub
|
||||
Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error)
|
||||
Info(ctx context.Context, sections ...string) (string, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// PubSub wraps Redis pub/sub subscription.
|
||||
type PubSub interface {
|
||||
ReceiveMessage(ctx context.Context) (*PubSubMessage, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// PubSubMessage represents a received pub/sub message.
|
||||
type PubSubMessage struct {
|
||||
Channel string
|
||||
Payload string
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInitDB_SQLite(t *testing.T) {
|
||||
client, driver, err := InitDB("sqlite://:memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("InitDB() error = %v", err)
|
||||
}
|
||||
if driver != "sqlite" {
|
||||
t.Errorf("InitDB() driver = %q, want sqlite", driver)
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("InitDB() client = nil")
|
||||
}
|
||||
|
||||
// Verify tables exist by running AutoMigrate (idempotent)
|
||||
err = client.DB.AutoMigrate(&Message{}, &ModerationLog{}, &Media{}, &ScheduleOverride{})
|
||||
if err != nil {
|
||||
t.Errorf("AutoMigrate error = %v", err)
|
||||
}
|
||||
|
||||
// Verify CRUD on Message
|
||||
msg := Message{
|
||||
Room: "test",
|
||||
Username: "user1",
|
||||
Text: "hello",
|
||||
Timestamp: time.Now(),
|
||||
Type: "public",
|
||||
}
|
||||
if err := client.DB.Create(&msg).Error; err != nil {
|
||||
t.Fatalf("Create message error = %v", err)
|
||||
}
|
||||
if msg.ID == 0 {
|
||||
t.Error("Create message did not set ID")
|
||||
}
|
||||
|
||||
var count int64
|
||||
client.DB.Model(&Message{}).Count(&count)
|
||||
if count != 1 {
|
||||
t.Errorf("Message count = %d, want 1", count)
|
||||
}
|
||||
|
||||
// Verify read
|
||||
var read Message
|
||||
if err := client.DB.First(&read, msg.ID).Error; err != nil {
|
||||
t.Fatalf("First message error = %v", err)
|
||||
}
|
||||
if read.Text != "hello" {
|
||||
t.Errorf("read.Text = %q, want hello", read.Text)
|
||||
}
|
||||
|
||||
// Verify delete
|
||||
client.DB.Delete(&Message{}, msg.ID)
|
||||
client.DB.Model(&Message{}).Count(&count)
|
||||
if count != 0 {
|
||||
t.Errorf("Message count after delete = %d, want 0", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitDB_ModerationLog(t *testing.T) {
|
||||
client, _, err := InitDB("sqlite://:memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("InitDB() error = %v", err)
|
||||
}
|
||||
|
||||
log := ModerationLog{
|
||||
Action: "mute",
|
||||
Target: "user1",
|
||||
Moderator: "admin",
|
||||
Reason: "spam",
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
if err := client.DB.Create(&log).Error; err != nil {
|
||||
t.Fatalf("Create moderation log error = %v", err)
|
||||
}
|
||||
if log.ID == 0 {
|
||||
t.Error("Create moderation log did not set ID")
|
||||
}
|
||||
|
||||
var count int64
|
||||
client.DB.Model(&ModerationLog{}).Count(&count)
|
||||
if count != 1 {
|
||||
t.Errorf("ModerationLog count = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitDB_Media(t *testing.T) {
|
||||
client, _, err := InitDB("sqlite://:memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("InitDB() error = %v", err)
|
||||
}
|
||||
|
||||
media := Media{
|
||||
Filename: "test.mp4",
|
||||
OriginalName: "test.mp4",
|
||||
Path: "/media/test.mp4",
|
||||
SizeBytes: 1024,
|
||||
DurationSeconds: 60,
|
||||
Status: "ready",
|
||||
Title: "Test Video",
|
||||
Tags: "tag1,tag2",
|
||||
}
|
||||
if err := client.DB.Create(&media).Error; err != nil {
|
||||
t.Fatalf("Create media error = %v", err)
|
||||
}
|
||||
if media.ID == 0 {
|
||||
t.Error("Create media did not set ID")
|
||||
}
|
||||
|
||||
// Verify update
|
||||
client.DB.Model(&media).Update("status", "needs_normalization")
|
||||
var updated Media
|
||||
client.DB.First(&updated, media.ID)
|
||||
if updated.Status != "needs_normalization" {
|
||||
t.Errorf("updated.Status = %q, want needs_normalization", updated.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitDB_ScheduleOverride(t *testing.T) {
|
||||
client, _, err := InitDB("sqlite://:memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("InitDB() error = %v", err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
ov := ScheduleOverride{
|
||||
MediaID: 1,
|
||||
StartTime: now,
|
||||
EndTime: now.Add(1 * time.Hour),
|
||||
Reason: "special event",
|
||||
}
|
||||
if err := client.DB.Create(&ov).Error; err != nil {
|
||||
t.Fatalf("Create schedule override error = %v", err)
|
||||
}
|
||||
if ov.ID == 0 {
|
||||
t.Error("Create schedule override did not set ID")
|
||||
}
|
||||
|
||||
// Conflict detection
|
||||
var conflict int64
|
||||
client.DB.Model(&ScheduleOverride{}).
|
||||
Where("start_time < ? AND end_time > ?", ov.EndTime, ov.StartTime).
|
||||
Count(&conflict)
|
||||
if conflict != 1 {
|
||||
t.Errorf("conflict count = %d, want 1", conflict)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitDB_EmptyURL(t *testing.T) {
|
||||
// Empty URL should default to SQLite chat.db
|
||||
// This creates a file, clean up after
|
||||
client, driver, err := InitDB("")
|
||||
if err != nil {
|
||||
t.Fatalf("InitDB(\"\") error = %v", err)
|
||||
}
|
||||
if driver != "sqlite" {
|
||||
t.Errorf("InitDB(\"\") driver = %q, want sqlite", driver)
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("InitDB(\"\") client = nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitDB_SQLitePath(t *testing.T) {
|
||||
client, driver, err := InitDB("sqlite://:memory:?cache=shared")
|
||||
if err != nil {
|
||||
t.Fatalf("InitDB() error = %v", err)
|
||||
}
|
||||
if driver != "sqlite" {
|
||||
t.Errorf("InitDB() driver = %q, want sqlite", driver)
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("InitDB() client = nil")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Message represents a chat message (persisted in SQL DB per ADR-006)
|
||||
type Message struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
Room string `json:"room"`
|
||||
Username string `json:"username"`
|
||||
Text string `json:"text"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Type string `json:"type"` // "public", "private", "system", "moderation"
|
||||
}
|
||||
|
||||
// ModerationLog for audit trail (immutable per ADR-005)
|
||||
type ModerationLog struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
Action string `json:"action"` // "mute", "ban", "delete", "timeout"
|
||||
Target string `json:"target"`
|
||||
Moderator string `json:"moderator"`
|
||||
Reason string `json:"reason"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
// Media stores admin media library metadata (filesystem + DB model per ADR-006)
|
||||
type Media struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
Filename string `gorm:"index" json:"filename"`
|
||||
OriginalName string `json:"originalName"`
|
||||
Path string `json:"path"`
|
||||
SizeBytes int64 `json:"sizeBytes"`
|
||||
DurationSeconds int `json:"durationSeconds"`
|
||||
Status string `gorm:"index" json:"status"`
|
||||
Title string `json:"title"`
|
||||
Tags string `json:"tags"`
|
||||
UploadedAt time.Time `gorm:"autoCreateTime" json:"uploadedAt"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
|
||||
}
|
||||
|
||||
// ScheduleOverride represents a scheduled playlist override (ADR-004)
|
||||
type ScheduleOverride struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
MediaID uint `json:"mediaId"`
|
||||
StartTime time.Time `json:"startTime"`
|
||||
EndTime time.Time `json:"endTime"`
|
||||
Reason string `json:"reason"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
// Presence tracks online users per room (using Redis per ADR-007)
|
||||
type Presence struct {
|
||||
Username string `json:"username"`
|
||||
Room string `json:"room"`
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// RedisWrapper wraps go-redis client to implement RedisClient interface.
|
||||
type RedisWrapper struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
// NewRedisWrapper creates a RedisWrapper from a go-redis client.
|
||||
func NewRedisWrapper(client *redis.Client) *RedisWrapper {
|
||||
return &RedisWrapper{client: client}
|
||||
}
|
||||
|
||||
// InitRedis initializes Redis for presence, rate limiting, and pub/sub (ADR-007).
|
||||
// Returns nil if Redis is unavailable (non-fatal).
|
||||
func InitRedis(redisAddress string) *RedisWrapper {
|
||||
var options *redis.Options
|
||||
var err error
|
||||
|
||||
if strings.Contains(redisAddress, "://") {
|
||||
options, err = redis.ParseURL(redisAddress)
|
||||
if err != nil {
|
||||
log.Printf("Redis configuration warning: %v (rate limiting and presence will be disabled)", err)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
options = &redis.Options{Addr: redisAddress}
|
||||
}
|
||||
|
||||
client := redis.NewClient(options)
|
||||
if err := client.Ping(context.Background()).Err(); err != nil {
|
||||
log.Printf("Redis connection warning: %v (rate limiting and presence will be disabled)", err)
|
||||
client.Close()
|
||||
// Start auto-reconnect goroutine
|
||||
go redisReconnectLoop(redisAddress, options)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Println("Redis connected successfully (presence, rate limiting, pub/sub enabled)")
|
||||
return &RedisWrapper{client: client}
|
||||
}
|
||||
|
||||
// redisReconnectLoop attempts to reconnect to Redis with exponential backoff.
|
||||
func redisReconnectLoop(addr string, baseOptions *redis.Options) {
|
||||
backoff := 2 * time.Second
|
||||
maxBackoff := 60 * time.Second
|
||||
for {
|
||||
time.Sleep(backoff)
|
||||
testClient := redis.NewClient(baseOptions)
|
||||
if err := testClient.Ping(context.Background()).Err(); err != nil {
|
||||
log.Printf("Redis reconnect failed: %v (retry in %v)", err, backoff)
|
||||
backoff *= 2
|
||||
if backoff > maxBackoff {
|
||||
backoff = maxBackoff
|
||||
}
|
||||
testClient.Close()
|
||||
continue
|
||||
}
|
||||
// Reconnected — caller must replace their reference.
|
||||
// This is a best-effort background reconnect; the wrapper
|
||||
// returned by InitRedis remains nil until a new InitRedis call.
|
||||
log.Println("Redis reconnected successfully (background)")
|
||||
testClient.Close()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Ping checks Redis connectivity.
|
||||
func (r *RedisWrapper) Ping(ctx context.Context) error {
|
||||
if r == nil || r.client == nil {
|
||||
return redis.ErrClosed
|
||||
}
|
||||
return r.client.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
// Set stores a key with TTL.
|
||||
func (r *RedisWrapper) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
return r.client.Set(ctx, key, value, expiration).Err()
|
||||
}
|
||||
|
||||
// Get retrieves a key.
|
||||
func (r *RedisWrapper) Get(ctx context.Context, key string) (string, error) {
|
||||
return r.client.Get(ctx, key).Result()
|
||||
}
|
||||
|
||||
// Del removes keys.
|
||||
func (r *RedisWrapper) Del(ctx context.Context, keys ...string) error {
|
||||
return r.client.Del(ctx, keys...).Err()
|
||||
}
|
||||
|
||||
// Incr increments a key.
|
||||
func (r *RedisWrapper) Incr(ctx context.Context, key string) (int64, error) {
|
||||
return r.client.Incr(ctx, key).Result()
|
||||
}
|
||||
|
||||
// Decr decrements a key.
|
||||
func (r *RedisWrapper) Decr(ctx context.Context, key string) (int64, error) {
|
||||
return r.client.Decr(ctx, key).Result()
|
||||
}
|
||||
|
||||
// Expire sets TTL on a key.
|
||||
func (r *RedisWrapper) Expire(ctx context.Context, key string, expiration time.Duration) error {
|
||||
return r.client.Expire(ctx, key, expiration).Err()
|
||||
}
|
||||
|
||||
// Exists checks if keys exist.
|
||||
func (r *RedisWrapper) Exists(ctx context.Context, keys ...string) (int64, error) {
|
||||
return r.client.Exists(ctx, keys...).Result()
|
||||
}
|
||||
|
||||
// Publish publishes a message to a channel.
|
||||
func (r *RedisWrapper) Publish(ctx context.Context, channel string, message interface{}) error {
|
||||
return r.client.Publish(ctx, channel, message).Err()
|
||||
}
|
||||
|
||||
// PSubscribe subscribes to channels by pattern.
|
||||
func (r *RedisWrapper) PSubscribe(ctx context.Context, channels ...string) PubSub {
|
||||
ps := r.client.PSubscribe(ctx, channels...)
|
||||
return &redisPubSubWrapper{ps: ps}
|
||||
}
|
||||
|
||||
// Scan scans keys matching a pattern.
|
||||
func (r *RedisWrapper) Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error) {
|
||||
return r.client.Scan(ctx, cursor, match, count).Result()
|
||||
}
|
||||
|
||||
// Info returns Redis server info.
|
||||
func (r *RedisWrapper) Info(ctx context.Context, sections ...string) (string, error) {
|
||||
return r.client.Info(ctx, sections...).Result()
|
||||
}
|
||||
|
||||
// Close closes the Redis connection.
|
||||
func (r *RedisWrapper) Close() error {
|
||||
if r == nil || r.client == nil {
|
||||
return nil
|
||||
}
|
||||
return r.client.Close()
|
||||
}
|
||||
|
||||
// redisPubSubWrapper wraps go-redis PubSub.
|
||||
type redisPubSubWrapper struct {
|
||||
ps *redis.PubSub
|
||||
}
|
||||
|
||||
func (w *redisPubSubWrapper) ReceiveMessage(ctx context.Context) (*PubSubMessage, error) {
|
||||
msg, err := w.ps.ReceiveMessage(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PubSubMessage{Channel: msg.Channel, Payload: msg.Payload}, nil
|
||||
}
|
||||
|
||||
func (w *redisPubSubWrapper) Close() error {
|
||||
return w.ps.Close()
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,947 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"chat/src/auth"
|
||||
"chat/src/config"
|
||||
"chat/src/db"
|
||||
"chat/src/ws"
|
||||
)
|
||||
|
||||
// mockRedis implements db.RedisClient for handler tests.
|
||||
type mockRedis struct {
|
||||
mu sync.Mutex
|
||||
store map[string]string
|
||||
}
|
||||
|
||||
func newMockRedis() *mockRedis {
|
||||
return &mockRedis{store: make(map[string]string)}
|
||||
}
|
||||
|
||||
func (m *mockRedis) Ping(ctx context.Context) error { return nil }
|
||||
func (m *mockRedis) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.store[key] = value.(string)
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) Get(ctx context.Context, key string) (string, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.store[key], nil
|
||||
}
|
||||
func (m *mockRedis) Del(ctx context.Context, keys ...string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, k := range keys {
|
||||
delete(m.store, k)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) Incr(ctx context.Context, key string) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.store[key] = "1"
|
||||
return 1, nil
|
||||
}
|
||||
func (m *mockRedis) Decr(ctx context.Context, key string) (int64, error) { return 0, nil }
|
||||
func (m *mockRedis) Expire(ctx context.Context, key string, expiration time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) Exists(ctx context.Context, keys ...string) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, k := range keys {
|
||||
if _, ok := m.store[k]; ok {
|
||||
return 1, nil
|
||||
}
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
func (m *mockRedis) Publish(ctx context.Context, channel string, message interface{}) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) PSubscribe(ctx context.Context, channels ...string) db.PubSub {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
var keys []string
|
||||
for k := range m.store {
|
||||
if strings.Contains(k, strings.TrimSuffix(match, "*")) {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
return keys, 0, nil
|
||||
}
|
||||
func (m *mockRedis) Info(ctx context.Context, sections ...string) (string, error) {
|
||||
return "connected_clients:1\nused_memory_human:1M\n", nil
|
||||
}
|
||||
func (m *mockRedis) Close() error { return nil }
|
||||
|
||||
func setupTestDeps(t *testing.T) (*Dependencies, *gorm.DB, *auth.SessionStore) {
|
||||
t.Helper()
|
||||
gormDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open test DB: %v", err)
|
||||
}
|
||||
gormDB.AutoMigrate(&db.Message{}, &db.ModerationLog{}, &db.Media{}, &db.ScheduleOverride{})
|
||||
mr := newMockRedis()
|
||||
sessionStore := auth.NewSessionStore()
|
||||
hub := ws.NewHub(gormDB, mr, "test-instance", 10, 10*time.Second, config.AdminUsername())
|
||||
|
||||
deps := &Dependencies{
|
||||
DB: gormDB,
|
||||
Redis: mr,
|
||||
Hub: hub,
|
||||
SessionStore: sessionStore,
|
||||
DBDriver: "sqlite",
|
||||
}
|
||||
return deps, gormDB, sessionStore
|
||||
}
|
||||
|
||||
// adminCookie creates a valid admin session and returns the cookie header.
|
||||
func adminCookie(t *testing.T, store *auth.SessionStore) string {
|
||||
t.Helper()
|
||||
token, _ := auth.GenerateToken()
|
||||
store.Add(token, time.Now().Add(1*time.Hour))
|
||||
return config.AdminSessionCookieName + "=" + token
|
||||
}
|
||||
|
||||
func TestHandleAdminLogin_Success(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
handler := HandleAdminLogin(deps)
|
||||
|
||||
// Ensure env vars are set for credential check
|
||||
os.Setenv("ADMIN_USERNAME", "admin")
|
||||
os.Setenv("ADMIN_PASSWORD", "admin")
|
||||
defer os.Unsetenv("ADMIN_USERNAME")
|
||||
defer os.Unsetenv("ADMIN_PASSWORD")
|
||||
|
||||
body := `{"username":"admin","password":"admin"}`
|
||||
req := httptest.NewRequest("POST", "/api/admin/login", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var resp AdminSessionResponse
|
||||
json.NewDecoder(w.Body).Decode(&resp)
|
||||
if !resp.Authenticated {
|
||||
t.Error("Authenticated = false, want true")
|
||||
}
|
||||
|
||||
// Check cookie set
|
||||
cookies := w.Result().Cookies()
|
||||
found := false
|
||||
for _, c := range cookies {
|
||||
if c.Name == config.AdminSessionCookieName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("session cookie not set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminLogin_InvalidCreds(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
handler := HandleAdminLogin(deps)
|
||||
|
||||
body := `{"username":"admin","password":"wrong"}`
|
||||
req := httptest.NewRequest("POST", "/api/admin/login", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("status = %d, want 401", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminLogin_InvalidJSON(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
handler := HandleAdminLogin(deps)
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/admin/login", strings.NewReader("not json"))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminLogin_WrongMethod(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
handler := HandleAdminLogin(deps)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/admin/login", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("status = %d, want 405", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminLogout(t *testing.T) {
|
||||
deps, _, store := setupTestDeps(t)
|
||||
handler := HandleAdminLogout(deps)
|
||||
|
||||
cookie := adminCookie(t, store)
|
||||
req := httptest.NewRequest("POST", "/api/admin/logout", nil)
|
||||
req.Header.Set("Cookie", cookie)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
// Cookie should be cleared
|
||||
cookies := w.Result().Cookies()
|
||||
found := false
|
||||
for _, c := range cookies {
|
||||
if c.Name == config.AdminSessionCookieName && c.MaxAge == -1 {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("session cookie not cleared")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminSession_Authenticated(t *testing.T) {
|
||||
deps, _, store := setupTestDeps(t)
|
||||
handler := HandleAdminSession(deps)
|
||||
|
||||
cookie := adminCookie(t, store)
|
||||
req := httptest.NewRequest("GET", "/api/admin/session", nil)
|
||||
req.Header.Set("Cookie", cookie)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var resp AdminSessionResponse
|
||||
json.NewDecoder(w.Body).Decode(&resp)
|
||||
if !resp.Authenticated {
|
||||
t.Error("Authenticated = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminSession_Unauthenticated(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
handler := HandleAdminSession(deps)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/admin/session", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("status = %d, want 401", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminOverview(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
// Add some test data
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u1", Text: "hi", Timestamp: time.Now(), Type: "public"})
|
||||
gormDB.Create(&db.ModerationLog{Action: "mute", Target: "u2", Moderator: "admin", Reason: "spam", Timestamp: time.Now()})
|
||||
|
||||
handler := HandleAdminOverview(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/overview", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var overview AdminOverview
|
||||
json.NewDecoder(w.Body).Decode(&overview)
|
||||
if overview.StoredMessages != 1 {
|
||||
t.Errorf("StoredMessages = %d, want 1", overview.StoredMessages)
|
||||
}
|
||||
if overview.ModerationActions != 1 {
|
||||
t.Errorf("ModerationActions = %d, want 1", overview.ModerationActions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminPlaylist_Get(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
// Create temp playlist
|
||||
tmpDir := t.TempDir()
|
||||
playlistPath := filepath.Join(tmpDir, "playlist.txt")
|
||||
os.WriteFile(playlistPath, []byte("file /videos/test.mp4\n"), 0644)
|
||||
os.Setenv("PLAYLIST_PATH", playlistPath)
|
||||
defer os.Unsetenv("PLAYLIST_PATH")
|
||||
|
||||
handler := HandleAdminPlaylist(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/playlist", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var payload PlaylistPayload
|
||||
json.NewDecoder(w.Body).Decode(&payload)
|
||||
if !strings.Contains(payload.Content, "test.mp4") {
|
||||
t.Errorf("Content = %q, want test.mp4", payload.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminPlaylist_Put(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
playlistPath := filepath.Join(tmpDir, "playlist.txt")
|
||||
os.Setenv("PLAYLIST_PATH", playlistPath)
|
||||
defer os.Unsetenv("PLAYLIST_PATH")
|
||||
|
||||
handler := HandleAdminPlaylist(deps)
|
||||
body := `{"content":"file /videos/new.mp4"}`
|
||||
req := httptest.NewRequest("PUT", "/api/admin/playlist", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
// Verify file written
|
||||
data, _ := os.ReadFile(playlistPath)
|
||||
if !strings.Contains(string(data), "new.mp4") {
|
||||
t.Errorf("file content = %q, want new.mp4", string(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminModerationLogs(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.ModerationLog{Action: "mute", Target: "u1", Moderator: "admin", Reason: "spam", Timestamp: time.Now()})
|
||||
|
||||
handler := HandleAdminModerationLogs(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/moderation/logs", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var resp ModerationLogsResponse
|
||||
json.NewDecoder(w.Body).Decode(&resp)
|
||||
if len(resp.Logs) != 1 {
|
||||
t.Errorf("len(Logs) = %d, want 1", len(resp.Logs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminModerationAction(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminModerationAction(deps)
|
||||
body := `{"action":"mute","target":"baduser","moderator":"admin","reason":"spam","room":"main"}`
|
||||
req := httptest.NewRequest("POST", "/api/admin/moderation/actions", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var count int64
|
||||
gormDB.Model(&db.ModerationLog{}).Count(&count)
|
||||
if count != 1 {
|
||||
t.Errorf("moderation log count = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminModerationAction_MissingFields(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminModerationAction(deps)
|
||||
body := `{"action":"mute"}`
|
||||
req := httptest.NewRequest("POST", "/api/admin/moderation/actions", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminAnalytics(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u1", Text: "hi", Timestamp: time.Now(), Type: "public"})
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u2", Text: "hey", Timestamp: time.Now(), Type: "public"})
|
||||
|
||||
handler := HandleAdminAnalytics(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/analytics", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var analytics AdminAnalytics
|
||||
json.NewDecoder(w.Body).Decode(&analytics)
|
||||
if analytics.TotalMessages != 2 {
|
||||
t.Errorf("TotalMessages = %d, want 2", analytics.TotalMessages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminSystemSummary(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminSystemSummary(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/system", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var summary AdminSystemSummary
|
||||
json.NewDecoder(w.Body).Decode(&summary)
|
||||
if summary.DatabaseDriver != "sqlite" {
|
||||
t.Errorf("DatabaseDriver = %q, want sqlite", summary.DatabaseDriver)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminConfig_Get(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminConfig(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/config", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminConfig_Put(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminConfig(deps)
|
||||
body := `{"rateLimitMax":20}`
|
||||
req := httptest.NewRequest("PUT", "/api/admin/config", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminRestart(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminRestart(deps)
|
||||
req := httptest.NewRequest("POST", "/api/admin/restart?service=chat", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminUsers_Get(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminUsers(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/users", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminUsers_Post(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminUsers(deps)
|
||||
body := `{"action":"mute","username":"baduser","duration":10}`
|
||||
req := httptest.NewRequest("POST", "/api/admin/users", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminSchedule(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
playlistPath := filepath.Join(tmpDir, "playlist.txt")
|
||||
os.WriteFile(playlistPath, []byte("file /videos/test.mp4\n"), 0644)
|
||||
os.Setenv("PLAYLIST_PATH", playlistPath)
|
||||
defer os.Unsetenv("PLAYLIST_PATH")
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "test.mp4",
|
||||
OriginalName: "test.mp4",
|
||||
Path: "/videos/test.mp4",
|
||||
SizeBytes: 1024,
|
||||
DurationSeconds: 60,
|
||||
Status: "ready",
|
||||
Title: "Test Video",
|
||||
})
|
||||
|
||||
handler := HandleAdminSchedule(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/schedule", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var resp AdminScheduleResponse
|
||||
json.NewDecoder(w.Body).Decode(&resp)
|
||||
if resp.MatchedEntries != 1 {
|
||||
t.Errorf("MatchedEntries = %d, want 1", resp.MatchedEntries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminScheduleOverrides_CRUD(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
// Create
|
||||
handler := HandleAdminScheduleOverrides(deps)
|
||||
now := time.Now()
|
||||
body, _ := json.Marshal(db.ScheduleOverride{
|
||||
MediaID: 1,
|
||||
StartTime: now,
|
||||
EndTime: now.Add(1 * time.Hour),
|
||||
Reason: "test",
|
||||
})
|
||||
req := httptest.NewRequest("POST", "/api/admin/schedule/overrides", bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Errorf("POST status = %d, want 201", w.Code)
|
||||
}
|
||||
|
||||
// List
|
||||
req2 := httptest.NewRequest("GET", "/api/admin/schedule/overrides", nil)
|
||||
w2 := httptest.NewRecorder()
|
||||
handler(w2, req2)
|
||||
|
||||
if w2.Code != http.StatusOK {
|
||||
t.Errorf("GET status = %d, want 200", w2.Code)
|
||||
}
|
||||
|
||||
// Delete
|
||||
var overrides []db.ScheduleOverride
|
||||
gormDB.Find(&overrides)
|
||||
if len(overrides) > 0 {
|
||||
req3 := httptest.NewRequest("DELETE", "/api/admin/schedule/overrides?id=1", nil)
|
||||
w3 := httptest.NewRecorder()
|
||||
handler(w3, req3)
|
||||
if w3.Code != http.StatusNoContent {
|
||||
t.Errorf("DELETE status = %d, want 204", w3.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminChatLog(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u1", Text: "hello", Timestamp: time.Now(), Type: "public"})
|
||||
|
||||
handler := HandleAdminChatLog(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/chat-log", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var resp ChatLogListResponse
|
||||
json.NewDecoder(w.Body).Decode(&resp)
|
||||
if resp.Total != 1 {
|
||||
t.Errorf("Total = %d, want 1", resp.Total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminChatLog_FilterByRoom(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u1", Text: "hello", Timestamp: time.Now(), Type: "public"})
|
||||
gormDB.Create(&db.Message{Room: "lobby", Username: "u2", Text: "hey", Timestamp: time.Now(), Type: "public"})
|
||||
|
||||
handler := HandleAdminChatLog(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/chat-log?room=main", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var resp ChatLogListResponse
|
||||
json.NewDecoder(w.Body).Decode(&resp)
|
||||
if resp.Total != 1 {
|
||||
t.Errorf("Total = %d, want 1", resp.Total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminChatLogByID_Get(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
msg := db.Message{Room: "main", Username: "u1", Text: "hello", Timestamp: time.Now(), Type: "public"}
|
||||
gormDB.Create(&msg)
|
||||
|
||||
handler := HandleAdminChatLogByID(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/chat-log/1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminChatLogByID_Delete(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
msg := db.Message{Room: "main", Username: "u1", Text: "hello", Timestamp: time.Now(), Type: "public"}
|
||||
gormDB.Create(&msg)
|
||||
|
||||
handler := HandleAdminChatLogByID(deps)
|
||||
req := httptest.NewRequest("DELETE", "/api/admin/chat-log/1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Errorf("status = %d, want 204", w.Code)
|
||||
}
|
||||
|
||||
var count int64
|
||||
gormDB.Model(&db.Message{}).Count(&count)
|
||||
if count != 0 {
|
||||
t.Errorf("message count after delete = %d, want 0", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminChatLogBulk(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u1", Text: "m1", Timestamp: time.Now(), Type: "public"})
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u2", Text: "m2", Timestamp: time.Now(), Type: "public"})
|
||||
|
||||
handler := HandleAdminChatLogBulk(deps)
|
||||
body := `{"ids":[1,2]}`
|
||||
req := httptest.NewRequest("DELETE", "/api/admin/chat-log/bulk", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var count int64
|
||||
gormDB.Model(&db.Message{}).Count(&count)
|
||||
if count != 0 {
|
||||
t.Errorf("message count after bulk delete = %d, want 0", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminChatLogExport(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u1", Text: "hello", Timestamp: time.Now(), Type: "public"})
|
||||
|
||||
handler := HandleAdminChatLogExport(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/chat-log/export", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if !strings.Contains(contentType, "text/csv") {
|
||||
t.Errorf("Content-Type = %q, want text/csv", contentType)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, "hello") {
|
||||
t.Errorf("CSV body missing 'hello': %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlePublicSchedule(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
playlistPath := filepath.Join(tmpDir, "playlist.txt")
|
||||
os.WriteFile(playlistPath, []byte("file /videos/test.mp4\n"), 0644)
|
||||
os.Setenv("PLAYLIST_PATH", playlistPath)
|
||||
defer os.Unsetenv("PLAYLIST_PATH")
|
||||
|
||||
handler := HandlePublicSchedule(deps)
|
||||
req := httptest.NewRequest("GET", "/api/schedule", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMedia_Get(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "test.mp4",
|
||||
OriginalName: "test.mp4",
|
||||
Path: "/media/test.mp4",
|
||||
SizeBytes: 1024,
|
||||
DurationSeconds: 60,
|
||||
Status: "ready",
|
||||
Title: "Test",
|
||||
})
|
||||
|
||||
handler := HandleAdminMedia(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/media", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var resp AdminMediaResponse
|
||||
json.NewDecoder(w.Body).Decode(&resp)
|
||||
if len(resp.Files) != 1 {
|
||||
t.Errorf("len(Files) = %d, want 1", len(resp.Files))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMediaByID_Get(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "test.mp4",
|
||||
OriginalName: "test.mp4",
|
||||
Path: "/media/test.mp4",
|
||||
SizeBytes: 1024,
|
||||
DurationSeconds: 60,
|
||||
Status: "ready",
|
||||
Title: "Test",
|
||||
})
|
||||
|
||||
handler := HandleAdminMediaByID(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/media/1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMediaByID_NotFound(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminMediaByID(deps)
|
||||
req := httptest.NewRequest("GET", "/api/admin/media/999", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want 404", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMediaByID_Put(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "test.mp4",
|
||||
OriginalName: "test.mp4",
|
||||
Path: "/media/test.mp4",
|
||||
SizeBytes: 1024,
|
||||
DurationSeconds: 60,
|
||||
Status: "ready",
|
||||
Title: "Old Title",
|
||||
})
|
||||
|
||||
handler := HandleAdminMediaByID(deps)
|
||||
body := `{"title":"New Title","status":"needs_normalization","tags":"tag1"}`
|
||||
req := httptest.NewRequest("PUT", "/api/admin/media/1", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var updated db.Media
|
||||
gormDB.First(&updated, 1)
|
||||
if updated.Title != "New Title" {
|
||||
t.Errorf("Title = %q, want New Title", updated.Title)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMediaByID_Delete(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
// Create a real temp file for the media record
|
||||
tmpDir := t.TempDir()
|
||||
os.Setenv("MEDIA_DIR", tmpDir)
|
||||
defer os.Unsetenv("MEDIA_DIR")
|
||||
|
||||
mediaPath := filepath.Join(tmpDir, "test.mp4")
|
||||
os.WriteFile(mediaPath, []byte("fake video"), 0644)
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "test.mp4",
|
||||
OriginalName: "test.mp4",
|
||||
Path: mediaPath,
|
||||
SizeBytes: 10,
|
||||
DurationSeconds: 1,
|
||||
Status: "ready",
|
||||
Title: "Test",
|
||||
})
|
||||
|
||||
handler := HandleAdminMediaByID(deps)
|
||||
req := httptest.NewRequest("DELETE", "/api/admin/media/1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var count int64
|
||||
gormDB.Model(&db.Media{}).Count(&count)
|
||||
if count != 0 {
|
||||
t.Errorf("media count after delete = %d, want 0", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMediaUploads_Init(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminMediaUploads(deps)
|
||||
body := `{"fileName":"video.mp4","fileSize":1024,"mimeType":"video/mp4","chunkSize":524288,"totalChunks":1}`
|
||||
req := httptest.NewRequest("POST", "/api/admin/media/uploads", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
var status AdminMediaUploadStatusResponse
|
||||
json.NewDecoder(w.Body).Decode(&status)
|
||||
if status.FileName != "video.mp4" {
|
||||
t.Errorf("FileName = %q, want video.mp4", status.FileName)
|
||||
}
|
||||
if status.UploadID == "" {
|
||||
t.Error("UploadID is empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMediaUploads_InvalidFile(t *testing.T) {
|
||||
deps, _, _ := setupTestDeps(t)
|
||||
|
||||
handler := HandleAdminMediaUploads(deps)
|
||||
body := `{"fileName":"bad.exe","fileSize":1024,"mimeType":"application/x-msdownload","chunkSize":524288,"totalChunks":1}`
|
||||
req := httptest.NewRequest("POST", "/api/admin/media/uploads", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
+78
-1120
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,385 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// FormatDuration converts seconds to HH:MM:SS string.
|
||||
func FormatDuration(seconds int) string {
|
||||
if seconds < 0 {
|
||||
seconds = 0
|
||||
}
|
||||
hours := seconds / 3600
|
||||
minutes := (seconds % 3600) / 60
|
||||
remainingSeconds := seconds % 60
|
||||
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, remainingSeconds)
|
||||
}
|
||||
|
||||
// NormalizationResult holds ffprobe validation output for admin uploads.
|
||||
type NormalizationResult struct {
|
||||
Valid bool `json:"valid"`
|
||||
Errors []string `json:"errors"`
|
||||
}
|
||||
|
||||
// ValidateNormalization runs ffprobe against a media file and checks
|
||||
// conformance with the Linear Void normalization profile (ADR-001).
|
||||
func ValidateNormalization(mediaPath string) NormalizationResult {
|
||||
result := NormalizationResult{Valid: true, Errors: []string{}}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
out, err := exec.CommandContext(ctx,
|
||||
"ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", mediaPath,
|
||||
).CombinedOutput()
|
||||
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
return NormalizationResult{Valid: false, Errors: []string{"ffprobe timed out"}}
|
||||
}
|
||||
if err != nil {
|
||||
return NormalizationResult{Valid: false, Errors: []string{fmt.Sprintf("ffprobe failed: %v (%s)", err, strings.TrimSpace(string(out)))}}
|
||||
}
|
||||
|
||||
var probe struct {
|
||||
Streams []struct {
|
||||
CodecName string `json:"codec_name"`
|
||||
CodecType string `json:"codec_type"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
RFrameRate string `json:"r_frame_rate"`
|
||||
PixFmt string `json:"pix_fmt"`
|
||||
SampleRate string `json:"sample_rate"`
|
||||
} `json:"streams"`
|
||||
}
|
||||
if err := json.Unmarshal(out, &probe); err != nil {
|
||||
return NormalizationResult{Valid: false, Errors: []string{"ffprobe JSON parse failed: " + err.Error()}}
|
||||
}
|
||||
|
||||
var hasVideo, hasAudio bool
|
||||
for _, s := range probe.Streams {
|
||||
switch s.CodecType {
|
||||
case "video":
|
||||
hasVideo = true
|
||||
if s.CodecName != "h264" {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("video codec must be h264, got %s", s.CodecName))
|
||||
}
|
||||
if s.RFrameRate != "30/1" {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("framerate must be 30 fps constant, got %s", s.RFrameRate))
|
||||
}
|
||||
if s.Width != 1280 && s.Width != 1920 {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("width must be 1280 or 1920, got %d", s.Width))
|
||||
}
|
||||
if s.Height != 720 && s.Height != 1080 {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("height must be 720 or 1080, got %d", s.Height))
|
||||
}
|
||||
if s.PixFmt != "yuv420p" {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("pixel format must be yuv420p, got %s", s.PixFmt))
|
||||
}
|
||||
case "audio":
|
||||
hasAudio = true
|
||||
if s.CodecName != "aac" {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("audio codec must be aac, got %s", s.CodecName))
|
||||
}
|
||||
if s.SampleRate != "48000" {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("sample rate must be 48000 Hz, got %s", s.SampleRate))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !hasVideo {
|
||||
result.Errors = append(result.Errors, "no video stream found")
|
||||
}
|
||||
if !hasAudio {
|
||||
result.Errors = append(result.Errors, "no audio stream found")
|
||||
}
|
||||
|
||||
if len(result.Errors) > 0 {
|
||||
result.Valid = false
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// ProbeDurationSeconds returns the duration of a media file in seconds.
|
||||
func ProbeDurationSeconds(mediaPath string) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
out, err := exec.CommandContext(
|
||||
ctx,
|
||||
"ffprobe",
|
||||
"-v", "error",
|
||||
"-show_entries", "format=duration",
|
||||
"-of", "default=noprint_wrappers=1:nokey=1",
|
||||
mediaPath,
|
||||
).CombinedOutput()
|
||||
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
return 0, fmt.Errorf("ffprobe timed out")
|
||||
}
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("ffprobe failed: %w (%s)", err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
|
||||
rawDuration := strings.TrimSpace(string(out))
|
||||
if rawDuration == "" {
|
||||
return 0, fmt.Errorf("ffprobe returned empty duration")
|
||||
}
|
||||
|
||||
secondsFloat, err := strconv.ParseFloat(rawDuration, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to parse duration %q: %w", rawDuration, err)
|
||||
}
|
||||
if secondsFloat < 0 {
|
||||
secondsFloat = 0
|
||||
}
|
||||
|
||||
seconds := int(secondsFloat + 0.5)
|
||||
if secondsFloat > 0 && seconds == 0 {
|
||||
seconds = 1
|
||||
}
|
||||
|
||||
return seconds, nil
|
||||
}
|
||||
|
||||
// DetectMime reads the first 512 bytes of a file to detect its MIME type.
|
||||
func DetectMime(filePath string) (string, error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
head := make([]byte, 512)
|
||||
n, err := f.Read(head)
|
||||
if err != nil && err != io.EOF {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.SplitN(http.DetectContentType(head[:n]), ";", 2)[0], nil
|
||||
}
|
||||
|
||||
// IsAllowedExtension checks if the file extension is a supported video format.
|
||||
func IsAllowedExtension(fileName string) bool {
|
||||
ext := strings.ToLower(filepath.Ext(fileName))
|
||||
switch ext {
|
||||
case ".mp4", ".mov", ".mkv", ".webm", ".m4v", ".ts":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IsAllowedMime checks if the MIME type is a supported video type.
|
||||
func IsAllowedMime(mime string) bool {
|
||||
mime = strings.TrimSpace(strings.ToLower(mime))
|
||||
if mime == "" {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(mime, "video/") {
|
||||
return true
|
||||
}
|
||||
return mime == "application/octet-stream"
|
||||
}
|
||||
|
||||
// UniqueTargetPath returns a unique file path in mediaDir, appending -N if needed.
|
||||
func UniqueTargetPath(mediaDir, fileName string) string {
|
||||
baseName := filepath.Base(fileName)
|
||||
ext := filepath.Ext(baseName)
|
||||
nameOnly := strings.TrimSuffix(baseName, ext)
|
||||
candidate := filepath.Join(mediaDir, baseName)
|
||||
|
||||
for i := 1; ; i++ {
|
||||
if _, err := os.Stat(candidate); os.IsNotExist(err) {
|
||||
return candidate
|
||||
}
|
||||
candidate = filepath.Join(mediaDir, fmt.Sprintf("%s-%d%s", nameOnly, i, ext))
|
||||
}
|
||||
}
|
||||
|
||||
// FileEntry represents a media file for API responses.
|
||||
type FileEntry struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
OriginalName string `json:"originalName"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
DurationSeconds int `json:"durationSeconds"`
|
||||
Runtime string `json:"runtime"`
|
||||
Status string `json:"status"`
|
||||
Title string `json:"title"`
|
||||
Tags string `json:"tags"`
|
||||
UploadedAt string `json:"uploadedAt"`
|
||||
ModifiedAt string `json:"modifiedAt"`
|
||||
}
|
||||
|
||||
// MediaRecord is the interface for media DB records.
|
||||
type MediaRecord interface {
|
||||
GetID() uint
|
||||
GetFilename() string
|
||||
GetOriginalName() string
|
||||
GetPath() string
|
||||
GetSizeBytes() int64
|
||||
GetDurationSeconds() int
|
||||
GetStatus() string
|
||||
GetTitle() string
|
||||
GetTags() string
|
||||
GetUploadedAt() time.Time
|
||||
GetUpdatedAt() time.Time
|
||||
}
|
||||
|
||||
// ListFiles returns all media files sorted by upload date descending.
|
||||
func ListFiles(db *gorm.DB) ([]FileEntry, error) {
|
||||
type record struct {
|
||||
ID uint
|
||||
Filename string
|
||||
OriginalName string
|
||||
Path string
|
||||
SizeBytes int64
|
||||
DurationSeconds int
|
||||
Status string
|
||||
Title string
|
||||
Tags string
|
||||
UploadedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
var records []record
|
||||
if err := db.Table("media").Order("uploaded_at desc").Find(&records).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files := make([]FileEntry, 0, len(records))
|
||||
for _, r := range records {
|
||||
files = append(files, FileEntry{
|
||||
ID: r.ID,
|
||||
Name: r.Filename,
|
||||
OriginalName: r.OriginalName,
|
||||
Path: r.Path,
|
||||
Size: r.SizeBytes,
|
||||
DurationSeconds: r.DurationSeconds,
|
||||
Runtime: FormatDuration(r.DurationSeconds),
|
||||
Status: r.Status,
|
||||
Title: r.Title,
|
||||
Tags: r.Tags,
|
||||
UploadedAt: r.UploadedAt.UTC().Format(time.RFC3339),
|
||||
ModifiedAt: r.UpdatedAt.UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
if files[i].UploadedAt == files[j].UploadedAt {
|
||||
return files[i].Name < files[j].Name
|
||||
}
|
||||
return files[i].UploadedAt > files[j].UploadedAt
|
||||
})
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// ParsePlaylistEntries parses playlist content into schedule entries.
|
||||
type ScheduleEntry struct {
|
||||
Index int `json:"index"`
|
||||
Path string `json:"path"`
|
||||
Label string `json:"label"`
|
||||
DurationSeconds int `json:"durationSeconds"`
|
||||
Runtime string `json:"runtime"`
|
||||
Status string `json:"status"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
// ParsePlaylist parses playlist text content into entries.
|
||||
func ParsePlaylist(content string) []ScheduleEntry {
|
||||
entries := []ScheduleEntry{}
|
||||
lines := strings.Split(content, "\n")
|
||||
for _, rawLine := range lines {
|
||||
line := strings.TrimSpace(rawLine)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "file ") {
|
||||
line = strings.TrimSpace(strings.TrimPrefix(line, "file "))
|
||||
line = strings.Trim(line, "'\"")
|
||||
}
|
||||
|
||||
entries = append(entries, ScheduleEntry{
|
||||
Index: len(entries) + 1,
|
||||
Path: line,
|
||||
Label: filepath.Base(line),
|
||||
})
|
||||
}
|
||||
|
||||
return entries
|
||||
}
|
||||
|
||||
// UploadSessionDir returns the directory for an upload session's chunks.
|
||||
func UploadSessionDir(mediaDir, uploadID string) string {
|
||||
return filepath.Join(mediaDir, ".uploads", uploadID)
|
||||
}
|
||||
|
||||
// UploadSessionMetaPath returns the path to the session metadata file.
|
||||
func UploadSessionMetaPath(mediaDir, uploadID string) string {
|
||||
return filepath.Join(UploadSessionDir(mediaDir, uploadID), "session.json")
|
||||
}
|
||||
|
||||
// UploadChunkPath returns the path for a specific chunk file.
|
||||
func UploadChunkPath(mediaDir, uploadID string, chunkIndex int) string {
|
||||
return filepath.Join(UploadSessionDir(mediaDir, uploadID), fmt.Sprintf("chunk-%06d.part", chunkIndex))
|
||||
}
|
||||
|
||||
// CountUploadedChunks counts how many chunks exist for a session.
|
||||
func CountUploadedChunks(mediaDir, uploadID string, totalChunks int) int {
|
||||
uploaded := 0
|
||||
for i := 0; i < totalChunks; i++ {
|
||||
if _, err := os.Stat(UploadChunkPath(mediaDir, uploadID, i)); err == nil {
|
||||
uploaded++
|
||||
}
|
||||
}
|
||||
return uploaded
|
||||
}
|
||||
|
||||
// ValidateUploadID checks that an upload ID is a valid hex string of appropriate length.
|
||||
func ValidateUploadID(uploadID string) bool {
|
||||
if len(uploadID) < 16 || len(uploadID) > 128 {
|
||||
return false
|
||||
}
|
||||
for _, r := range uploadID {
|
||||
if (r < 'a' || r > 'f') && (r < '0' || r > '9') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetMediaIDFromPath extracts a media ID from a URL path like /api/admin/media/123.
|
||||
func GetMediaIDFromPath(path, prefix string) (uint, error) {
|
||||
if !strings.HasPrefix(path, prefix) {
|
||||
return 0, fmt.Errorf("invalid media path")
|
||||
}
|
||||
idPart := strings.TrimPrefix(path, prefix)
|
||||
idPart = strings.TrimSpace(idPart)
|
||||
if idPart == "" || strings.Contains(idPart, "/") {
|
||||
return 0, fmt.Errorf("invalid media id")
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(idPart, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
return 0, fmt.Errorf("invalid media id")
|
||||
}
|
||||
|
||||
return uint(id), nil
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatDuration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
seconds int
|
||||
expected string
|
||||
}{
|
||||
{"zero", 0, "00:00:00"},
|
||||
{"one second", 1, "00:00:01"},
|
||||
{"one minute", 60, "00:01:00"},
|
||||
{"one hour", 3600, "01:00:00"},
|
||||
{"mixed", 3661, "01:01:01"},
|
||||
{"large", 86399, "23:59:59"},
|
||||
{"negative", -5, "00:00:00"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := FormatDuration(tt.seconds); got != tt.expected {
|
||||
t.Errorf("FormatDuration(%d) = %q, want %q", tt.seconds, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllowedExtension(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
expected bool
|
||||
}{
|
||||
{"video.mp4", true},
|
||||
{"video.mov", true},
|
||||
{"video.mkv", true},
|
||||
{"video.webm", true},
|
||||
{"video.m4v", true},
|
||||
{"video.ts", true},
|
||||
{"video.MP4", true},
|
||||
{"video.avi", false},
|
||||
{"video.flv", false},
|
||||
{"video.wmv", false},
|
||||
{"video", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsAllowedExtension(tt.name); got != tt.expected {
|
||||
t.Errorf("IsAllowedExtension(%q) = %v, want %v", tt.name, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllowedMime(t *testing.T) {
|
||||
tests := []struct {
|
||||
mime string
|
||||
expected bool
|
||||
}{
|
||||
{"video/mp4", true},
|
||||
{"video/quicktime", true},
|
||||
{"video/x-matroska", true},
|
||||
{"video/webm", true},
|
||||
{"application/octet-stream", true},
|
||||
{"", true},
|
||||
{"image/jpeg", false},
|
||||
{"audio/mpeg", false},
|
||||
{"text/plain", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.mime, func(t *testing.T) {
|
||||
if got := IsAllowedMime(tt.mime); got != tt.expected {
|
||||
t.Errorf("IsAllowedMime(%q) = %v, want %v", tt.mime, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateUploadID(t *testing.T) {
|
||||
tests := []struct {
|
||||
id string
|
||||
expected bool
|
||||
}{
|
||||
{"abcdef0123456789", true}, // 16 hex chars
|
||||
{"abcdef0123456789abcdef0123456789", true}, // 32 hex chars
|
||||
{"abc", false}, // too short
|
||||
{"abcdef012345678", false}, // 15 chars (too short)
|
||||
{"abcdef0123456789g", false}, // non-hex char
|
||||
{"ABCDEF0123456789", false}, // uppercase (not allowed)
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.id, func(t *testing.T) {
|
||||
if got := ValidateUploadID(tt.id); got != tt.expected {
|
||||
t.Errorf("ValidateUploadID(%q) = %v, want %v", tt.id, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePlaylist(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
expected int
|
||||
first string
|
||||
}{
|
||||
{
|
||||
name: "simple",
|
||||
content: "/videos/video1.mp4\n/videos/video2.mp4\n",
|
||||
expected: 2,
|
||||
first: "/videos/video1.mp4",
|
||||
},
|
||||
{
|
||||
name: "with file prefix",
|
||||
content: "file /videos/video1.mp4\nfile '/videos/video2.mp4'\n",
|
||||
expected: 2,
|
||||
first: "/videos/video1.mp4",
|
||||
},
|
||||
{
|
||||
name: "with comments and blanks",
|
||||
content: "# comment\n\n/videos/video1.mp4\n\n# another comment\n/videos/video2.mp4\n",
|
||||
expected: 2,
|
||||
first: "/videos/video1.mp4",
|
||||
},
|
||||
{
|
||||
name: "empty",
|
||||
content: "",
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "only comments",
|
||||
content: "# comment\n# another\n",
|
||||
expected: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
entries := ParsePlaylist(tt.content)
|
||||
if len(entries) != tt.expected {
|
||||
t.Errorf("ParsePlaylist() len = %d, want %d", len(entries), tt.expected)
|
||||
}
|
||||
if tt.expected > 0 && entries[0].Path != tt.first {
|
||||
t.Errorf("ParsePlaylist()[0].Path = %q, want %q", entries[0].Path, tt.first)
|
||||
}
|
||||
// Check indices
|
||||
for i, e := range entries {
|
||||
if e.Index != i+1 {
|
||||
t.Errorf("ParsePlaylist()[%d].Index = %d, want %d", i, e.Index, i+1)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMediaIDFromPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
prefix string
|
||||
expected uint
|
||||
expectErr bool
|
||||
}{
|
||||
{"/api/admin/media/123", "/api/admin/media/", 123, false},
|
||||
{"/api/admin/media/1", "/api/admin/media/", 1, false},
|
||||
{"/api/admin/media/", "/api/admin/media/", 0, true},
|
||||
{"/api/admin/media/abc", "/api/admin/media/", 0, true},
|
||||
{"/api/admin/media/0", "/api/admin/media/", 0, true},
|
||||
{"/other/path", "/api/admin/media/", 0, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.path, func(t *testing.T) {
|
||||
id, err := GetMediaIDFromPath(tt.path, tt.prefix)
|
||||
if tt.expectErr && err == nil {
|
||||
t.Errorf("GetMediaIDFromPath(%q) expected error, got nil", tt.path)
|
||||
}
|
||||
if !tt.expectErr && err != nil {
|
||||
t.Errorf("GetMediaIDFromPath(%q) error = %v", tt.path, err)
|
||||
}
|
||||
if id != tt.expected {
|
||||
t.Errorf("GetMediaIDFromPath(%q) = %d, want %d", tt.path, id, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniqueTargetPath(t *testing.T) {
|
||||
// Test that the function returns a path with the mediaDir prefix
|
||||
// and the correct filename. We can't easily test collision behavior
|
||||
// without filesystem setup, but we can verify the basic path.
|
||||
expected := filepath.Join("/tmp/media", "video.mp4")
|
||||
path := UniqueTargetPath("/tmp/media", "video.mp4")
|
||||
if path != expected {
|
||||
t.Errorf("UniqueTargetPath() = %q, want %q", path, expected)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"chat/src/auth"
|
||||
"chat/src/db"
|
||||
"chat/src/ws"
|
||||
)
|
||||
|
||||
// HandleMetrics returns Prometheus-format metrics for the chat service.
|
||||
func HandleMetrics(gormDB *gorm.DB, redis db.RedisClient, hub *ws.Hub, sessionStore *auth.SessionStore, dbDriver, instanceID string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var storedMessages int64
|
||||
if gormDB != nil {
|
||||
_ = gormDB.Model(&db.Message{}).Count(&storedMessages).Error
|
||||
}
|
||||
|
||||
var moderationActions int64
|
||||
if gormDB != nil {
|
||||
_ = gormDB.Model(&db.ModerationLog{}).Count(&moderationActions).Error
|
||||
}
|
||||
|
||||
redisEnabled := 0
|
||||
redisUp := 0
|
||||
redisClients := 0
|
||||
if redis != nil {
|
||||
redisEnabled = 1
|
||||
if err := redis.Ping(context.Background()); err == nil {
|
||||
redisUp = 1
|
||||
if info, err := redis.Info(context.Background(), "clients"); err == nil {
|
||||
for _, line := range strings.Split(info, "\n") {
|
||||
if strings.HasPrefix(line, "connected_clients:") {
|
||||
if value, err := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(line, "connected_clients:"))); err == nil {
|
||||
redisClients = value
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
connectedClients := 0
|
||||
dmsSent := int64(0)
|
||||
if hub != nil {
|
||||
connectedClients = hub.ConnectedClients()
|
||||
dmsSent = hub.DMsSent
|
||||
}
|
||||
|
||||
activeSessions := 0
|
||||
if sessionStore != nil {
|
||||
activeSessions = sessionStore.CountActive()
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
|
||||
|
||||
writeLine := func(name string, value any) {
|
||||
fmt.Fprintf(w, "%s %v\n", name, value)
|
||||
}
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_connected_clients Current connected WebSocket clients.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_connected_clients gauge")
|
||||
writeLine("linearvoid_chat_connected_clients", connectedClients)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_messages_total Total stored chat messages.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_messages_total gauge")
|
||||
writeLine("linearvoid_chat_messages_total", storedMessages)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_dms_total Total private messages sent.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_dms_total counter")
|
||||
writeLine("linearvoid_chat_dms_total", dmsSent)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_moderation_actions_total Total moderation log entries.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_moderation_actions_total gauge")
|
||||
writeLine("linearvoid_chat_moderation_actions_total", moderationActions)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_redis_enabled Redis configured for chat service.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_redis_enabled gauge")
|
||||
writeLine("linearvoid_chat_redis_enabled", redisEnabled)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_redis_up Redis reachable from chat service.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_redis_up gauge")
|
||||
writeLine("linearvoid_chat_redis_up", redisUp)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_redis_connected_clients Redis connected client count seen by chat service.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_redis_connected_clients gauge")
|
||||
writeLine("linearvoid_chat_redis_connected_clients", redisClients)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_admin_sessions_active Active unexpired admin sessions.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_admin_sessions_active gauge")
|
||||
writeLine("linearvoid_chat_admin_sessions_active", activeSessions)
|
||||
|
||||
fmt.Fprintln(w, "# HELP linearvoid_chat_build_info Static build info for chat service.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_chat_build_info gauge")
|
||||
fmt.Fprintf(w, "linearvoid_chat_build_info{service=\"chat\",database_driver=\"%s\",instance_id=\"%s\"} 1\n", dbDriver, instanceID)
|
||||
}
|
||||
}
|
||||
|
||||
// HandleHealth returns a simple health check endpoint.
|
||||
func HandleHealth() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, `{"status":"healthy","service":"chat","timestamp":"`+time.Now().UTC().Format(time.RFC3339)+`"}`)
|
||||
}
|
||||
}
|
||||
|
||||
// HandleNotFound returns a 404 JSON response for all unhandled paths.
|
||||
func HandleNotFound() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprint(w, `{"error":"not found"}`)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"chat/src/auth"
|
||||
"chat/src/db"
|
||||
"chat/src/ws"
|
||||
)
|
||||
|
||||
func TestHandleHealth(t *testing.T) {
|
||||
handler := HandleHealth()
|
||||
req := httptest.NewRequest("GET", "/health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, `"healthy"`) {
|
||||
t.Errorf("body missing healthy: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, `"chat"`) {
|
||||
t.Errorf("body missing service name: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleNotFound(t *testing.T) {
|
||||
handler := HandleNotFound()
|
||||
req := httptest.NewRequest("GET", "/nonexistent", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want 404", w.Code)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, `"not found"`) {
|
||||
t.Errorf("body missing not found: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMetrics(t *testing.T) {
|
||||
gormDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open test DB: %v", err)
|
||||
}
|
||||
gormDB.AutoMigrate(&db.Message{}, &db.ModerationLog{})
|
||||
|
||||
// Add some test data
|
||||
gormDB.Create(&db.Message{Room: "main", Username: "u1", Text: "hi", Timestamp: time.Now(), Type: "public"})
|
||||
gormDB.Create(&db.ModerationLog{Action: "mute", Target: "u2", Moderator: "admin", Reason: "spam", Timestamp: time.Now()})
|
||||
|
||||
sessionStore := auth.NewSessionStore()
|
||||
hub := ws.NewHub(gormDB, nil, "test-instance", 10, 10*time.Second, "admin")
|
||||
hub.DMsSent = 5
|
||||
|
||||
handler := HandleMetrics(gormDB, nil, hub, sessionStore, "sqlite", "test-instance")
|
||||
req := httptest.NewRequest("GET", "/metrics", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
|
||||
// Check required metric names
|
||||
requiredMetrics := []string{
|
||||
"linearvoid_chat_connected_clients",
|
||||
"linearvoid_chat_messages_total",
|
||||
"linearvoid_chat_dms_total",
|
||||
"linearvoid_chat_moderation_actions_total",
|
||||
"linearvoid_chat_redis_enabled",
|
||||
"linearvoid_chat_redis_up",
|
||||
"linearvoid_chat_redis_connected_clients",
|
||||
"linearvoid_chat_admin_sessions_active",
|
||||
"linearvoid_chat_build_info",
|
||||
}
|
||||
|
||||
for _, metric := range requiredMetrics {
|
||||
if !strings.Contains(body, metric) {
|
||||
t.Errorf("body missing metric %s", metric)
|
||||
}
|
||||
}
|
||||
|
||||
// Check build_info has instance_id label
|
||||
if !strings.Contains(body, `instance_id="test-instance"`) {
|
||||
t.Error("build_info missing instance_id label")
|
||||
}
|
||||
|
||||
// Check values
|
||||
if !strings.Contains(body, "linearvoid_chat_messages_total 1") {
|
||||
t.Error("messages_total should be 1")
|
||||
}
|
||||
if !strings.Contains(body, "linearvoid_chat_moderation_actions_total 1") {
|
||||
t.Error("moderation_actions_total should be 1")
|
||||
}
|
||||
if !strings.Contains(body, "linearvoid_chat_dms_total 5") {
|
||||
t.Error("dms_total should be 5")
|
||||
}
|
||||
if !strings.Contains(body, "linearvoid_chat_redis_enabled 0") {
|
||||
t.Error("redis_enabled should be 0 (nil redis)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMetrics_WrongMethod(t *testing.T) {
|
||||
handler := HandleMetrics(nil, nil, nil, nil, "", "")
|
||||
req := httptest.NewRequest("POST", "/metrics", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("status = %d, want 405", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMetrics_WithRedis(t *testing.T) {
|
||||
gormDB, _ := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
gormDB.AutoMigrate(&db.Message{}, &db.ModerationLog{})
|
||||
|
||||
// mockRedis for metrics test
|
||||
mr := &metricsMockRedis{}
|
||||
sessionStore := auth.NewSessionStore()
|
||||
hub := ws.NewHub(gormDB, mr, "test-instance", 10, 10*time.Second, "admin")
|
||||
|
||||
handler := HandleMetrics(gormDB, mr, hub, sessionStore, "sqlite", "inst-123")
|
||||
req := httptest.NewRequest("GET", "/metrics", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, "linearvoid_chat_redis_enabled 1") {
|
||||
t.Error("redis_enabled should be 1")
|
||||
}
|
||||
if !strings.Contains(body, "linearvoid_chat_redis_up 1") {
|
||||
t.Error("redis_up should be 1")
|
||||
}
|
||||
if !strings.Contains(body, "linearvoid_chat_redis_connected_clients 5") {
|
||||
t.Error("redis_connected_clients should be 5")
|
||||
}
|
||||
}
|
||||
|
||||
// metricsMockRedis is a minimal mock for metrics tests.
|
||||
type metricsMockRedis struct{}
|
||||
|
||||
func (m *metricsMockRedis) Ping(ctx context.Context) error { return nil }
|
||||
func (m *metricsMockRedis) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *metricsMockRedis) Get(ctx context.Context, key string) (string, error) { return "", nil }
|
||||
func (m *metricsMockRedis) Del(ctx context.Context, keys ...string) error { return nil }
|
||||
func (m *metricsMockRedis) Incr(ctx context.Context, key string) (int64, error) { return 0, nil }
|
||||
func (m *metricsMockRedis) Decr(ctx context.Context, key string) (int64, error) { return 0, nil }
|
||||
func (m *metricsMockRedis) Expire(ctx context.Context, key string, expiration time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *metricsMockRedis) Exists(ctx context.Context, keys ...string) (int64, error) { return 0, nil }
|
||||
func (m *metricsMockRedis) Publish(ctx context.Context, channel string, message interface{}) error {
|
||||
return nil
|
||||
}
|
||||
func (m *metricsMockRedis) PSubscribe(ctx context.Context, channels ...string) db.PubSub { return nil }
|
||||
func (m *metricsMockRedis) Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error) {
|
||||
return nil, 0, nil
|
||||
}
|
||||
func (m *metricsMockRedis) Info(ctx context.Context, sections ...string) (string, error) {
|
||||
return "connected_clients:5\nused_memory_human:1M\n", nil
|
||||
}
|
||||
func (m *metricsMockRedis) Close() error { return nil }
|
||||
@@ -0,0 +1,75 @@
|
||||
package ratelimit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RedisClient is the subset of Redis operations needed for rate limiting.
|
||||
type RedisClient interface {
|
||||
Incr(ctx context.Context, key string) (int64, error)
|
||||
Expire(ctx context.Context, key string, expiration time.Duration) error
|
||||
Decr(ctx context.Context, key string) (int64, error)
|
||||
}
|
||||
|
||||
// CheckMessageRate checks if a user has exceeded the message rate limit.
|
||||
// Returns true if allowed, false if rate limited.
|
||||
func CheckMessageRate(ctx context.Context, redis RedisClient, username string, max int, window time.Duration) (bool, error) {
|
||||
if redis == nil {
|
||||
return true, nil // No Redis = no rate limiting
|
||||
}
|
||||
key := fmt.Sprintf("rate:%s", username)
|
||||
count, err := redis.Incr(ctx, key)
|
||||
if err != nil {
|
||||
return true, err // Fail open on Redis error
|
||||
}
|
||||
if count == 1 {
|
||||
_ = redis.Expire(ctx, key, window)
|
||||
}
|
||||
return count <= int64(max), nil
|
||||
}
|
||||
|
||||
// CheckLoginRate checks if an IP has exceeded the login attempt limit.
|
||||
// Returns true if allowed, false if rate limited.
|
||||
func CheckLoginRate(ctx context.Context, redis RedisClient, clientIP string) (bool, error) {
|
||||
if redis == nil {
|
||||
return true, nil
|
||||
}
|
||||
key := fmt.Sprintf("login_attempts:%s", clientIP)
|
||||
count, err := redis.Incr(ctx, key)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
if count == 1 {
|
||||
_ = redis.Expire(ctx, key, 5*time.Minute)
|
||||
}
|
||||
return count <= 5, nil
|
||||
}
|
||||
|
||||
// CheckWSConnRate checks if an IP has exceeded the WebSocket connection limit.
|
||||
// Returns true if allowed, false if rate limited.
|
||||
// Caller must call Decr on disconnect.
|
||||
func CheckWSConnRate(ctx context.Context, redis RedisClient, clientIP string) (bool, error) {
|
||||
if redis == nil {
|
||||
return true, nil
|
||||
}
|
||||
key := fmt.Sprintf("ws_conn:%s", clientIP)
|
||||
count, err := redis.Incr(ctx, key)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
if count == 1 {
|
||||
_ = redis.Expire(ctx, key, 60*time.Second)
|
||||
}
|
||||
return count <= 5, nil
|
||||
}
|
||||
|
||||
// DecrWSConn decrements the WebSocket connection counter for an IP.
|
||||
func DecrWSConn(ctx context.Context, redis RedisClient, clientIP string) {
|
||||
if redis == nil {
|
||||
return
|
||||
}
|
||||
key := fmt.Sprintf("ws_conn:%s", clientIP)
|
||||
_, _ = redis.Decr(ctx, key)
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package ratelimit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// mockRedis implements RedisClient for testing.
|
||||
type mockRedis struct {
|
||||
mu sync.Mutex
|
||||
store map[string]int64
|
||||
}
|
||||
|
||||
func newMockRedis() *mockRedis {
|
||||
return &mockRedis{store: make(map[string]int64)}
|
||||
}
|
||||
|
||||
func (m *mockRedis) Incr(ctx context.Context, key string) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.store[key]++
|
||||
return m.store[key], nil
|
||||
}
|
||||
|
||||
func (m *mockRedis) Expire(ctx context.Context, key string, expiration time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockRedis) Decr(ctx context.Context, key string) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.store[key]--
|
||||
return m.store[key], nil
|
||||
}
|
||||
|
||||
func TestCheckMessageRate(t *testing.T) {
|
||||
redis := newMockRedis()
|
||||
ctx := context.Background()
|
||||
|
||||
// First 10 messages allowed
|
||||
for i := 0; i < 10; i++ {
|
||||
allowed, err := CheckMessageRate(ctx, redis, "user1", 10, 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("CheckMessageRate #%d error = %v", i+1, err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Errorf("CheckMessageRate #%d = false, want true", i+1)
|
||||
}
|
||||
}
|
||||
|
||||
// 11th message blocked
|
||||
allowed, err := CheckMessageRate(ctx, redis, "user1", 10, 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("CheckMessageRate #11 error = %v", err)
|
||||
}
|
||||
if allowed {
|
||||
t.Error("CheckMessageRate #11 = true, want false (rate limited)")
|
||||
}
|
||||
|
||||
// Different user not affected
|
||||
allowed, err = CheckMessageRate(ctx, redis, "user2", 10, 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("CheckMessageRate user2 error = %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Error("CheckMessageRate user2 = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMessageRate_NilRedis(t *testing.T) {
|
||||
allowed, err := CheckMessageRate(context.Background(), nil, "user1", 10, 10*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("CheckMessageRate nil redis error = %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Error("CheckMessageRate nil redis = false, want true (fail open)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckLoginRate(t *testing.T) {
|
||||
redis := newMockRedis()
|
||||
ctx := context.Background()
|
||||
|
||||
// First 5 attempts allowed
|
||||
for i := 0; i < 5; i++ {
|
||||
allowed, err := CheckLoginRate(ctx, redis, "192.168.1.1")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckLoginRate #%d error = %v", i+1, err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Errorf("CheckLoginRate #%d = false, want true", i+1)
|
||||
}
|
||||
}
|
||||
|
||||
// 6th attempt blocked
|
||||
allowed, err := CheckLoginRate(ctx, redis, "192.168.1.1")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckLoginRate #6 error = %v", err)
|
||||
}
|
||||
if allowed {
|
||||
t.Error("CheckLoginRate #6 = true, want false (rate limited)")
|
||||
}
|
||||
|
||||
// Different IP not affected
|
||||
allowed, err = CheckLoginRate(ctx, redis, "10.0.0.1")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckLoginRate other IP error = %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Error("CheckLoginRate other IP = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckLoginRate_NilRedis(t *testing.T) {
|
||||
allowed, err := CheckLoginRate(context.Background(), nil, "192.168.1.1")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckLoginRate nil redis error = %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Error("CheckLoginRate nil redis = false, want true (fail open)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckWSConnRate(t *testing.T) {
|
||||
redis := newMockRedis()
|
||||
ctx := context.Background()
|
||||
|
||||
// First 5 connections allowed
|
||||
for i := 0; i < 5; i++ {
|
||||
allowed, err := CheckWSConnRate(ctx, redis, "192.168.1.1")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckWSConnRate #%d error = %v", i+1, err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Errorf("CheckWSConnRate #%d = false, want true", i+1)
|
||||
}
|
||||
}
|
||||
|
||||
// 6th connection blocked
|
||||
allowed, err := CheckWSConnRate(ctx, redis, "192.168.1.1")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckWSConnRate #6 error = %v", err)
|
||||
}
|
||||
if allowed {
|
||||
t.Error("CheckWSConnRate #6 = true, want false (rate limited)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckWSConnRate_NilRedis(t *testing.T) {
|
||||
allowed, err := CheckWSConnRate(context.Background(), nil, "192.168.1.1")
|
||||
if err != nil {
|
||||
t.Fatalf("CheckWSConnRate nil redis error = %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Error("CheckWSConnRate nil redis = false, want true (fail open)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecrWSConn(t *testing.T) {
|
||||
redis := newMockRedis()
|
||||
ctx := context.Background()
|
||||
|
||||
// Increment to 5
|
||||
for i := 0; i < 5; i++ {
|
||||
CheckWSConnRate(ctx, redis, "192.168.1.1")
|
||||
}
|
||||
|
||||
// 6th should be blocked
|
||||
allowed, _ := CheckWSConnRate(ctx, redis, "192.168.1.1")
|
||||
if allowed {
|
||||
t.Error("6th connection should be blocked")
|
||||
}
|
||||
|
||||
// Decrement twice (simulate 2 disconnects)
|
||||
DecrWSConn(ctx, redis, "192.168.1.1")
|
||||
DecrWSConn(ctx, redis, "192.168.1.1")
|
||||
|
||||
// Now should be allowed again (counter 6→4, then incr→5)
|
||||
allowed, _ = CheckWSConnRate(ctx, redis, "192.168.1.1")
|
||||
if !allowed {
|
||||
t.Error("after 2x DecrWSConn, connection should be allowed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecrWSConn_NilRedis(t *testing.T) {
|
||||
// Should not panic
|
||||
DecrWSConn(context.Background(), nil, "192.168.1.1")
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"chat/src/db"
|
||||
"chat/src/ratelimit"
|
||||
)
|
||||
|
||||
// Upgrader is the WebSocket upgrader configuration.
|
||||
var Upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
// ClientInfo stores metadata about a connected WebSocket client.
|
||||
type ClientInfo struct {
|
||||
Username string
|
||||
Room string
|
||||
}
|
||||
|
||||
// Hub manages all WebSocket connections and message routing.
|
||||
type Hub struct {
|
||||
mu sync.Mutex
|
||||
clients map[*websocket.Conn]bool
|
||||
clientMeta map[*websocket.Conn]ClientInfo
|
||||
DB *gorm.DB
|
||||
Redis db.RedisClient
|
||||
InstanceID string
|
||||
RateLimitMax int
|
||||
RateLimitWindow time.Duration
|
||||
AdminUsername string
|
||||
DMsSent int64
|
||||
}
|
||||
|
||||
// NewHub creates a new WebSocket hub.
|
||||
func NewHub(gormDB *gorm.DB, redis db.RedisClient, instanceID string, rateMax int, rateWindow time.Duration, adminUser string) *Hub {
|
||||
return &Hub{
|
||||
clients: make(map[*websocket.Conn]bool),
|
||||
clientMeta: make(map[*websocket.Conn]ClientInfo),
|
||||
DB: gormDB,
|
||||
Redis: redis,
|
||||
InstanceID: instanceID,
|
||||
RateLimitMax: rateMax,
|
||||
RateLimitWindow: rateWindow,
|
||||
AdminUsername: adminUser,
|
||||
}
|
||||
}
|
||||
|
||||
// ConnectedClients returns the number of currently connected clients.
|
||||
func (h *Hub) ConnectedClients() int {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return len(h.clients)
|
||||
}
|
||||
|
||||
// Broadcast sends a message to all connected clients and persists to DB.
|
||||
func (h *Hub) Broadcast(msg db.Message) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
// Persist to configured SQL backend.
|
||||
if h.DB != nil {
|
||||
h.DB.Create(&msg)
|
||||
}
|
||||
|
||||
// Local fan-out
|
||||
for client := range h.clients {
|
||||
if err := client.WriteJSON(msg); err != nil {
|
||||
client.Close()
|
||||
delete(h.clients, client)
|
||||
delete(h.clientMeta, client)
|
||||
}
|
||||
}
|
||||
|
||||
// Redis pub/sub for multi-instance fan-out
|
||||
if h.Redis != nil && msg.Room != "" {
|
||||
payload, _ := json.Marshal(map[string]any{
|
||||
"instance": h.InstanceID,
|
||||
"msg": msg,
|
||||
})
|
||||
_ = h.Redis.Publish(context.Background(), fmt.Sprintf("room:%s:messages", msg.Room), payload)
|
||||
}
|
||||
}
|
||||
|
||||
// PushChatHistory sends the last N messages for a room via WebSocket.
|
||||
func (h *Hub) PushChatHistory(conn *websocket.Conn, room string, limit int) {
|
||||
if h.DB == nil || room == "" {
|
||||
return
|
||||
}
|
||||
var messages []db.Message
|
||||
h.DB.Where("room = ?", room).Order("timestamp desc").Limit(limit).Find(&messages)
|
||||
// Reverse to chronological order
|
||||
for i := len(messages)/2 - 1; i >= 0; i-- {
|
||||
opp := len(messages) - 1 - i
|
||||
messages[i], messages[opp] = messages[opp], messages[i]
|
||||
}
|
||||
for _, msg := range messages {
|
||||
conn.WriteJSON(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// HandleConnection handles a new WebSocket connection.
|
||||
func (h *Hub) HandleConnection(w http.ResponseWriter, r *http.Request) {
|
||||
clientIP := r.Header.Get("X-Forwarded-For")
|
||||
if clientIP == "" {
|
||||
clientIP = r.RemoteAddr
|
||||
}
|
||||
|
||||
// IP-based connection rate limiting
|
||||
allowed, err := ratelimit.CheckWSConnRate(context.Background(), h.Redis, clientIP)
|
||||
if err != nil {
|
||||
log.Printf("WS rate limit check error: %v", err)
|
||||
}
|
||||
if !allowed {
|
||||
http.Error(w, "too many connections from this IP", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
defer ratelimit.DecrWSConn(context.Background(), h.Redis, clientIP)
|
||||
|
||||
conn, err := Upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Println("Upgrade error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
username := r.URL.Query().Get("username")
|
||||
room := r.URL.Query().Get("room")
|
||||
if username == "" {
|
||||
username = fmt.Sprintf("anon_%d", time.Now().UnixNano()%10000)
|
||||
}
|
||||
|
||||
h.mu.Lock()
|
||||
h.clients[conn] = true
|
||||
h.clientMeta[conn] = ClientInfo{Username: username, Room: room}
|
||||
h.mu.Unlock()
|
||||
|
||||
// Announce presence (ADR-007)
|
||||
if h.Redis != nil && room != "" {
|
||||
_ = h.Redis.Set(context.Background(), fmt.Sprintf("presence:%s:%s", room, username), "online", 60*time.Second)
|
||||
h.Broadcast(db.Message{Room: room, Username: "system", Text: username + " joined " + room, Type: "system", Timestamp: time.Now()})
|
||||
}
|
||||
|
||||
// Push chat history on join (last 50 messages)
|
||||
h.PushChatHistory(conn, room, 50)
|
||||
|
||||
// Heartbeat goroutine: refresh presence TTL every 30s
|
||||
heartbeatStop := make(chan struct{})
|
||||
go func() {
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if h.Redis != nil && room != "" {
|
||||
_ = h.Redis.Set(context.Background(), fmt.Sprintf("presence:%s:%s", room, username), "online", 60*time.Second)
|
||||
}
|
||||
// Ping to detect dead connections
|
||||
if err := conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||
return
|
||||
}
|
||||
case <-heartbeatStop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
close(heartbeatStop)
|
||||
h.mu.Lock()
|
||||
delete(h.clients, conn)
|
||||
delete(h.clientMeta, conn)
|
||||
h.mu.Unlock()
|
||||
if h.Redis != nil && room != "" {
|
||||
_ = h.Redis.Del(context.Background(), fmt.Sprintf("presence:%s:%s", room, username))
|
||||
h.Broadcast(db.Message{Room: room, Username: "system", Text: username + " left " + room, Type: "system", Timestamp: time.Now()})
|
||||
}
|
||||
conn.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
var msg db.Message
|
||||
if err := conn.ReadJSON(&msg); err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
msg.Timestamp = time.Now()
|
||||
msg.Username = username // enforce from query param for security
|
||||
|
||||
// Check if banned before processing
|
||||
if h.Redis != nil {
|
||||
if isBanned, _ := h.Redis.Exists(context.Background(), fmt.Sprintf("banned:%s", username)); isBanned > 0 {
|
||||
conn.WriteJSON(db.Message{Type: "system", Text: "You are banned from chat.", Timestamp: time.Now()})
|
||||
continue
|
||||
}
|
||||
if isMuted, _ := h.Redis.Exists(context.Background(), fmt.Sprintf("muted:%s", username)); isMuted > 0 {
|
||||
conn.WriteJSON(db.Message{Type: "system", Text: "You are muted.", Timestamp: time.Now()})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Rate limiting check
|
||||
allowed, _ := ratelimit.CheckMessageRate(context.Background(), h.Redis, msg.Username, h.RateLimitMax, h.RateLimitWindow)
|
||||
if !allowed {
|
||||
conn.WriteJSON(db.Message{Type: "system", Text: "Rate limit exceeded. Please slow down.", Timestamp: time.Now()})
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle moderation commands
|
||||
if strings.HasPrefix(msg.Text, "/mod ") || strings.HasPrefix(msg.Text, "/mute ") || strings.HasPrefix(msg.Text, "/ban ") {
|
||||
h.handleModerationCommand(msg, conn)
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle private DMs
|
||||
if strings.HasPrefix(msg.Room, "dm:") {
|
||||
h.handlePrivateDM(msg)
|
||||
continue
|
||||
}
|
||||
|
||||
msg.Type = "public"
|
||||
log.Printf("Chat message in room %s from %s: %s", msg.Room, msg.Username, msg.Text)
|
||||
h.Broadcast(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// handlePrivateDM routes private messages to the specific user pair.
|
||||
func (h *Hub) handlePrivateDM(msg db.Message) {
|
||||
msg.Type = "private"
|
||||
h.DMsSent++
|
||||
|
||||
// Persist for history
|
||||
if h.DB != nil {
|
||||
h.DB.Create(&msg)
|
||||
}
|
||||
|
||||
// Deliver to target if online
|
||||
h.mu.Lock()
|
||||
targetOnline := false
|
||||
for conn, info := range h.clientMeta {
|
||||
if info.Username == msg.Room || strings.Contains(msg.Room, info.Username) {
|
||||
if err := conn.WriteJSON(msg); err == nil {
|
||||
targetOnline = true
|
||||
}
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
if !targetOnline {
|
||||
log.Printf("DM stored for offline user: %s -> room %s", msg.Username, msg.Room)
|
||||
}
|
||||
|
||||
// Also deliver to sender for confirmation
|
||||
h.mu.Lock()
|
||||
for conn, info := range h.clientMeta {
|
||||
if info.Username == msg.Username {
|
||||
conn.WriteJSON(msg)
|
||||
break
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
log.Printf("Private DM from %s to room %s: %s", msg.Username, msg.Room, msg.Text)
|
||||
}
|
||||
|
||||
// logModerationAction logs an action to the immutable ModerationLog table.
|
||||
func (h *Hub) logModerationAction(action, target, moderator, reason string) {
|
||||
logEntry := db.ModerationLog{
|
||||
Action: action,
|
||||
Target: target,
|
||||
Moderator: moderator,
|
||||
Reason: reason,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
if h.DB != nil {
|
||||
h.DB.Create(&logEntry)
|
||||
}
|
||||
log.Printf("Moderation action logged: %s %s by %s - %s", action, target, moderator, reason)
|
||||
}
|
||||
|
||||
// handleModerationCommand processes /mod, /mute, /ban commands.
|
||||
func (h *Hub) handleModerationCommand(msg db.Message, conn *websocket.Conn) {
|
||||
parts := strings.Fields(msg.Text)
|
||||
if len(parts) < 3 {
|
||||
conn.WriteJSON(db.Message{Type: "system", Text: "Usage: /mute <user> <duration_m> <reason> or /ban <user> <reason>", Timestamp: time.Now()})
|
||||
return
|
||||
}
|
||||
|
||||
if msg.Username != h.AdminUsername {
|
||||
conn.WriteJSON(db.Message{Type: "system", Text: "Only moderators can use moderation commands.", Timestamp: time.Now()})
|
||||
return
|
||||
}
|
||||
|
||||
action := strings.TrimPrefix(parts[0], "/")
|
||||
target := parts[1]
|
||||
reason := strings.Join(parts[2:], " ")
|
||||
|
||||
if h.Redis != nil {
|
||||
switch action {
|
||||
case "mute", "mod":
|
||||
durationMinutes := 10 // default
|
||||
if len(parts) >= 3 {
|
||||
if d, err := strconv.Atoi(parts[2]); err == nil && d > 0 {
|
||||
durationMinutes = d
|
||||
if len(parts) > 3 {
|
||||
reason = strings.Join(parts[3:], " ")
|
||||
} else {
|
||||
reason = "no reason given"
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = h.Redis.Set(context.Background(), fmt.Sprintf("muted:%s", target), "1", time.Duration(durationMinutes)*time.Minute)
|
||||
case "ban":
|
||||
_ = h.Redis.Set(context.Background(), fmt.Sprintf("banned:%s", target), "1", 0) // no expiry
|
||||
}
|
||||
}
|
||||
|
||||
h.logModerationAction(action, target, msg.Username, reason)
|
||||
|
||||
h.Broadcast(db.Message{
|
||||
Room: msg.Room,
|
||||
Username: "system",
|
||||
Type: "system",
|
||||
Text: fmt.Sprintf("%s was %s by %s: %s", target, action, msg.Username, reason),
|
||||
Timestamp: time.Now(),
|
||||
})
|
||||
}
|
||||
|
||||
// SubscribeRoomMessages listens for Redis pub/sub messages and fans out locally.
|
||||
func (h *Hub) SubscribeRoomMessages() {
|
||||
if h.Redis == nil {
|
||||
return
|
||||
}
|
||||
pubsub := h.Redis.PSubscribe(context.Background(), "room:*:messages")
|
||||
defer pubsub.Close()
|
||||
|
||||
log.Printf("Redis pub/sub subscriber started (instance %s)", h.InstanceID)
|
||||
|
||||
for {
|
||||
msg, err := pubsub.ReceiveMessage(context.Background())
|
||||
if err != nil {
|
||||
log.Printf("Redis pub/sub error: %v — retrying in 5s", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
pubsub = h.Redis.PSubscribe(context.Background(), "room:*:messages")
|
||||
continue
|
||||
}
|
||||
|
||||
var envelope struct {
|
||||
Instance string `json:"instance"`
|
||||
Msg db.Message `json:"msg"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(msg.Payload), &envelope); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip messages originating from this instance
|
||||
if envelope.Instance == h.InstanceID {
|
||||
continue
|
||||
}
|
||||
|
||||
// Fan out to local clients
|
||||
h.mu.Lock()
|
||||
for client := range h.clients {
|
||||
client.WriteJSON(envelope.Msg)
|
||||
}
|
||||
h.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// ScanPresence returns all active presence entries from Redis.
|
||||
func (h *Hub) ScanPresence(ctx context.Context) ([]db.Presence, error) {
|
||||
if h.Redis == nil {
|
||||
return nil, nil
|
||||
}
|
||||
keys, _, err := h.Redis.Scan(ctx, 0, "presence:*", 200)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var presences []db.Presence
|
||||
for _, key := range keys {
|
||||
parts := strings.SplitN(key, ":", 3)
|
||||
if len(parts) != 3 {
|
||||
continue
|
||||
}
|
||||
presences = append(presences, db.Presence{
|
||||
Room: parts[1],
|
||||
Username: parts[2],
|
||||
Online: true,
|
||||
})
|
||||
}
|
||||
return presences, nil
|
||||
}
|
||||
|
||||
// MuteUser mutes a user for a duration.
|
||||
func (h *Hub) MuteUser(ctx context.Context, username string, duration time.Duration) error {
|
||||
if h.Redis == nil {
|
||||
return fmt.Errorf("redis unavailable")
|
||||
}
|
||||
return h.Redis.Set(ctx, fmt.Sprintf("muted:%s", username), "1", duration)
|
||||
}
|
||||
|
||||
// UnmuteUser removes a mute.
|
||||
func (h *Hub) UnmuteUser(ctx context.Context, username string) error {
|
||||
if h.Redis == nil {
|
||||
return fmt.Errorf("redis unavailable")
|
||||
}
|
||||
return h.Redis.Del(ctx, fmt.Sprintf("muted:%s", username))
|
||||
}
|
||||
|
||||
// BanUser bans a user permanently.
|
||||
func (h *Hub) BanUser(ctx context.Context, username string) error {
|
||||
if h.Redis == nil {
|
||||
return fmt.Errorf("redis unavailable")
|
||||
}
|
||||
return h.Redis.Set(ctx, fmt.Sprintf("banned:%s", username), "1", 0)
|
||||
}
|
||||
|
||||
// UnbanUser removes a ban.
|
||||
func (h *Hub) UnbanUser(ctx context.Context, username string) error {
|
||||
if h.Redis == nil {
|
||||
return fmt.Errorf("redis unavailable")
|
||||
}
|
||||
return h.Redis.Del(ctx, fmt.Sprintf("banned:%s", username))
|
||||
}
|
||||
@@ -0,0 +1,563 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/gorilla/websocket"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"chat/src/db"
|
||||
)
|
||||
|
||||
// mockRedis implements db.RedisClient for testing.
|
||||
type mockRedis struct {
|
||||
mu sync.Mutex
|
||||
store map[string]string
|
||||
pubsubs []*mockPubSub
|
||||
}
|
||||
|
||||
func newMockRedis() *mockRedis {
|
||||
return &mockRedis{store: make(map[string]string)}
|
||||
}
|
||||
|
||||
func (m *mockRedis) Ping(ctx context.Context) error { return nil }
|
||||
func (m *mockRedis) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.store[key] = value.(string)
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) Get(ctx context.Context, key string) (string, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.store[key], nil
|
||||
}
|
||||
func (m *mockRedis) Del(ctx context.Context, keys ...string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, k := range keys {
|
||||
delete(m.store, k)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) Incr(ctx context.Context, key string) (int64, error) { return 0, nil }
|
||||
func (m *mockRedis) Decr(ctx context.Context, key string) (int64, error) { return 0, nil }
|
||||
func (m *mockRedis) Expire(ctx context.Context, key string, expiration time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) Exists(ctx context.Context, keys ...string) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, k := range keys {
|
||||
if _, ok := m.store[k]; ok {
|
||||
return 1, nil
|
||||
}
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
func (m *mockRedis) Publish(ctx context.Context, channel string, message interface{}) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
payload, _ := json.Marshal(message)
|
||||
for _, ps := range m.pubsubs {
|
||||
ps.messages <- &db.PubSubMessage{Channel: channel, Payload: string(payload)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *mockRedis) PSubscribe(ctx context.Context, channels ...string) db.PubSub {
|
||||
ps := &mockPubSub{messages: make(chan *db.PubSubMessage, 100)}
|
||||
m.mu.Lock()
|
||||
m.pubsubs = append(m.pubsubs, ps)
|
||||
m.mu.Unlock()
|
||||
return ps
|
||||
}
|
||||
func (m *mockRedis) Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
var keys []string
|
||||
for k := range m.store {
|
||||
if strings.Contains(k, strings.TrimSuffix(match, "*")) {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
return keys, 0, nil
|
||||
}
|
||||
func (m *mockRedis) Info(ctx context.Context, sections ...string) (string, error) { return "", nil }
|
||||
func (m *mockRedis) Close() error { return nil }
|
||||
|
||||
type mockPubSub struct {
|
||||
messages chan *db.PubSubMessage
|
||||
}
|
||||
|
||||
func (m *mockPubSub) ReceiveMessage(ctx context.Context) (*db.PubSubMessage, error) {
|
||||
select {
|
||||
case msg := <-m.messages:
|
||||
return msg, nil
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
func (m *mockPubSub) Close() error { return nil }
|
||||
|
||||
func setupTestHub(t *testing.T) (*Hub, *gorm.DB, *mockRedis) {
|
||||
t.Helper()
|
||||
gormDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open test DB: %v", err)
|
||||
}
|
||||
gormDB.AutoMigrate(&db.Message{}, &db.ModerationLog{})
|
||||
mr := newMockRedis()
|
||||
hub := NewHub(gormDB, mr, "test-instance", 10, 10*time.Second, "admin")
|
||||
return hub, gormDB, mr
|
||||
}
|
||||
|
||||
func setupWSServer(hub *Hub) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(hub.HandleConnection))
|
||||
}
|
||||
|
||||
// wsClient wraps a websocket.Conn with a channel-based read helper.
|
||||
type wsClient struct {
|
||||
conn *websocket.Conn
|
||||
msgs chan db.Message
|
||||
errors chan error
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func newWSClient(t *testing.T, url string) *wsClient {
|
||||
t.Helper()
|
||||
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Dial error: %v", err)
|
||||
}
|
||||
wc := &wsClient{
|
||||
conn: conn,
|
||||
msgs: make(chan db.Message, 100),
|
||||
errors: make(chan error, 1),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
go wc.readLoop()
|
||||
return wc
|
||||
}
|
||||
|
||||
func (wc *wsClient) readLoop() {
|
||||
defer close(wc.msgs)
|
||||
for {
|
||||
var msg db.Message
|
||||
if err := wc.conn.ReadJSON(&msg); err != nil {
|
||||
wc.errors <- err
|
||||
return
|
||||
}
|
||||
select {
|
||||
case wc.msgs <- msg:
|
||||
case <-wc.done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *wsClient) close() {
|
||||
close(wc.done)
|
||||
wc.conn.Close()
|
||||
}
|
||||
|
||||
// recvMsg reads the next message with a timeout.
|
||||
func (wc *wsClient) recvMsg(timeout time.Duration) (*db.Message, error) {
|
||||
select {
|
||||
case msg := <-wc.msgs:
|
||||
return &msg, nil
|
||||
case err := <-wc.errors:
|
||||
return nil, err
|
||||
case <-time.After(timeout):
|
||||
return nil, context.DeadlineExceeded
|
||||
}
|
||||
}
|
||||
|
||||
// skipSystem reads and discards system messages.
|
||||
func (wc *wsClient) skipSystem(timeout time.Duration) {
|
||||
for {
|
||||
msg, err := wc.recvMsg(timeout)
|
||||
if err != nil || msg.Type != "system" {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *wsClient) writeJSON(v interface{}) error {
|
||||
return wc.conn.WriteJSON(v)
|
||||
}
|
||||
|
||||
func TestHub_Broadcast(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
|
||||
msg := db.Message{
|
||||
Room: "main",
|
||||
Username: "user1",
|
||||
Text: "hello",
|
||||
Timestamp: time.Now(),
|
||||
Type: "public",
|
||||
}
|
||||
hub.Broadcast(msg)
|
||||
|
||||
var count int64
|
||||
hub.DB.Model(&db.Message{}).Count(&count)
|
||||
if count != 1 {
|
||||
t.Errorf("message count = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_ConnectedClients(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
if hub.ConnectedClients() != 0 {
|
||||
t.Errorf("ConnectedClients() = %d, want 0", hub.ConnectedClients())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_WebSocketConnection(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=testuser&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
defer wc.close()
|
||||
|
||||
msg, err := wc.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("recvMsg error: %v", err)
|
||||
}
|
||||
if !strings.Contains(msg.Text, "joined") {
|
||||
t.Errorf("expected join message, got text=%s", msg.Text)
|
||||
}
|
||||
|
||||
if hub.ConnectedClients() != 1 {
|
||||
t.Errorf("ConnectedClients() = %d, want 1", hub.ConnectedClients())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_SendMessage(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=testuser&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
defer wc.close()
|
||||
|
||||
wc.skipSystem(2 * time.Second)
|
||||
|
||||
if err := wc.writeJSON(db.Message{Room: "main", Text: "hello world"}); err != nil {
|
||||
t.Fatalf("writeJSON error: %v", err)
|
||||
}
|
||||
|
||||
recvMsg, err := wc.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("recvMsg error: %v", err)
|
||||
}
|
||||
if recvMsg.Text != "hello world" {
|
||||
t.Errorf("recvMsg.Text = %q, want hello world", recvMsg.Text)
|
||||
}
|
||||
if recvMsg.Username != "testuser" {
|
||||
t.Errorf("recvMsg.Username = %q, want testuser", recvMsg.Username)
|
||||
}
|
||||
if recvMsg.Type != "public" {
|
||||
t.Errorf("recvMsg.Type = %q, want public", recvMsg.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_Disconnect(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=testuser&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
wc.skipSystem(2 * time.Second)
|
||||
|
||||
if hub.ConnectedClients() != 1 {
|
||||
t.Errorf("ConnectedClients() = %d, want 1", hub.ConnectedClients())
|
||||
}
|
||||
|
||||
wc.close()
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
if hub.ConnectedClients() != 0 {
|
||||
t.Errorf("ConnectedClients() after close = %d, want 0", hub.ConnectedClients())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_BannedUser(t *testing.T) {
|
||||
hub, _, mr := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
mr.Set(context.Background(), "banned:testuser", "1", 0)
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=testuser&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
defer wc.close()
|
||||
|
||||
wc.skipSystem(2 * time.Second)
|
||||
wc.writeJSON(db.Message{Room: "main", Text: "hello"})
|
||||
|
||||
recvMsg, err := wc.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("recvMsg error: %v", err)
|
||||
}
|
||||
if !strings.Contains(recvMsg.Text, "banned") {
|
||||
t.Errorf("expected banned message, got %q", recvMsg.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_MutedUser(t *testing.T) {
|
||||
hub, _, mr := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
mr.Set(context.Background(), "muted:testuser", "1", 0)
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=testuser&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
defer wc.close()
|
||||
|
||||
wc.skipSystem(2 * time.Second)
|
||||
wc.writeJSON(db.Message{Room: "main", Text: "hello"})
|
||||
|
||||
recvMsg, err := wc.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("recvMsg error: %v", err)
|
||||
}
|
||||
if !strings.Contains(recvMsg.Text, "muted") {
|
||||
t.Errorf("expected muted message, got %q", recvMsg.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_ModerationCommand(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=admin&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
defer wc.close()
|
||||
|
||||
wc.skipSystem(2 * time.Second)
|
||||
wc.writeJSON(db.Message{Room: "main", Text: "/mute baduser 5 spamming"})
|
||||
|
||||
recvMsg, err := wc.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("recvMsg error: %v", err)
|
||||
}
|
||||
if !strings.Contains(recvMsg.Text, "mute") {
|
||||
t.Errorf("expected mute broadcast, got text=%s", recvMsg.Text)
|
||||
}
|
||||
|
||||
var count int64
|
||||
hub.DB.Model(&db.ModerationLog{}).Count(&count)
|
||||
if count != 1 {
|
||||
t.Errorf("moderation log count = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_NonAdminModerationCommand(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=regularuser&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
defer wc.close()
|
||||
|
||||
wc.skipSystem(2 * time.Second)
|
||||
wc.writeJSON(db.Message{Room: "main", Text: "/mute baduser 5 spamming"})
|
||||
|
||||
recvMsg, err := wc.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("recvMsg error: %v", err)
|
||||
}
|
||||
if !strings.Contains(recvMsg.Text, "Only moderators") {
|
||||
t.Errorf("expected moderator-only message, got %q", recvMsg.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_PrivateDM(t *testing.T) {
|
||||
hub, _, _ := setupTestHub(t)
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http")
|
||||
wc1 := newWSClient(t, wsURL+"?username=alice&room=main")
|
||||
defer wc1.close()
|
||||
wc2 := newWSClient(t, wsURL+"?username=bob&room=main")
|
||||
defer wc2.close()
|
||||
|
||||
wc1.skipSystem(2 * time.Second)
|
||||
wc2.skipSystem(2 * time.Second)
|
||||
|
||||
wc1.writeJSON(db.Message{Room: "dm:bob", Text: "hey bob"})
|
||||
|
||||
recvMsg, err := wc2.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("bob recvMsg error: %v", err)
|
||||
}
|
||||
if recvMsg.Type != "private" {
|
||||
t.Errorf("expected private type, got %s", recvMsg.Type)
|
||||
}
|
||||
if recvMsg.Text != "hey bob" {
|
||||
t.Errorf("expected 'hey bob', got %q", recvMsg.Text)
|
||||
}
|
||||
|
||||
recvMsg2, err := wc1.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("alice recvMsg error: %v", err)
|
||||
}
|
||||
if recvMsg2.Type != "private" {
|
||||
t.Errorf("expected private confirmation, got %s", recvMsg2.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_PushChatHistory(t *testing.T) {
|
||||
hub, gormDB, _ := setupTestHub(t)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
gormDB.Create(&db.Message{
|
||||
Room: "main",
|
||||
Username: "user1",
|
||||
Text: "msg",
|
||||
Timestamp: time.Now().Add(time.Duration(i) * time.Second),
|
||||
Type: "public",
|
||||
})
|
||||
}
|
||||
|
||||
srv := setupWSServer(hub)
|
||||
defer srv.Close()
|
||||
|
||||
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?username=newuser&room=main"
|
||||
wc := newWSClient(t, wsURL)
|
||||
defer wc.close()
|
||||
|
||||
count := 0
|
||||
for i := 0; i < 10; i++ {
|
||||
msg, err := wc.recvMsg(2 * time.Second)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if msg.Type == "public" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 5 {
|
||||
t.Errorf("history message count = %d, want 5", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_MuteUser_UnmuteUser(t *testing.T) {
|
||||
hub, _, mr := setupTestHub(t)
|
||||
ctx := context.Background()
|
||||
|
||||
err := hub.MuteUser(ctx, "user1", 10*time.Minute)
|
||||
if err != nil {
|
||||
t.Fatalf("MuteUser error = %v", err)
|
||||
}
|
||||
|
||||
exists, _ := mr.Exists(ctx, "muted:user1")
|
||||
if exists != 1 {
|
||||
t.Error("muted key not found after MuteUser")
|
||||
}
|
||||
|
||||
err = hub.UnmuteUser(ctx, "user1")
|
||||
if err != nil {
|
||||
t.Fatalf("UnmuteUser error = %v", err)
|
||||
}
|
||||
|
||||
exists, _ = mr.Exists(ctx, "muted:user1")
|
||||
if exists != 0 {
|
||||
t.Error("muted key still exists after UnmuteUser")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_BanUser_UnbanUser(t *testing.T) {
|
||||
hub, _, mr := setupTestHub(t)
|
||||
ctx := context.Background()
|
||||
|
||||
err := hub.BanUser(ctx, "user1")
|
||||
if err != nil {
|
||||
t.Fatalf("BanUser error = %v", err)
|
||||
}
|
||||
|
||||
exists, _ := mr.Exists(ctx, "banned:user1")
|
||||
if exists != 1 {
|
||||
t.Error("banned key not found after BanUser")
|
||||
}
|
||||
|
||||
err = hub.UnbanUser(ctx, "user1")
|
||||
if err != nil {
|
||||
t.Fatalf("UnbanUser error = %v", err)
|
||||
}
|
||||
|
||||
exists, _ = mr.Exists(ctx, "banned:user1")
|
||||
if exists != 0 {
|
||||
t.Error("banned key still exists after UnbanUser")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_ScanPresence(t *testing.T) {
|
||||
hub, _, mr := setupTestHub(t)
|
||||
ctx := context.Background()
|
||||
|
||||
mr.Set(ctx, "presence:main:user1", "online", 0)
|
||||
mr.Set(ctx, "presence:main:user2", "online", 0)
|
||||
mr.Set(ctx, "presence:lobby:user3", "online", 0)
|
||||
|
||||
presences, err := hub.ScanPresence(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ScanPresence error = %v", err)
|
||||
}
|
||||
if len(presences) != 3 {
|
||||
t.Errorf("ScanPresence len = %d, want 3", len(presences))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHub_SubscribeRoomMessages(t *testing.T) {
|
||||
hub, _, mr := setupTestHub(t)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
hub.SubscribeRoomMessages()
|
||||
}()
|
||||
|
||||
payload, _ := json.Marshal(map[string]any{
|
||||
"instance": "other-instance",
|
||||
"msg": db.Message{
|
||||
Room: "main",
|
||||
Username: "remoteuser",
|
||||
Text: "remote hello",
|
||||
Timestamp: time.Now(),
|
||||
Type: "public",
|
||||
},
|
||||
})
|
||||
mr.Publish(ctx, "room:main:messages", string(payload))
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
ownPayload, _ := json.Marshal(map[string]any{
|
||||
"instance": "test-instance",
|
||||
"msg": db.Message{
|
||||
Room: "main",
|
||||
Username: "localuser",
|
||||
Text: "local hello",
|
||||
Timestamp: time.Now(),
|
||||
Type: "public",
|
||||
},
|
||||
})
|
||||
mr.Publish(ctx, "room:main:messages", string(ownPayload))
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
@@ -18,14 +18,15 @@ RUN apk add --no-cache \
|
||||
bash
|
||||
# Set working directory for playout
|
||||
WORKDIR /app
|
||||
# Create directories for playlist, media, HLS output, and emergency slate
|
||||
RUN mkdir -p /app/hls /app/media /app/playlists /app/slate
|
||||
# Copy the playout start script and the built playlist manager binary
|
||||
COPY start-playout.sh /app/
|
||||
# Copy demo data for out-of-the-box playout
|
||||
COPY demodata/ /app/
|
||||
# Copy the built playlist manager binary from the builder stage
|
||||
COPY --from=builder /playlist-manager /app/playlist-manager
|
||||
# Create directories for playlist, media, and HLS output
|
||||
RUN mkdir -p /app/hls
|
||||
# Set execute permissions for the start script and playlist manager binary
|
||||
RUN chmod +x /app/start-playout.sh /app/playlist-manager
|
||||
# Expose HLS directory (will be served by nginx)
|
||||
VOLUME ["/app/hls"]
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
module playout
|
||||
|
||||
go 1.26.5
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.43.4
|
||||
github.com/aws/aws-sdk-go-v2/config v1.32.35
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.107.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.16 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.34 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.35 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.35 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.35 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.36 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.28 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.35 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.36 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.5.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.33.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.38.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.45.4 // indirect
|
||||
github.com/aws/smithy-go v1.27.6 // indirect
|
||||
)
|
||||
@@ -0,0 +1,36 @@
|
||||
github.com/aws/aws-sdk-go-v2 v1.43.4 h1:b9FTvbRwy+JCsfp2Wp6wV/KbOx3Aj7nkoFb2cRX0IhE=
|
||||
github.com/aws/aws-sdk-go-v2 v1.43.4/go.mod h1:70vwSy16txshwG+g55WkpgPKDIByzHI8ccBsOteo3bQ=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.16 h1:aiuaKlDweRC5qExJondpWjOgyzMHpofpwspGXUtwn4c=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.16/go.mod h1:nG/LOlmox9BDe9HvQnXWzgcK8uKbgBMZ/Hp5pVt/21I=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.32.35 h1:UEzXuET8E42lxBPijuACu/tEK7v5lFPlk0Q+GT5WD9E=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.32.35/go.mod h1:KaMtJpFa2JlL2BStjjHQVwQpzZEmw+ND/EgVrfFoo2g=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.34 h1:y6GkSmcv5myd1ngrYbGmiLlwQqB6TQhOuN/tbSSuWDY=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.34/go.mod h1:w3dTcnDVoQIewjo7JG45hduAToikiIFLC4FIO7fndvw=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.35 h1:+S7kbJoLDDQ5tE+lHrUBgMkzC8NLgsaioS2F3dVoFAE=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.35/go.mod h1:Ak7xXviIARfFdNUJ9Etb0bdVDt/KAvKjMGJVLWXDzik=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.35 h1:kzVuGlatQtYinwBJEEyLAbggepCoavosiaHHX9+fD+c=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.35/go.mod h1:0yLx0yEI+SfqeJMPvOtIEFoZbiQYXMGszBueiutQyaI=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.35 h1:WK6CjihTuLisCjSKKbildJ79sGZZgbBz3iNa7VsKIhU=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.35/go.mod h1:KYleN57luLoe97R7vTnx8PMcVrr9gAcRECtOjl91DNg=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.36 h1:jbGY4CXLzZElOXgGsexlC3Hi+3YM0rSmk4opFXKqg/k=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.36/go.mod h1:uBu/9aKsS/UQGc72RAt3y54kjgYQxmhut8ZD2dXCDNE=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.15 h1:JJLBQxwY+AFwuPAi5ivGc1ChnTdUt4cXMv7e76m2c/Y=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.15/go.mod h1:lQknBIe78MVL0cQOQDlag8KGflMbMEVFx9mB6O8ENvk=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.28 h1:Q1TF1J9jVD+vFo0LzNnmNdQ9EAt52TS+MQlq9Ir+Yxo=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.28/go.mod h1:4KqXXC/p1hrotmouDFbrRoWaLy962b9PMUReCG6+uWo=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.35 h1:BBEElKh4a+rKshvjrfpajTe9CbpZvrbb4Jkg2PB7RzA=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.35/go.mod h1:zaZk983w//8beSruBVec/mr4CmDwgZitW/qzGhAAX0g=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.36 h1:EUIwBoN+q7UmhAejxgD27APiRjh1vwCFo53gSqdT0BM=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.36/go.mod h1:6u00gmlTGR6W0b2k9NBrld7MnOEmf1Spqx0VVt6AqyE=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.107.0 h1:OkYV+1171za+ab9otU1tGxMXhx6uZvwVEtVddjLuYTg=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.107.0/go.mod h1:5FTZoQxhmLEiCAtYVk6V+t0iS/B5yGZVLZ3Wq5FDJZI=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.5.4 h1:cOJELVNrq5Q3Udry2GLuHUM7MhwpeaQRdYaoa6GI/yI=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.5.4/go.mod h1:f4LxzKBtaTxD7xh3PiVg3CE1tchQemfmghaJr+NbK2c=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.33.4 h1:AMW7a7S8iQaHjBYZdU3PCq4GKRPijTPRAc7e6XtEThY=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.33.4/go.mod h1:QQNsFV1DVXoXcZt18FS8lI8rtUrlDyAuWZLQ5shunv4=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.38.4 h1:AsbZcJAQPRmHDJG8K1N0pof/1zPWjVT8TFlTWuGLSvo=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.38.4/go.mod h1:6imqztH0//t0mKbl6yWl7swSEl7F/w32oAmqB3vP1ag=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.45.4 h1:w/AryDYMjSUANSQ2uoZxJovUsMTwWJNTv3IMex30Y+4=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.45.4/go.mod h1:WeBiAa67azG7Su9Vf+ChGDBLiAozJCXzdjXiPBUwtbc=
|
||||
github.com/aws/smithy-go v1.27.6 h1:0zjT8jgK3jbrTT7JJ3EE6JsMhX8JTrZ+f1sEndYDXrA=
|
||||
github.com/aws/smithy-go v1.27.6/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
|
||||
+282
-15
@@ -3,11 +3,13 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -25,8 +27,18 @@ type VideoFile struct {
|
||||
}
|
||||
|
||||
var (
|
||||
r2Client *s3.Client
|
||||
bucket = os.Getenv("R2_BUCKET_NAME")
|
||||
r2Client *s3.Client
|
||||
bucket = os.Getenv("R2_BUCKET_NAME")
|
||||
playoutMode = "starting" // "live", "slate", "degraded"
|
||||
restartCount = 0
|
||||
restartWindow = time.Now()
|
||||
slateDir = "/app/slate"
|
||||
hlsDir = "/app/hls"
|
||||
)
|
||||
|
||||
const (
|
||||
maxRestartsPerMinute = 5
|
||||
slateManifestName = "playlist.m3u8"
|
||||
)
|
||||
|
||||
// initR2Client initializes the R2 (S3-compatible) client
|
||||
@@ -79,23 +91,181 @@ func LoadPlaylist(playlistPath string) ([]VideoFile, error) {
|
||||
// ValidateVideo uses ffprobe to enforce codec, fps, and keyframe interval (ADR-001)
|
||||
func ValidateVideo(videoPath string) (VideoFile, error) {
|
||||
v := VideoFile{Path: videoPath}
|
||||
var errors []string
|
||||
|
||||
// Run ffprobe
|
||||
cmd := exec.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", videoPath)
|
||||
_, err := cmd.Output()
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
v.Validation = "ffprobe failed: " + err.Error()
|
||||
return v, err
|
||||
return v, fmt.Errorf("ffprobe: %w", err)
|
||||
}
|
||||
|
||||
// TODO: Parse JSON for strict checks (codec_name=h264, r_frame_rate=30/1, key_frame interval)
|
||||
// For MVP we assume valid if ffprobe succeeds
|
||||
v.Valid = true
|
||||
v.Validation = "Validation passed (H.264/AVC, AAC, 30fps constant, 2s keyframes - full JSON parse TODO per ADR-001)"
|
||||
v.Duration = 60.0 // placeholder from ffprobe
|
||||
// Parse JSON
|
||||
var probe struct {
|
||||
Streams []struct {
|
||||
CodecName string `json:"codec_name"`
|
||||
CodecType string `json:"codec_type"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
RFrameRate string `json:"r_frame_rate"`
|
||||
PixFmt string `json:"pix_fmt"`
|
||||
SampleRate string `json:"sample_rate"`
|
||||
Channels int `json:"channels"`
|
||||
Duration string `json:"duration"`
|
||||
} `json:"streams"`
|
||||
Format struct {
|
||||
Duration string `json:"duration"`
|
||||
} `json:"format"`
|
||||
}
|
||||
if err := json.Unmarshal(output, &probe); err != nil {
|
||||
v.Validation = "ffprobe JSON parse failed: " + err.Error()
|
||||
return v, fmt.Errorf("ffprobe json: %w", err)
|
||||
}
|
||||
|
||||
if len(probe.Streams) == 0 {
|
||||
errors = append(errors, "no streams detected")
|
||||
}
|
||||
|
||||
var hasVideo, hasAudio bool
|
||||
for _, s := range probe.Streams {
|
||||
switch s.CodecType {
|
||||
case "video":
|
||||
hasVideo = true
|
||||
if s.CodecName != "h264" {
|
||||
errors = append(errors, fmt.Sprintf("video codec must be h264, got %s", s.CodecName))
|
||||
}
|
||||
if s.RFrameRate != "30/1" {
|
||||
errors = append(errors, fmt.Sprintf("framerate must be 30/1, got %s", s.RFrameRate))
|
||||
}
|
||||
if s.Width != 1280 && s.Width != 1920 {
|
||||
errors = append(errors, fmt.Sprintf("width must be 1280 or 1920, got %d", s.Width))
|
||||
}
|
||||
if s.Height != 720 && s.Height != 1080 {
|
||||
errors = append(errors, fmt.Sprintf("height must be 720 or 1080, got %d", s.Height))
|
||||
}
|
||||
if s.PixFmt != "yuv420p" {
|
||||
errors = append(errors, fmt.Sprintf("pixel format must be yuv420p, got %s", s.PixFmt))
|
||||
}
|
||||
case "audio":
|
||||
hasAudio = true
|
||||
if s.CodecName != "aac" {
|
||||
errors = append(errors, fmt.Sprintf("audio codec must be aac, got %s", s.CodecName))
|
||||
}
|
||||
if s.SampleRate != "48000" {
|
||||
errors = append(errors, fmt.Sprintf("audio sample rate must be 48000, got %s", s.SampleRate))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !hasVideo {
|
||||
errors = append(errors, "no video stream found")
|
||||
}
|
||||
if !hasAudio {
|
||||
errors = append(errors, "no audio stream found")
|
||||
}
|
||||
|
||||
// Parse duration from format or stream
|
||||
durationSec := 0.0
|
||||
if probe.Format.Duration != "" {
|
||||
if d, err := strconv.ParseFloat(probe.Format.Duration, 64); err == nil {
|
||||
durationSec = d
|
||||
}
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
v.Valid = false
|
||||
v.Validation = strings.Join(errors, "; ")
|
||||
return v, fmt.Errorf("validation failed: %s", v.Validation)
|
||||
}
|
||||
|
||||
// Validate keyframe interval <=2s (60 frames at 30fps)
|
||||
keyframeErr := validateKeyframeInterval(videoPath)
|
||||
if keyframeErr != nil {
|
||||
errors = append(errors, keyframeErr.Error())
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
v.Valid = false
|
||||
v.Validation = strings.Join(errors, "; ")
|
||||
return v, fmt.Errorf("validation failed: %s", v.Validation)
|
||||
}
|
||||
|
||||
v.Valid = true
|
||||
v.Duration = durationSec
|
||||
v.Validation = fmt.Sprintf("OK: H.264/AAC, 30fps, yuv420p, 48kHz, %.1fs, keyframes OK", durationSec)
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// validateKeyframeInterval checks that max gap between keyframes <=2s (60 frames at 30fps).
|
||||
func validateKeyframeInterval(videoPath string) error {
|
||||
cmd := exec.Command("ffprobe",
|
||||
"-v", "quiet",
|
||||
"-select_streams", "v:0",
|
||||
"-show_frames",
|
||||
"-show_entries", "frame=key_frame,pkt_pts_time",
|
||||
"-of", "csv=p=0",
|
||||
videoPath)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("keyframe probe failed: %w", err)
|
||||
}
|
||||
|
||||
type kfEntry struct{ pts float64 }
|
||||
var keyframes []kfEntry
|
||||
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.Split(line, ",")
|
||||
if len(parts) < 2 || parts[0] != "1" {
|
||||
continue
|
||||
}
|
||||
pts, err := strconv.ParseFloat(strings.TrimSpace(parts[1]), 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
keyframes = append(keyframes, kfEntry{pts})
|
||||
}
|
||||
|
||||
if len(keyframes) < 2 {
|
||||
return fmt.Errorf("insufficient keyframes for interval analysis (need >=2, got %d)", len(keyframes))
|
||||
}
|
||||
|
||||
maxGap := 0.0
|
||||
for i := 1; i < len(keyframes); i++ {
|
||||
gap := keyframes[i].pts - keyframes[i-1].pts
|
||||
if gap > maxGap {
|
||||
maxGap = gap
|
||||
}
|
||||
}
|
||||
|
||||
if maxGap > 2.1 { // small tolerance for floating point
|
||||
return fmt.Errorf("keyframe interval too large: max %.2fs (must be <=2s)", maxGap)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateVideoStrict runs ValidateVideo and returns a structured JSON result.
|
||||
func ValidateVideoStrict(videoPath string) map[string]any {
|
||||
v, err := ValidateVideo(videoPath)
|
||||
result := map[string]any{
|
||||
"path": videoPath,
|
||||
"valid": v.Valid,
|
||||
"errors": []string{},
|
||||
}
|
||||
if err != nil {
|
||||
result["errors"] = strings.Split(v.Validation, "; ")
|
||||
}
|
||||
if v.Valid {
|
||||
result["duration_seconds"] = v.Duration
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ValidatePlaylist runs normalization validation on all entries
|
||||
func ValidatePlaylist(playlistPath string) ([]VideoFile, error) {
|
||||
videos, err := LoadPlaylist(playlistPath)
|
||||
@@ -139,8 +309,65 @@ func SyncToR2(localPath, r2Key string) error {
|
||||
// HealthHandler provides a simple health check endpoint
|
||||
func HealthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
status := "healthy"
|
||||
if playoutMode == "slate" {
|
||||
status = "degraded"
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, `{"status":"healthy","service":"playout","timestamp":"`+time.Now().UTC().Format(time.RFC3339)+`"}`)
|
||||
fmt.Fprintf(w, `{"status":"%s","service":"playout","mode":"%s","restarts":%d,"timestamp":"%s"}`,
|
||||
status, playoutMode, restartCount, time.Now().UTC().Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// activateSlate copies pre-rendered slate HLS into the live output directory.
|
||||
// Writes a static manifest referencing slate segments. Returns error if slate not found.
|
||||
func activateSlate() error {
|
||||
slateHLS := filepath.Join(slateDir, "hls")
|
||||
if _, err := os.Stat(filepath.Join(slateHLS, "slate.m3u8")); os.IsNotExist(err) {
|
||||
return fmt.Errorf("slate HLS not found at %s — run scripts/generate-slate.sh first", slateHLS)
|
||||
}
|
||||
|
||||
// Clear existing live segments
|
||||
if err := resetHLSOutput(hlsDir); err != nil {
|
||||
return fmt.Errorf("failed to clear HLS output for slate: %w", err)
|
||||
}
|
||||
|
||||
// Copy slate segments into live HLS dir
|
||||
slateFiles, err := filepath.Glob(filepath.Join(slateHLS, "*"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, src := range slateFiles {
|
||||
dst := filepath.Join(hlsDir, filepath.Base(src))
|
||||
data, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(dst, data, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
playoutMode = "slate"
|
||||
fmt.Printf("[%s] EMERGENCY: Switched to slate fallback. %d restarts in window.\n",
|
||||
time.Now().Format(time.RFC3339), restartCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
// deactivateSlate clears slate state and prepares for live playout resumption.
|
||||
func deactivateSlate() {
|
||||
playoutMode = "live"
|
||||
fmt.Printf("[%s] RECOVERY: Slate deactivated, resuming live playout.\n",
|
||||
time.Now().Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// shouldEscalateToSlate checks if restart count exceeds threshold within the window.
|
||||
func shouldEscalateToSlate() bool {
|
||||
if time.Since(restartWindow) > 60*time.Second {
|
||||
restartCount = 0
|
||||
restartWindow = time.Now()
|
||||
return false
|
||||
}
|
||||
return restartCount >= maxRestartsPerMinute
|
||||
}
|
||||
|
||||
func resetHLSOutput(hlsDir string) error {
|
||||
@@ -207,6 +434,21 @@ func main() {
|
||||
|
||||
// Start HTTP health check server
|
||||
http.HandleFunc("/health", HealthHandler)
|
||||
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
|
||||
fmt.Fprintln(w, "# HELP linearvoid_playout_restarts_total Total FFmpeg restarts.")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_playout_restarts_total counter")
|
||||
fmt.Fprintf(w, "linearvoid_playout_restarts_total %d\n", restartCount)
|
||||
fmt.Fprintln(w, "# HELP linearvoid_playout_mode Current playout mode (0=live, 1=slate, 2=degraded).")
|
||||
fmt.Fprintln(w, "# TYPE linearvoid_playout_mode gauge")
|
||||
modeVal := 0
|
||||
if playoutMode == "slate" {
|
||||
modeVal = 1
|
||||
} else if playoutMode == "degraded" {
|
||||
modeVal = 2
|
||||
}
|
||||
fmt.Fprintf(w, "linearvoid_playout_mode %d\n", modeVal)
|
||||
})
|
||||
go func() {
|
||||
fmt.Println("Health check server listening on :8081/health")
|
||||
if err := http.ListenAndServe(":8081", nil); err != nil {
|
||||
@@ -214,9 +456,14 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// Run FFmpeg in a loop for continuous playback with R2 sync hook
|
||||
// Run FFmpeg in a loop for continuous playback with slate fallback
|
||||
for {
|
||||
if err := resetHLSOutput("/app/hls"); err != nil {
|
||||
if playoutMode == "slate" {
|
||||
// During slate: attempt recovery by restarting FFmpeg
|
||||
fmt.Printf("[%s] Slate active — attempting live playout recovery...\n", time.Now().Format(time.RFC3339))
|
||||
}
|
||||
|
||||
if err := resetHLSOutput(hlsDir); err != nil {
|
||||
fmt.Println("Failed to reset HLS output:", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
@@ -241,12 +488,32 @@ func main() {
|
||||
"-hls_segment_filename", "/app/hls/segment_%Y%m%d_%H%M%S.m4s",
|
||||
"/app/hls/playlist.m3u8")
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
fmt.Println("FFmpeg exited with error. Restarting in 5s (per ADR-001 resilience)...")
|
||||
runErr := cmd.Run()
|
||||
|
||||
// Track restart and check for slate escalation
|
||||
restartCount++
|
||||
fmt.Printf("[%s] FFmpeg exited (restart #%d in window): %v\n",
|
||||
time.Now().Format(time.RFC3339), restartCount, runErr)
|
||||
|
||||
if shouldEscalateToSlate() {
|
||||
fmt.Printf("[%s] CRITICAL: %d restarts in 60s — escalating to slate fallback\n",
|
||||
time.Now().Format(time.RFC3339), restartCount)
|
||||
if err := activateSlate(); err != nil {
|
||||
fmt.Printf("[%s] Slate activation failed: %v — will retry\n",
|
||||
time.Now().Format(time.RFC3339), err)
|
||||
}
|
||||
// Stay in slate mode, retry recovery after delay
|
||||
time.Sleep(30 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
// If we were in slate but FFmpeg ran successfully, we recovered
|
||||
if playoutMode == "slate" {
|
||||
deactivateSlate()
|
||||
}
|
||||
|
||||
// After FFmpeg run, sync latest segments and manifest to R2
|
||||
if err := syncLatestToR2("/app/hls"); err != nil {
|
||||
if err := syncLatestToR2(hlsDir); err != nil {
|
||||
fmt.Println("R2 sync warning:", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,353 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadPlaylist(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
wantLen int
|
||||
wantErr bool
|
||||
first string
|
||||
}{
|
||||
{
|
||||
name: "simple paths",
|
||||
content: "/videos/video1.mp4\n/videos/video2.mp4\n",
|
||||
wantLen: 2,
|
||||
first: "/videos/video1.mp4",
|
||||
},
|
||||
{
|
||||
name: "with file prefix",
|
||||
content: "file /videos/video1.mp4\nfile '/videos/video2.mp4'\n",
|
||||
wantLen: 2,
|
||||
first: "/videos/video1.mp4",
|
||||
},
|
||||
{
|
||||
name: "with file prefix double quotes",
|
||||
content: "file \"/videos/video1.mp4\"\n",
|
||||
wantLen: 1,
|
||||
first: "/videos/video1.mp4",
|
||||
},
|
||||
{
|
||||
name: "with comments and blanks",
|
||||
content: "# comment\n\n/videos/video1.mp4\n\n# another\n/videos/video2.mp4\n",
|
||||
wantLen: 2,
|
||||
first: "/videos/video1.mp4",
|
||||
},
|
||||
{
|
||||
name: "empty file",
|
||||
content: "",
|
||||
wantLen: 0,
|
||||
},
|
||||
{
|
||||
name: "only comments",
|
||||
content: "# comment\n# another\n",
|
||||
wantLen: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := filepath.Join(tmpDir, tt.name+".txt")
|
||||
os.WriteFile(path, []byte(tt.content), 0644)
|
||||
|
||||
videos, err := LoadPlaylist(path)
|
||||
if tt.wantErr && err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
if !tt.wantErr && err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if len(videos) != tt.wantLen {
|
||||
t.Errorf("len = %d, want %d", len(videos), tt.wantLen)
|
||||
}
|
||||
if tt.wantLen > 0 && videos[0].Path != tt.first {
|
||||
t.Errorf("videos[0].Path = %q, want %q", videos[0].Path, tt.first)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadPlaylist_FileNotFound(t *testing.T) {
|
||||
_, err := LoadPlaylist("/nonexistent/playlist.txt")
|
||||
if err == nil {
|
||||
t.Error("expected error for missing file, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateKeyframeInterval(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
csvData string
|
||||
wantErr bool
|
||||
errText string
|
||||
}{
|
||||
{
|
||||
name: "valid keyframes (gaps <=2s)",
|
||||
csvData: "1,0.00\n0,0.50\n1,1.00\n0,1.50\n1,2.00\n0,2.50\n1,3.00\n",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "gap too large >2s",
|
||||
csvData: "1,0.00\n0,1.00\n1,3.50\n0,4.00\n1,6.00\n",
|
||||
wantErr: true,
|
||||
errText: "keyframe interval too large",
|
||||
},
|
||||
{
|
||||
name: "insufficient keyframes (<2)",
|
||||
csvData: "1,0.00\n",
|
||||
wantErr: true,
|
||||
errText: "insufficient keyframes",
|
||||
},
|
||||
{
|
||||
name: "empty output",
|
||||
csvData: "",
|
||||
wantErr: true,
|
||||
errText: "keyframe probe failed",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// create temp ffprobe that outputs csvData
|
||||
tmpDir := t.TempDir()
|
||||
mockProbe := filepath.Join(tmpDir, "ffprobe")
|
||||
var script string
|
||||
if tt.csvData == "" {
|
||||
script = "@echo off\necho\nexit /b 1\n"
|
||||
} else {
|
||||
script = "@echo off\necho " + strings.ReplaceAll(tt.csvData, "\n", "& echo ") + "\nexit /b 0\n"
|
||||
}
|
||||
os.WriteFile(mockProbe+".bat", []byte(script), 0755)
|
||||
os.Setenv("PATH", tmpDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
defer os.Unsetenv("PATH")
|
||||
|
||||
err := validateKeyframeInterval("test.mp4")
|
||||
if tt.wantErr && err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
if !tt.wantErr && err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if tt.wantErr && tt.errText != "" && err != nil {
|
||||
if !strings.Contains(err.Error(), tt.errText) {
|
||||
t.Errorf("error = %v, want containing %q", err, tt.errText)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateVideoStrict(t *testing.T) {
|
||||
// ValidateVideoStrict calls ValidateVideo which calls ffprobe
|
||||
// Test only the result structure when ValidateVideo returns valid=false
|
||||
// Mock not feasible without full ffprobe mock, skip for now
|
||||
t.Skip("requires ffprobe mock or test media fixtures")
|
||||
}
|
||||
|
||||
func TestHealthHandler(t *testing.T) {
|
||||
// Save and restore globals
|
||||
prevMode := playoutMode
|
||||
prevCount := restartCount
|
||||
playoutMode = "live"
|
||||
restartCount = 3
|
||||
defer func() {
|
||||
playoutMode = prevMode
|
||||
restartCount = prevCount
|
||||
}()
|
||||
|
||||
req := httptest.NewRequest("GET", "/health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
HealthHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, `"healthy"`) {
|
||||
t.Errorf("body missing healthy: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, `"playout"`) {
|
||||
t.Errorf("body missing service: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, `"restarts":3`) {
|
||||
t.Errorf("body missing restarts: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthHandler_Degraded(t *testing.T) {
|
||||
prevMode := playoutMode
|
||||
playoutMode = "slate"
|
||||
defer func() { playoutMode = prevMode }()
|
||||
|
||||
req := httptest.NewRequest("GET", "/health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
HealthHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", w.Code)
|
||||
}
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, `"degraded"`) {
|
||||
t.Errorf("body missing degraded: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldEscalateToSlate(t *testing.T) {
|
||||
// Save globals
|
||||
prevCount := restartCount
|
||||
prevWindow := restartWindow
|
||||
defer func() {
|
||||
restartCount = prevCount
|
||||
restartWindow = prevWindow
|
||||
}()
|
||||
|
||||
// Under threshold
|
||||
restartWindow = time.Now()
|
||||
restartCount = 3
|
||||
if shouldEscalateToSlate() {
|
||||
t.Error("shouldEscalateToSlate() = true, want false (under threshold)")
|
||||
}
|
||||
|
||||
// At threshold
|
||||
restartCount = maxRestartsPerMinute
|
||||
if !shouldEscalateToSlate() {
|
||||
t.Error("shouldEscalateToSlate() = false, want true (at threshold)")
|
||||
}
|
||||
|
||||
// Over threshold
|
||||
restartCount = 7
|
||||
if !shouldEscalateToSlate() {
|
||||
t.Error("shouldEscalateToSlate() = false, want true (over threshold)")
|
||||
}
|
||||
|
||||
// Window expired
|
||||
restartWindow = time.Now().Add(-61 * time.Second)
|
||||
restartCount = 10
|
||||
if shouldEscalateToSlate() {
|
||||
t.Error("shouldEscalateToSlate() = true, want false (window expired, should reset)")
|
||||
}
|
||||
if restartCount != 0 {
|
||||
t.Errorf("restartCount after window reset = %d, want 0", restartCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetHLSOutput(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create some segment files
|
||||
os.MkdirAll(tmpDir, 0755)
|
||||
os.WriteFile(filepath.Join(tmpDir, "segment_test.ts"), []byte("data"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "segment_test.m4s"), []byte("data"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "playlist.m3u8"), []byte("#EXTM3U"), 0644)
|
||||
os.WriteFile(filepath.Join(tmpDir, "init.mp4"), []byte("init"), 0644)
|
||||
|
||||
err := resetHLSOutput(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("resetHLSOutput error: %v", err)
|
||||
}
|
||||
|
||||
// Verify all cleaned
|
||||
for _, name := range []string{"segment_test.ts", "segment_test.m4s", "playlist.m3u8", "init.mp4"} {
|
||||
if _, err := os.Stat(filepath.Join(tmpDir, name)); !os.IsNotExist(err) {
|
||||
t.Errorf("file %s still exists after reset", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetHLSOutput_EmptyDir(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
err := resetHLSOutput(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("resetHLSOutput on empty dir error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivateSlate_NoSlate(t *testing.T) {
|
||||
prevSlateDir := slateDir
|
||||
prevHLSDir := hlsDir
|
||||
prevMode := playoutMode
|
||||
tmpDir := t.TempDir()
|
||||
slateDir = filepath.Join(tmpDir, "slate")
|
||||
hlsDir = filepath.Join(tmpDir, "hls")
|
||||
os.MkdirAll(hlsDir, 0755)
|
||||
defer func() {
|
||||
slateDir = prevSlateDir
|
||||
hlsDir = prevHLSDir
|
||||
playoutMode = prevMode
|
||||
}()
|
||||
|
||||
err := activateSlate()
|
||||
if err == nil {
|
||||
t.Error("expected error when slate HLS missing, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivateSlate_Success(t *testing.T) {
|
||||
prevSlateDir := slateDir
|
||||
prevHLSDir := hlsDir
|
||||
prevMode := playoutMode
|
||||
tmpDir := t.TempDir()
|
||||
slateDir = filepath.Join(tmpDir, "slate")
|
||||
hlsDir = filepath.Join(tmpDir, "hls")
|
||||
|
||||
// Create slate HLS structure
|
||||
slateHLS := filepath.Join(slateDir, "hls")
|
||||
os.MkdirAll(slateHLS, 0755)
|
||||
os.MkdirAll(hlsDir, 0755)
|
||||
os.WriteFile(filepath.Join(slateHLS, "slate.m3u8"), []byte("#EXTM3U\n#EXT-X-ENDLIST\n"), 0644)
|
||||
os.WriteFile(filepath.Join(slateHLS, "slate_segment.ts"), []byte("slate-data"), 0644)
|
||||
|
||||
defer func() {
|
||||
slateDir = prevSlateDir
|
||||
hlsDir = prevHLSDir
|
||||
playoutMode = prevMode
|
||||
}()
|
||||
|
||||
err := activateSlate()
|
||||
if err != nil {
|
||||
t.Fatalf("activateSlate error: %v", err)
|
||||
}
|
||||
|
||||
if playoutMode != "slate" {
|
||||
t.Errorf("playoutMode = %q, want slate", playoutMode)
|
||||
}
|
||||
|
||||
// Verify files copied
|
||||
if _, err := os.Stat(filepath.Join(hlsDir, "slate.m3u8")); os.IsNotExist(err) {
|
||||
t.Error("slate.m3u8 not copied to hls dir")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeactivateSlate(t *testing.T) {
|
||||
prevMode := playoutMode
|
||||
playoutMode = "slate"
|
||||
defer func() { playoutMode = prevMode }()
|
||||
|
||||
deactivateSlate()
|
||||
|
||||
if playoutMode != "live" {
|
||||
t.Errorf("playoutMode = %q, want live", playoutMode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePlaylist(t *testing.T) {
|
||||
// ValidatePlaylist calls ValidateVideo which needs ffprobe
|
||||
t.Skip("requires ffprobe mock or test media fixtures")
|
||||
}
|
||||
|
||||
func TestSyncToR2(t *testing.T) {
|
||||
// Requires R2/S3 mock
|
||||
t.Skip("requires S3 client mock")
|
||||
}
|
||||
+62
-5
@@ -1,3 +1,9 @@
|
||||
# docker-compose.prod.yml — TEST LAB DEPLOYMENT
|
||||
# Target: test lab host 192.168.88.40 (NOT Hetzner production).
|
||||
# Production Hetzner VPS not yet provisioned — see DEPLOYMENT.md Production Deployment section.
|
||||
# Uses pre-built images from Gitea Container Registry (no local build).
|
||||
# Override DOCKER_HOST in CD workflow to switch target.
|
||||
|
||||
services:
|
||||
playout:
|
||||
image: git.allucanget.biz/zwitschi/linearvoid/playout:latest
|
||||
@@ -9,6 +15,7 @@ services:
|
||||
- ./data/playlist:/app/playlist:rw
|
||||
- ./data/media:/app/media:ro
|
||||
- ./data/hls:/app/hls:rw
|
||||
- ./data/slate:/app/slate:ro
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
@@ -42,7 +49,7 @@ services:
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 10s
|
||||
start_period: 30s
|
||||
|
||||
nginx:
|
||||
image: git.allucanget.biz/zwitschi/linearvoid/nginx:latest
|
||||
@@ -68,7 +75,7 @@ services:
|
||||
image: redis:7-alpine
|
||||
container_name: linearvoid-redis
|
||||
restart: unless-stopped
|
||||
command: redis-server --appendonly yes
|
||||
command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
networks:
|
||||
@@ -97,6 +104,56 @@ services:
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
||||
nginx-exporter:
|
||||
image: nginx/nginx-prometheus-exporter:latest
|
||||
container_name: linearvoid-nginx-exporter
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- "-nginx.scrape-uri=http://nginx/nginx-status"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
- nginx
|
||||
|
||||
redis-exporter:
|
||||
image: oliver006/redis_exporter:latest
|
||||
container_name: linearvoid-redis-exporter
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- "-redis.addr=redis://redis:6379"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
- redis
|
||||
|
||||
postgres-exporter:
|
||||
image: prometheuscommunity/postgres-exporter:latest
|
||||
container_name: linearvoid-postgres-exporter
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
DATA_SOURCE_NAME: "postgresql://linearvoid:${POSTGRES_PASSWORD:-linearvoid}@db:5432/linearvoid?sslmode=disable"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
- db
|
||||
|
||||
node-exporter:
|
||||
image: prom/node-exporter:latest
|
||||
container_name: linearvoid-node-exporter
|
||||
restart: unless-stopped
|
||||
pid: host
|
||||
volumes:
|
||||
- /proc:/host/proc:ro
|
||||
- /sys:/host/sys:ro
|
||||
- /:/rootfs:ro
|
||||
command:
|
||||
- "--path.procfs=/host/proc"
|
||||
- "--path.sysfs=/host/sys"
|
||||
- "--path.rootfs=/rootfs"
|
||||
- "--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:latest
|
||||
container_name: linearvoid-prometheus
|
||||
@@ -123,8 +180,7 @@ services:
|
||||
- "3001:3000"
|
||||
volumes:
|
||||
- grafana-data:/var/lib/grafana
|
||||
- ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards
|
||||
- ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources
|
||||
- grafana-provisioning:/etc/grafana/provisioning
|
||||
environment:
|
||||
- GF_SECURITY_ADMIN_USER=admin
|
||||
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin}
|
||||
@@ -141,6 +197,7 @@ networks:
|
||||
volumes:
|
||||
redis-data:
|
||||
postgres-data:
|
||||
prometheus-data:
|
||||
prometheus-config:
|
||||
prometheus-data:
|
||||
grafana-data:
|
||||
grafana-provisioning:
|
||||
+58
-1
@@ -1,3 +1,8 @@
|
||||
# docker-compose.yml — LOCAL DEVELOPMENT
|
||||
# Target: local workstation or test lab (192.168.88.0/24).
|
||||
# NOT for production. Use docker-compose.prod.yml for deployment.
|
||||
# Services build from source; ports exposed for direct dev access.
|
||||
|
||||
services:
|
||||
playout:
|
||||
image: ${CI_REGISTRY_URL:-git.allucanget.biz}/${CI_REGISTRY_NAMESPACE:-zwitschi/linearvoid}/playout:latest
|
||||
@@ -12,6 +17,7 @@ services:
|
||||
- ./data/playlist:/app/playlist:ro
|
||||
- ./data/media:/app/media:ro
|
||||
- ./data/hls:/app/hls:rw
|
||||
- ./data/slate:/app/slate:ro
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
@@ -72,7 +78,7 @@ services:
|
||||
image: redis:7-alpine
|
||||
container_name: linearvoid-redis
|
||||
restart: unless-stopped
|
||||
command: redis-server --appendonly yes
|
||||
command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
networks:
|
||||
@@ -91,12 +97,63 @@ services:
|
||||
networks:
|
||||
- linearvoid-net
|
||||
|
||||
nginx-exporter:
|
||||
image: nginx/nginx-prometheus-exporter:latest
|
||||
container_name: linearvoid-nginx-exporter
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- "-nginx.scrape-uri=http://nginx/nginx-status"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
- nginx
|
||||
|
||||
redis-exporter:
|
||||
image: oliver006/redis_exporter:latest
|
||||
container_name: linearvoid-redis-exporter
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- "-redis.addr=redis://redis:6379"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
- redis
|
||||
|
||||
postgres-exporter:
|
||||
image: prometheuscommunity/postgres-exporter:latest
|
||||
container_name: linearvoid-postgres-exporter
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
DATA_SOURCE_NAME: "postgresql://linearvoid:${POSTGRES_PASSWORD:-linearvoid}@db:5432/linearvoid?sslmode=disable"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
depends_on:
|
||||
- db
|
||||
|
||||
node-exporter:
|
||||
image: prom/node-exporter:latest
|
||||
container_name: linearvoid-node-exporter
|
||||
restart: unless-stopped
|
||||
pid: host
|
||||
volumes:
|
||||
- /proc:/host/proc:ro
|
||||
- /sys:/host/sys:ro
|
||||
- /:/rootfs:ro
|
||||
command:
|
||||
- "--path.procfs=/host/proc"
|
||||
- "--path.sysfs=/host/sys"
|
||||
- "--path.rootfs=/rootfs"
|
||||
- "--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)"
|
||||
networks:
|
||||
- linearvoid-net
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:latest
|
||||
container_name: linearvoid-prometheus
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- ./monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
|
||||
- ./monitoring/prometheus/alerts.yml:/etc/prometheus/alerts.yml
|
||||
- prometheus-data:/prometheus
|
||||
ports:
|
||||
- "9090:9090"
|
||||
|
||||
+586
-225
@@ -4,18 +4,93 @@
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Introduction and Goals](#introduction-and-goals)
|
||||
- [Architecture Constraints](#architecture-constraints)
|
||||
- [Context and Scope](#context-and-scope)
|
||||
- [Solution Strategy](#solution-strategy)
|
||||
- [Building Block View](#building-block-view)
|
||||
- [Runtime View](#runtime-view)
|
||||
- [Deployment View](#deployment-view)
|
||||
- [Cross-cutting Concepts](#cross-cutting-concepts)
|
||||
- [Architecture Decisions](#architecture-decisions)
|
||||
- [Quality Requirements](#quality-requirements)
|
||||
- [Risks and Technical Debts](#risks-and-technical-debts)
|
||||
- [Glossary](#glossary)
|
||||
- [Architecture Documentation for Linear Void](#architecture-documentation-for-linear-void)
|
||||
- [Table of Contents](#table-of-contents)
|
||||
- [Companion Documentation](#companion-documentation)
|
||||
- [Introduction and Goals](#introduction-and-goals)
|
||||
- [Requirements Overview](#requirements-overview)
|
||||
- [Quality Goals](#quality-goals)
|
||||
- [Stakeholders](#stakeholders)
|
||||
- [Architecture Constraints](#architecture-constraints)
|
||||
- [Context and Scope](#context-and-scope)
|
||||
- [Business Context](#business-context)
|
||||
- [High-Level Context Diagram (Mermaid)](#high-level-context-diagram-mermaid)
|
||||
- [Technical Context](#technical-context)
|
||||
- [Solution Strategy](#solution-strategy)
|
||||
- [Building Block View](#building-block-view)
|
||||
- [Whitebox Overall System](#whitebox-overall-system)
|
||||
- [Level 2](#level-2)
|
||||
- [Playout Service (`backend/playout/src/playlist.go`)](#playout-service-backendplayoutsrcplaylistgo)
|
||||
- [Chat Service (`backend/chat/src/main.go`)](#chat-service-backendchatsrcmaingo)
|
||||
- [Frontend SPA (`frontend/`)](#frontend-spa-frontend)
|
||||
- [Reverse Proxy (`infrastructure/nginx/nginx.conf`)](#reverse-proxy-infrastructurenginxnginxconf)
|
||||
- [Level 3](#level-3)
|
||||
- [Chat Service Modules](#chat-service-modules)
|
||||
- [Playout Service Modules](#playout-service-modules)
|
||||
- [Frontend Modules](#frontend-modules)
|
||||
- [Runtime View](#runtime-view)
|
||||
- [Playout \& Delivery Scenario](#playout--delivery-scenario)
|
||||
- [Deployment View](#deployment-view)
|
||||
- [CI/CD Pipeline Architecture](#cicd-pipeline-architecture)
|
||||
- [Infrastructure Level 1](#infrastructure-level-1)
|
||||
- [Infrastructure Level 2](#infrastructure-level-2)
|
||||
- [Cross-cutting Concepts](#cross-cutting-concepts)
|
||||
- [Caching \& Cost Optimization](#caching--cost-optimization)
|
||||
- [Video Normalization \& Playout](#video-normalization--playout)
|
||||
- [Required Source Profile (Linear Void v1)](#required-source-profile-linear-void-v1)
|
||||
- [Reference Normalization Command](#reference-normalization-command)
|
||||
- [Verification Gate (ffprobe)](#verification-gate-ffprobe)
|
||||
- [Monitoring \& Resilience](#monitoring--resilience)
|
||||
- [Prometheus Baseline](#prometheus-baseline)
|
||||
- [Grafana Dashboards](#grafana-dashboards)
|
||||
- [Alerting Rules (Prometheus Rule Groups)](#alerting-rules-prometheus-rule-groups)
|
||||
- [API Documentation Reference](#api-documentation-reference)
|
||||
- [Chat WebSocket API](#chat-websocket-api)
|
||||
- [Playout and Delivery REST API](#playout-and-delivery-rest-api)
|
||||
- [Admin REST API](#admin-rest-api)
|
||||
- [Cloudflare Configuration Reference](#cloudflare-configuration-reference)
|
||||
- [DNS and Zone Routing](#dns-and-zone-routing)
|
||||
- [Cache Rules Baseline](#cache-rules-baseline)
|
||||
- [Edge Security and Controls](#edge-security-and-controls)
|
||||
- [Worker and Header Policy](#worker-and-header-policy)
|
||||
- [Operations and Validation](#operations-and-validation)
|
||||
- [Security \& Moderation](#security--moderation)
|
||||
- [Transport and Edge Protection](#transport-and-edge-protection)
|
||||
- [Reverse Proxy Enforcement (nginx)](#reverse-proxy-enforcement-nginx)
|
||||
- [Admin Authentication and Session Security](#admin-authentication-and-session-security)
|
||||
- [Rate Limiting and Abuse Controls](#rate-limiting-and-abuse-controls)
|
||||
- [CORS and Cross-Origin Behavior](#cors-and-cross-origin-behavior)
|
||||
- [Moderation and Privacy](#moderation-and-privacy)
|
||||
- [Chat](#chat)
|
||||
- [Architecture Decisions](#architecture-decisions)
|
||||
- [Quality Requirements](#quality-requirements)
|
||||
- [Performance Benchmarks and Capacity Planning](#performance-benchmarks-and-capacity-planning)
|
||||
- [Baseline SLO Targets (v1)](#baseline-slo-targets-v1)
|
||||
- [Workload Assumptions](#workload-assumptions)
|
||||
- [Initial Capacity Model](#initial-capacity-model)
|
||||
- [Benchmark Plan](#benchmark-plan)
|
||||
- [Capacity Triggers and Actions](#capacity-triggers-and-actions)
|
||||
- [Scaling Strategy](#scaling-strategy)
|
||||
- [Phase 1: Vertical + Cache-First (Current Baseline)](#phase-1-vertical--cache-first-current-baseline)
|
||||
- [Phase 2: Service Separation](#phase-2-service-separation)
|
||||
- [Phase 3: Multi-Origin Resilience](#phase-3-multi-origin-resilience)
|
||||
- [Phase 4: Regional and Throughput Expansion](#phase-4-regional-and-throughput-expansion)
|
||||
- [Scaling Triggers](#scaling-triggers)
|
||||
- [Disaster Recovery and Backup Procedures](#disaster-recovery-and-backup-procedures)
|
||||
- [Recovery Targets](#recovery-targets)
|
||||
- [Backup Scope](#backup-scope)
|
||||
- [Backup Cadence](#backup-cadence)
|
||||
- [Recovery Runbook (Minimum)](#recovery-runbook-minimum)
|
||||
- [Failure Modes and Strategy](#failure-modes-and-strategy)
|
||||
- [Risks and Technical Debts](#risks-and-technical-debts)
|
||||
- [Glossary](#glossary)
|
||||
|
||||
## Companion Documentation
|
||||
|
||||
- [API.md](../../API.md) - Implemented WebSocket and REST API reference.
|
||||
- [DEPLOYMENT.md](../../DEPLOYMENT.md) - Production deployment and operational runbook.
|
||||
- [MONITORING.md](../../MONITORING.md) - Metrics, dashboards, and monitoring troubleshooting.
|
||||
- [SECURITY.md](../../SECURITY.md) - Security model, controls, and hardening baseline.
|
||||
|
||||
## Introduction and Goals
|
||||
|
||||
@@ -38,14 +113,14 @@ Full decision rationale, alternatives, and consequences in [`adr/ADR-001.md`](ad
|
||||
|
||||
### Quality Goals
|
||||
|
||||
| Priority | Quality Attribute | Goal | Scenario |
|
||||
| -------- | ------------------------- | -------------------------------------------------------- | -------------------------------------------------------------------------------- |
|
||||
| 1 | **Cost Efficiency** | <$25/month for moderate scale (100s concurrent) | R2 $0 egress + immutable `.ts` edge caching eliminates per-viewer bandwidth cost |
|
||||
| 2 | **Performance / Latency** | Low US latency; acceptable HLS delay | Hetzner Ashburn origin + Cloudflare US POPs (15–35s end-to-end) |
|
||||
| 3 | **Availability** | 24/7 with minimal downtime | FFmpeg/MediaMTX auto-restart loop, fallback slate, monitoring |
|
||||
| 4 | **Scalability** | Grow from 10s to 1000s viewers without proportional cost | Edge caching + R2; only request volume limits |
|
||||
| 5 | **Maintainability** | Lightweight, clear configs, low ops overhead | Static SPA, Go/Node chat, SQLite/PostgreSQL+Redis |
|
||||
| 6 | **Security / Usability** | Secure transport, moderation tools, good UX | HTTPS/WSS, Cloudflare WAF, rate limits, intuitive chat/player |
|
||||
| Priority | Quality Attribute | Goal | Scenario |
|
||||
| -------- | ------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
|
||||
| 1 | **Cost Efficiency** | <$25/month for moderate scale (100s concurrent) | R2 $0 egress + immutable `.m4s` and `.ts` edge caching eliminates per-viewer bandwidth cost |
|
||||
| 2 | **Performance / Latency** | Low US latency; acceptable HLS delay | Hetzner Ashburn origin + Cloudflare US POPs (15–35s end-to-end) |
|
||||
| 3 | **Availability** | 24/7 with minimal downtime | FFmpeg/MediaMTX auto-restart loop, fallback slate, monitoring |
|
||||
| 4 | **Scalability** | Grow from 10s to 1000s viewers without proportional cost | Edge caching + R2; only request volume limits |
|
||||
| 5 | **Maintainability** | Lightweight, clear configs, low ops overhead | Static SPA, Go/Node chat, SQLite/PostgreSQL+Redis |
|
||||
| 6 | **Security / Usability** | Secure transport, moderation tools, good UX | HTTPS/WSS, Cloudflare WAF, rate limits, intuitive chat/player |
|
||||
|
||||
### Stakeholders
|
||||
|
||||
@@ -107,18 +182,11 @@ flowchart TD
|
||||
Chat --> VPS
|
||||
VPS -->|Proxy / Static| Pages
|
||||
CDN -->|Origin Pull on Miss| R2
|
||||
|
||||
classDef cloud fill:#f0f0ff,stroke:#4263eb
|
||||
classDef vps fill:#f0fff0,stroke:#2e7d32
|
||||
classDef user fill:#fff0f0,stroke:#c62828
|
||||
class Cloudflare,CDN,Pages,R2 cloud
|
||||
class VPS,Playout,Chat vps
|
||||
class Viewers,Admins user
|
||||
```
|
||||
|
||||
**External Interfaces**:
|
||||
|
||||
- **Cloudflare**: CDN (cache rules for `.ts` vs `.m3u8`), R2 (object storage, $0 egress), Pages (static hosting), WAF/TLS.
|
||||
- **Cloudflare**: CDN (cache rules for `.m4s` and `.ts` vs `.m3u8`), R2 (object storage, $0 egress), Pages (static hosting), WAF/TLS.
|
||||
- **Hetzner VPS**: Compute for playout and chat (low-cost US-East with good peering).
|
||||
- **Browsers**: Native HLS (`<video>` + hls.js) and WebSocket APIs.
|
||||
- **Source**: Pre-normalized MP4 files uploaded to R2.
|
||||
@@ -128,7 +196,7 @@ This diagram synthesizes the context views from all original ADRs. Full decision
|
||||
### Technical Context
|
||||
|
||||
- **Playout**: FFmpeg consuming normalized playlist → continuous HLS.
|
||||
- **Packaging/Delivery**: MediaMTX/SRS/Nginx → immutable `.ts` segments to R2; Cloudflare edge cache.
|
||||
- **Packaging/Delivery**: MediaMTX/SRS/Nginx → immutable `.m4s` and `.ts` segments to R2; Cloudflare edge cache.
|
||||
- **Frontend**: Static SPA with hls.js.
|
||||
- **Chat**: Lightweight WebSocket service (Node/Go) + Redis (presence/rate limits) + PostgreSQL/SQLite (history/moderation).
|
||||
- **Normalization**: All source videos must be H.264/AAC, 30 fps constant, 2s keyframes.
|
||||
@@ -144,7 +212,7 @@ This diagram synthesizes the context views from all original ADRs. Full decision
|
||||
|
||||
1. **US-East Playout on Hetzner Ashburn VPS** (`CX22` or equivalent): FFmpeg + MediaMTX/SRS for continuous playlist-to-HLS conversion (`-c copy` where possible).
|
||||
2. **Immutable HLS to Cloudflare R2**: Segments use unique names; R2 acts as origin with $0 egress.
|
||||
3. **Aggressive Edge Caching**: Long TTL for `.ts` (immutable), short TTL (1–10s) for live `.m3u8`. Tiered Cache enabled. This makes viewer delivery essentially free.
|
||||
3. **Aggressive Edge Caching**: Long TTL for `.m4s` and `.ts` (immutable), short TTL (1–10s) for live `.m3u8`. Tiered Cache enabled. This makes viewer delivery essentially free.
|
||||
4. **Lightweight Custom Frontend**: Static HTML/JS SPA + hls.js player + embedded chat sidebar.
|
||||
5. **Separate but co-located Chat Service**: WebSockets with persistence and moderation (low bandwidth).
|
||||
6. **Normalization & Monitoring**: Pre-process all videos; use dashboards + alerts for playout health, cache hit ratio, R2 ops.
|
||||
@@ -162,7 +230,7 @@ Overview (Mermaid diagram):
|
||||
```mermaid
|
||||
graph TD
|
||||
subgraph Cloudflare
|
||||
CDN[CDN + Cache Rules<br/>.ts immutable long TTL<br/>.m3u8 short TTL]
|
||||
CDN[CDN + Cache Rules<br/>.m4s and .ts immutable long TTL<br/>.m3u8 short TTL]
|
||||
R2[R2 - HLS + Source]
|
||||
Pages[Pages - Static SPA]
|
||||
end
|
||||
@@ -196,9 +264,116 @@ graph TD
|
||||
|
||||
Detailed black-box descriptions, level-2/3 views, and code locations are in `adr/ADR-001.md` and future implementation ADRs.
|
||||
|
||||
### Level 2 & 3
|
||||
### Level 2
|
||||
|
||||
Deferred to implementation (see ADR for component responsibilities, interfaces, quality attributes, and risks). Playout enforces normalization; Delivery layer focuses on cache rules and immutable naming.
|
||||
Detailed component diagram with code locations and persistence.
|
||||
|
||||
```mermaid
|
||||
graph TB
|
||||
subgraph "Cloudflare Edge"
|
||||
CF[CDN + WAF<br/>Cache Rules]
|
||||
R2[R2 Bucket<br/>media + HLS]
|
||||
PAGES[Pages<br/>SPA hosting]
|
||||
end
|
||||
|
||||
subgraph "Docker Host (Hetzner CX22)"
|
||||
NGINX[nginx<br/>:80/:443<br/>proxy + auth]
|
||||
CHAT[chat service<br/>:3000<br/>Go + gorilla/websocket]
|
||||
PLAYOUT[playout service<br/>:8081<br/>Go + FFmpeg]
|
||||
REDIS[Redis 7<br/>:6379<br/>presence/rate/pubsub]
|
||||
DB[PostgreSQL 15<br/>:5432<br/>chat history/moderation]
|
||||
end
|
||||
|
||||
subgraph "External"
|
||||
VIEWER[Viewer Browser<br/>hls.js + WebSocket]
|
||||
ADMIN[Admin Browser<br/>SPA + uploads]
|
||||
end
|
||||
|
||||
VIEWER -->|HTTPS| CF
|
||||
VIEWER -->|WSS| NGINX
|
||||
ADMIN -->|HTTPS| CF
|
||||
CF --> NGINX
|
||||
NGINX --> CHAT
|
||||
NGINX --> PLAYOUT
|
||||
NGINX -->|/hls/*| R2
|
||||
CHAT --> REDIS
|
||||
CHAT --> DB
|
||||
PLAYOUT --> R2
|
||||
PLAYOUT -->|reads| R2
|
||||
```
|
||||
|
||||
#### Playout Service (`backend/playout/src/playlist.go`)
|
||||
|
||||
- **Role**: Continuous HLS encoding loop using FFmpeg concat demuxer
|
||||
- **Input**: `data/playlist/playlist.txt` — ordered list of MP4 file paths
|
||||
- **Output**: HLS segments (`.m4s` and `.ts`) + live manifest (`.m3u8`) written to `data/hls/`, mirrored to R2
|
||||
- **Resilience**: Restart counter (>5/60s → activate slate), Prometheus metrics on port 8081
|
||||
- **Validation**: ffprobe gate on all ingested files (codec, fps, resolution, keyframe interval)
|
||||
|
||||
#### Chat Service (`backend/chat/src/main.go`)
|
||||
|
||||
- **Role**: WebSocket chat with rooms, DMs, moderation, admin API
|
||||
- **Persistence**: PostgreSQL 15 via GORM (messages, moderation logs, media metadata). SQLite for dev fallback
|
||||
- **Redis**: Presence tracking, rate limiting, pub/sub for multi-instance fan-out
|
||||
- **Auth**: Session-cookie admin auth with Redis-backed expiry
|
||||
- **Metrics**: Connected clients, stored messages, DMs, moderation actions on `/metrics`
|
||||
|
||||
#### Frontend SPA (`frontend/`)
|
||||
|
||||
- **Viewer**: `src/main.ts` — hls.js player, WebSocket chat, presence indicators, schedule overlay
|
||||
- **Admin**: `src/admin.ts` — 9 dynamic tabs (dashboard, upload, playlist, schedule, moderation, chat-log, users, analytics, system)
|
||||
- **Build**: Vite → static output in `frontend/dist/`
|
||||
|
||||
#### Reverse Proxy (`infrastructure/nginx/nginx.conf`)
|
||||
|
||||
- auth_request gate on `/admin` and `/api/admin`
|
||||
- WebSocket upgrade for `/ws`
|
||||
- HLS CORS headers + OPTIONS preflight
|
||||
- `/health` JSON endpoint, `/nginx-status` stub_status
|
||||
|
||||
### Level 3
|
||||
|
||||
Module-level breakdown per service.
|
||||
|
||||
#### Chat Service Modules
|
||||
|
||||
| Module | File/Lines | Responsibility |
|
||||
| ---------------------- | -------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
|
||||
| **Connection Manager** | `main.go` (hub, clients map) | Track active WebSocket connections per room, fan-out messages |
|
||||
| **Message Parser** | `main.go` (readPump, handleMessage) | Parse JSON envelope `{type, room?, ...}`, validate fields, route to handler |
|
||||
| **Room Registry** | `main.go` (rooms map, room broadcasts) | Map room name → client set, handle join/leave/close |
|
||||
| **DM Router** | `main.go` (handleDirectMessage) | Resolve target user in clients map, deliver private message |
|
||||
| **Persistence Layer** | `main.go` (GORM models: Message, DirectMessage, ModerationLog) | Store/retrieve chat messages, DMs, moderation events |
|
||||
| **Redis Subsystem** | `main.go` (redisClient, rate limiting, pub/sub, presence) | INCR+EXPIRE rate limit, SETEX presence, PSUBSCRIBE room fan-out |
|
||||
| **Auth Handler** | `main.go` (handleAdminLogin, adminAuthMiddleware) | Session cookie creation/validation, login rate limiting |
|
||||
| **Admin API Router** | `main.go` (handleAdmin\*) | Upload, playlist CRUD, schedule overrides, moderation actions, analytics, system summary |
|
||||
| **Metrics Exporter** | `main.go` (prometheus.MustRegister) | Expose connected_clients, messages_total, dms_total, moderation_actions, redis_up |
|
||||
|
||||
#### Playout Service Modules
|
||||
|
||||
| Module | File/Lines | Responsibility |
|
||||
| -------------------- | ---------------------------------------------- | ------------------------------------------------------------------ |
|
||||
| **Playlist Parser** | `playlist.go` (parsePlaylist) | Read `playlist.txt`, skip comments/blank lines, resolve file paths |
|
||||
| **FFmpeg Manager** | `playlist.go` (runPlayout, restart logic) | Spawn/monitor FFmpeg concat process, restart on crash |
|
||||
| **Slate Controller** | `playlist.go` (activateSlate, deactivateSlate) | Switch output to slate loop on excessive failures |
|
||||
| **R2 Sync** | `playlist.go` (syncToR2) | Upload HLS segments + manifest to R2 bucket |
|
||||
| **Health Endpoint** | `playlist.go` (healthHandler) | `/health` → JSON with mode, segment count, uptime |
|
||||
| **Metrics Exporter** | `playlist.go` (prometheus) | Expose restarts_total, mode (live/slate) |
|
||||
|
||||
#### Frontend Modules
|
||||
|
||||
| Module | File | Responsibility |
|
||||
| ----------------------- | ----------------------------------------------- | ----------------------------------------------------------------- |
|
||||
| **HLS Player** | `src/main.ts` | hls.js initialization, attach to video element, error recovery |
|
||||
| **WebSocket Client** | `src/main.ts` | Connect/disconnect, parse envelope, dispatch to UI handlers |
|
||||
| **Chat UI** | `src/main.ts` | Message list rendering, room tabs, DM panel, presence dots |
|
||||
| **Schedule Overlay** | `src/main.ts` | Poll `/api/schedule`, render current/next program |
|
||||
| **Admin Router** | `src/admin.ts` | Tab-based navigation, dynamic page loading |
|
||||
| **Upload Manager** | `src/admin.ts` | Multipart upload with chunk progress, ffprobe validation feedback |
|
||||
| **Playlist Editor** | `src/admin.ts` | Drag-reorder, add/remove items, save to `/api/admin/playlist` |
|
||||
| **Moderation Panel** | `src/admin.ts` + `public/admin/moderation.html` | Inline mute/ban, action log viewer |
|
||||
| **Analytics Dashboard** | `src/admin.ts` + `public/admin/analytics.html` | Message counts, top rooms, moderation stats |
|
||||
| **System Config** | `src/admin.ts` + `public/admin/system.html` | Service health, config metadata, restart buttons |
|
||||
|
||||
## Runtime View
|
||||
|
||||
@@ -208,7 +383,7 @@ Deferred to implementation (see ADR for component responsibilities, interfaces,
|
||||
2. Playout worker (FFmpeg loop with concat demuxer) reads next file, packages as HLS segments (4-6s).
|
||||
3. Segments uploaded to R2 with unique immutable names; live `.m3u8` updated.
|
||||
4. Viewer loads SPA → requests master/live manifest → hls.js player follows current edge.
|
||||
5. Cloudflare serves `.ts` from nearest edge cache (origin/R2 hit only once per POP/region).
|
||||
5. Cloudflare serves `.m4s` and `.ts` from nearest edge cache (origin/R2 hit only once per POP/region).
|
||||
6. Parallel WebSocket connection to chat service for synchronized interaction.
|
||||
|
||||
Notable aspects: Manifest freshness ensures all viewers stay on same timeline; cache hit ratio directly impacts cost and latency. Fallback slate activates on playout failure. Full cache policy, example configs (FFmpeg bash loop, Nginx RTMP HLS), and storage tables in `adr/ADR-001.md`.
|
||||
@@ -219,7 +394,7 @@ Notable aspects: Manifest freshness ensures all viewers stay on same timeline; c
|
||||
|
||||
### CI/CD Pipeline Architecture
|
||||
|
||||
The deployment follows a two-stage pipeline: **CI builds and pushes images**, then **CD pulls and deploys** them to the production host.
|
||||
The deployment uses parallel workflows on each push to `main`: **CI builds and pushes images** while **CD deploys latest images** to the test lab. A dedicated release workflow handles semantic version tags. Current runtime target is the **test lab** (192.168.88.40). Production Hetzner VPS is planned but not yet provisioned — the same pipeline will deploy there by changing `DOCKER_HOST`.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
@@ -230,7 +405,7 @@ flowchart LR
|
||||
subgraph "Gitea Container Registry"
|
||||
REGISTRY[git.allucanget.biz<br/>playout:latest<br/>chat:latest<br/>nginx:latest]
|
||||
end
|
||||
subgraph "Production Host (192.168.88.40)"
|
||||
subgraph "Test Lab Host (192.168.88.40) — Production Hetzner TBD"
|
||||
DOCKER[Docker Engine<br/>via TCP :2375]
|
||||
PLAYOUT[linearvoid-playout]
|
||||
CHAT[linearvoid-chat]
|
||||
@@ -251,30 +426,27 @@ flowchart LR
|
||||
DOCKER --> DB
|
||||
DOCKER --> PROM
|
||||
DOCKER --> GRAF
|
||||
|
||||
classDef ci fill:#e1f5fe,stroke:#0288d1
|
||||
classDef reg fill:#fff3e0,stroke:#f57c00
|
||||
classDef prod fill:#e8f5e9,stroke:#388e3c
|
||||
class CI,CD ci
|
||||
class REGISTRY reg
|
||||
class DOCKER,PLAYOUT,CHAT,NGINX,REDIS,DB,PROM,GRAF prod
|
||||
```
|
||||
|
||||
**Pipeline Sequence:**
|
||||
|
||||
1. **CI Workflow** (`.gitea/workflows/ci.yml`):
|
||||
- Triggers on `push` to `main` or `pull_request`
|
||||
- Runs `test` job: frontend lint/build, Go validation, `docker compose build`
|
||||
- Triggers on `push` to `main` and `pull_request`
|
||||
- Runs `test` job: frontend lint/build and Go validation
|
||||
- Runs `docker` job (after `test` passes, only on `main` push):
|
||||
- Builds `playout`, `chat`, `nginx` images with Buildx
|
||||
- Pushes to Gitea Container Registry with `main` and `latest` tags
|
||||
2. **CD Workflow** (`.gitea/workflows/cd.yml`):
|
||||
- Triggers on `workflow_run` when CI completes successfully, or manually via `workflow_dispatch`
|
||||
- Pulls latest images from registry
|
||||
- Triggers on `push` to `main` (parallel to CI), or manually via `workflow_dispatch`
|
||||
- Logs in remote Docker host to private registry
|
||||
- Generates `.env` from secrets
|
||||
- Seeds Prometheus config into named volume
|
||||
- Deploys via `docker compose -f docker-compose.prod.yml up -d` to remote host (`192.168.88.40:2375`)
|
||||
- Seeds Prometheus and Grafana provisioning into named volumes
|
||||
- Deploys via `docker compose -f docker-compose.prod.yml up -d --remove-orphans --force-recreate` to remote host (`192.168.88.40:2375` — test lab; switch to Hetzner IP for production)
|
||||
- Runs health checks with retry logic for playout initialization
|
||||
3. **Release Workflow** (`.gitea/workflows/release.yml`):
|
||||
- Triggers on semantic tags in `*.*.*` format (for example `1.0.9`)
|
||||
- Builds and pushes versioned image tags (`X.Y.Z`, `X.Y`, `latest`)
|
||||
- Creates release entry in Gitea
|
||||
|
||||
### Infrastructure Level 1
|
||||
|
||||
@@ -292,12 +464,12 @@ flowchart LR
|
||||
|
||||
### Infrastructure Level 2
|
||||
|
||||
**Hetzner VPS**: Runs playout (FFmpeg/MediaMTX), chat backend, Nginx. Monitoring via dashboards + UptimeRobot. Backups to R2.
|
||||
**Hetzner VPS (planned):** Will run playout (FFmpeg/MediaMTX), chat backend, Nginx. Monitoring via dashboards + UptimeRobot. Backups to R2. Currently test lab (`192.168.88.40`) serves this role. Production Hetzner instance not yet provisioned — see Production Deployment section in [`DEPLOYMENT.md`](../../DEPLOYMENT.md).
|
||||
|
||||
**Cloudflare Layer**:
|
||||
|
||||
- R2 bucket for media.
|
||||
- Cache Rules (`.ts`: Cache Everything, immutable, long TTL; `.m3u8`: short TTL/must-revalidate).
|
||||
- Cache Rules (`.m4s` and `.ts`: Cache Everything, immutable, long TTL; `.m3u8`: short TTL/must-revalidate).
|
||||
- WAF, rate limiting, Tiered Cache.
|
||||
- Pages for SPA hosting.
|
||||
|
||||
@@ -307,19 +479,226 @@ Future evolution: Docker Compose, managed DB, secondary regions (detailed in sub
|
||||
|
||||
### Caching & Cost Optimization
|
||||
|
||||
Immutable segment naming + targeted Cloudflare Cache Rules + R2 ($0 egress) = near-zero viewer bandwidth cost. Long TTL on `.ts`, short on live manifests. Monitor hit ratio and R2 Class B ops.
|
||||
Immutable segment naming + targeted Cloudflare Cache Rules + R2 ($0 egress) = near-zero viewer bandwidth cost. Long TTL on `.m4s` and `.ts`, short on live manifests. Monitor hit ratio and R2 Class B ops.
|
||||
|
||||
### Video Normalization & Playout
|
||||
|
||||
All source assets must be pre-normalized (H.264/AVC, AAC-LC, constant 30 fps, 2s keyframe interval, CRF). Prevents glitches during continuous concat. FFmpeg `-c copy` preferred for efficiency.
|
||||
All source assets must be pre-normalized before they are accepted into production playlists. This prevents decode resets, timestamp drift, and audio/video discontinuities during continuous concat playout.
|
||||
|
||||
#### Required Source Profile (Linear Void v1)
|
||||
|
||||
| Field | Required Value | Notes |
|
||||
| ----------------------- | ---------------------------------- | ------------------------------------------------------- |
|
||||
| Container | MP4 | Single-video-asset input format |
|
||||
| Video codec | H.264/AVC (`libx264`) | Baseline delivery codec for broad compatibility |
|
||||
| Audio codec | AAC-LC | Stereo preferred |
|
||||
| Resolution | 1280x720 or 1920x1080 | Keep single resolution per playlist block when possible |
|
||||
| Framerate | Constant 30 fps (`-r 30`) | Variable frame rate content must be normalized |
|
||||
| GOP / keyframe interval | 2 seconds (`-g 60 -keyint_min 60`) | Matches HLS segment boundaries better |
|
||||
| Pixel format | `yuv420p` | Browser compatibility baseline |
|
||||
| Audio sample rate | 48 kHz | Stable timing for concat playout |
|
||||
| Audio bitrate | 128k-192k | Predictable quality envelope |
|
||||
|
||||
#### Reference Normalization Command
|
||||
|
||||
```bash
|
||||
ffmpeg -i input.mov \
|
||||
-c:v libx264 -preset medium -crf 21 \
|
||||
-r 30 -g 60 -keyint_min 60 -sc_threshold 0 \
|
||||
-pix_fmt yuv420p \
|
||||
-c:a aac -b:a 160k -ar 48000 -ac 2 \
|
||||
-movflags +faststart \
|
||||
output-normalized.mp4
|
||||
```
|
||||
|
||||
#### Verification Gate (ffprobe)
|
||||
|
||||
- Validate codec, fps, resolution, and keyframe cadence before upload acceptance.
|
||||
- Reject non-conforming files from admin ingest with actionable error feedback.
|
||||
- Keep verification logic aligned with runtime playout assumptions to preserve `-c copy` efficiency where possible.
|
||||
|
||||
### Monitoring & Resilience
|
||||
|
||||
Alert on no new segment (>30–60s), low cache hits, high R2 ops, WebSocket errors. Maintain emergency slate, auto-restart, weekly backups, incident runbook.
|
||||
Linear Void uses a two-layer monitoring model:
|
||||
|
||||
1. **Application layer (chat + platform health)** via Prometheus metrics and a lightweight Grafana dashboard.
|
||||
2. **Infrastructure/runtime layer (Prometheus self-observability, TSDB, query engine, Go runtime)** via a detailed Grafana dashboard.
|
||||
|
||||
#### Prometheus Baseline
|
||||
|
||||
- Config file: `monitoring/prometheus/prometheus.yml`
|
||||
- Default scrape interval: `15s`
|
||||
- Current scrape jobs (7 total):
|
||||
- `chat` -> `chat:3000`
|
||||
- `playout` -> `playout:8081`
|
||||
- `nginx` -> `nginx-exporter:9113`
|
||||
- `redis` -> `redis-exporter:9121`
|
||||
- `postgres` -> `postgres-exporter:9187`
|
||||
- `node` -> `node-exporter:9100`
|
||||
- `prometheus` -> `localhost:9090`
|
||||
|
||||
#### Grafana Dashboards
|
||||
|
||||
- `monitoring/grafana/dashboard.json` (Linear Void app metrics: connected clients, messages, moderation actions, Redis reachability, admin sessions, scrape health).
|
||||
- `monitoring/grafana/dashboards/dashboard.json` (Prometheus operational visibility: status, uptime, CPU/memory, TSDB ingestion/churn/errors, HTTP latencies, query load, GC/scheduler).
|
||||
|
||||
Use the app dashboard for service-level behavior and the Prometheus overview dashboard for platform diagnostics and capacity planning.
|
||||
|
||||
#### Alerting Rules (Prometheus Rule Groups)
|
||||
|
||||
Rules are implemented in `monitoring/prometheus/alerts.yml` and loaded via `rule_files` from `prometheus.yml`.
|
||||
|
||||
Current rule set (8):
|
||||
|
||||
- `ChatDown`
|
||||
- `PlayoutDown`
|
||||
- `RedisDown`
|
||||
- `PlayoutSlateActive`
|
||||
- `HighChatLatency`
|
||||
- `PrometheusWALFailing`
|
||||
- `HighCPU`
|
||||
- `LowDiskSpace`
|
||||
|
||||
Route warning/critical severities to the operations channel and keep runbook links attached to each alert definition.
|
||||
|
||||
Operational resilience baseline:
|
||||
|
||||
- Emergency slate fallback for playout failure.
|
||||
- Auto-restart policies for playout/chat/nginx containers.
|
||||
- Weekly backup and restore drills for stateful data (`data/chat`, playlist metadata, Postgres).
|
||||
- Incident runbook with triage order: edge reachability -> origin health -> playout progression -> chat connectivity -> data-plane dependencies.
|
||||
|
||||
### API Documentation Reference
|
||||
|
||||
Linear Void currently exposes three operational API surfaces. The canonical detailed contract should live in `API.md`, while this section provides the architecture-level map.
|
||||
|
||||
#### Chat WebSocket API
|
||||
|
||||
- Endpoint: `/ws`
|
||||
- Transport: `wss://` in production, `ws://` locally
|
||||
- Query parameters:
|
||||
- `username`
|
||||
- `room`
|
||||
- Message envelope (typical):
|
||||
- `room`
|
||||
- `username`
|
||||
- `text`
|
||||
- `type` (`public`, `private`, `system`)
|
||||
- Responsibilities:
|
||||
- room fan-out
|
||||
- private DM channel routing
|
||||
- moderation command handling
|
||||
- rate limiting and presence integration
|
||||
|
||||
#### Playout and Delivery REST API
|
||||
|
||||
- Health endpoints:
|
||||
- playout health (`/health` on playout service)
|
||||
- edge/origin health (`/health` via nginx)
|
||||
- Stream delivery:
|
||||
- HLS manifest and segment path namespace under `/hls/`
|
||||
- Responsibilities:
|
||||
- expose liveness for orchestration and monitoring
|
||||
- provide stable HLS path contract for viewer and CDN
|
||||
|
||||
#### Admin REST API
|
||||
|
||||
- Auth/session:
|
||||
- `/api/admin/login`
|
||||
- `/api/admin/logout`
|
||||
- `/api/admin/session`
|
||||
- Operational/admin domains:
|
||||
- overview, analytics, users, moderation, schedule, playlist, media
|
||||
- Media upload domain:
|
||||
- `/api/admin/media/uploads` (init/resume)
|
||||
- `/api/admin/media/uploads/{uploadId}/chunk`
|
||||
- `/api/admin/media/uploads/{uploadId}/complete`
|
||||
- Responsibilities:
|
||||
- protected control plane for operator workflows
|
||||
- media metadata + upload orchestration
|
||||
- moderation/audit and system status management
|
||||
|
||||
### Cloudflare Configuration Reference
|
||||
|
||||
Cloudflare is part of the production control plane, not only a CDN edge. The following configuration set is required to maintain cost, latency, and reliability targets.
|
||||
|
||||
#### DNS and Zone Routing
|
||||
|
||||
- `linearvoid.tv` -> application origin (nginx)
|
||||
- `www.linearvoid.tv` -> redirect to apex
|
||||
- `media.linearvoid.tv` -> media domain for HLS/object delivery when split-domain mode is enabled
|
||||
|
||||
#### Cache Rules Baseline
|
||||
|
||||
- HLS segments (`.ts`, `.m4s`):
|
||||
- Cache Everything
|
||||
- long TTL (immutable)
|
||||
- respect immutable semantics
|
||||
- Manifests (`.m3u8`):
|
||||
- short TTL (1-10s)
|
||||
- revalidation-friendly policy for timeline freshness
|
||||
|
||||
#### Edge Security and Controls
|
||||
|
||||
- Enforce HTTPS/WSS and redirect HTTP to HTTPS.
|
||||
- Enable WAF baseline protections for common web exploits.
|
||||
- Apply rate limits for abusive request patterns (especially auth/admin paths).
|
||||
- Keep origin exposure minimized (prefer Cloudflare-only public ingress where feasible).
|
||||
|
||||
#### Worker and Header Policy
|
||||
|
||||
- Use Worker logic for immutable cache headers and request normalization where cache rules alone are insufficient.
|
||||
- Keep CORS behavior explicit for HLS resources to support browser playback.
|
||||
|
||||
#### Operations and Validation
|
||||
|
||||
- Validate cache behavior after deployment:
|
||||
- expected cache status progression (MISS -> HIT)
|
||||
- manifest freshness under active playout
|
||||
- Track edge metrics and adjust:
|
||||
- cache hit ratio
|
||||
- request volume and error rates
|
||||
- ToS/cost posture for sustained traffic profiles
|
||||
|
||||
### Security & Moderation
|
||||
|
||||
HTTPS/WSS only, strict CORS, Cloudflare WAF, authenticated admin paths, rate limiting, immutable moderation log. Private DMs not E2EE by default.
|
||||
Security is split between edge controls (Cloudflare), reverse proxy controls (nginx), and application controls (chat/admin backend).
|
||||
|
||||
#### Transport and Edge Protection
|
||||
|
||||
- Public traffic uses HTTPS/WSS at the edge.
|
||||
- Cloudflare WAF and rate limiting are part of the baseline (see `infrastructure/cloudflare/cache-rules.md`).
|
||||
- Cloudflare Worker can enforce immutable cache headers for segment paths (`infrastructure/cloudflare/worker.js`).
|
||||
|
||||
#### Reverse Proxy Enforcement (nginx)
|
||||
|
||||
- Admin UI and admin API paths are protected by `auth_request` before reaching backend handlers (`/admin/`, `/api/admin/`).
|
||||
- Unauthorized admin UI requests are redirected to `/admin/login/`.
|
||||
- Unauthorized admin API requests return JSON `401` from a dedicated location handler.
|
||||
- WebSocket upgrade headers are explicitly forwarded for `/ws`.
|
||||
|
||||
#### Admin Authentication and Session Security
|
||||
|
||||
- Admin authentication is cookie-session based (`linearvoid_admin_session`).
|
||||
- Session cookie controls in backend include `HttpOnly`, `SameSite=Strict`, and `Secure` when request is HTTPS (derived from TLS or `X-Forwarded-Proto`).
|
||||
- Session expiration is enforced server-side.
|
||||
|
||||
#### Rate Limiting and Abuse Controls
|
||||
|
||||
- Chat message rate limiting uses Redis `INCR + EXPIRE` windowing.
|
||||
- Current baseline in chat handler: `>10` messages within `10s` triggers a user-facing throttle response.
|
||||
- If Redis is unavailable, presence/rate-limit features degrade gracefully (warning logged; core chat remains available).
|
||||
|
||||
#### CORS and Cross-Origin Behavior
|
||||
|
||||
- HLS endpoints exposed by nginx include explicit `Access-Control-Allow-Origin: *` for player compatibility.
|
||||
- Admin/authenticated routes are expected to run same-origin behind nginx auth gates.
|
||||
|
||||
#### Moderation and Privacy
|
||||
|
||||
- Moderation commands (`/mod`, `/mute`, `/ban`) are handled in backend chat flow and persisted through chat storage paths.
|
||||
- Private DMs are application-level private channels, not end-to-end encrypted in current architecture.
|
||||
- Moderation and audit retention policies should be formalized in runbook/ops docs as part of production hardening.
|
||||
|
||||
### Chat
|
||||
|
||||
@@ -354,6 +733,147 @@ These ADRs together form the canonical decision log.
|
||||
|
||||
See **Quality Goals** table in the Introduction section. Primary scenarios focus on cost (near-zero egress), availability (auto-restart + fallback), scalability (edge caching), and usability (synchronized player + responsive chat).
|
||||
|
||||
### Performance Benchmarks and Capacity Planning
|
||||
|
||||
#### Baseline SLO Targets (v1)
|
||||
|
||||
- Playback startup: target < 4s after first manifest request on warm cache.
|
||||
- Steady-state playback: no segment starvation during normal operation.
|
||||
- End-to-end HLS latency: expected 15-35s (standard HLS trade-off).
|
||||
- Chat delivery latency: p95 < 500ms inside same region under normal load.
|
||||
- Admin page responsiveness: p95 API response < 1s for common read operations.
|
||||
|
||||
#### Workload Assumptions
|
||||
|
||||
- Primary audience in US; origin in US-East.
|
||||
- Edge cache serves most segment traffic; origin handles manifest churn and cache misses.
|
||||
- Chat traffic scales with concurrent viewers but remains low-bandwidth compared to video.
|
||||
|
||||
#### Initial Capacity Model
|
||||
|
||||
- Video bitrate envelopes (from ADR-001):
|
||||
- 720p30: ~2.6-4.2 Mbps total with audio.
|
||||
- 1080p30: ~5.1-8.2 Mbps total with audio.
|
||||
- Storage planning for 48h rolling library:
|
||||
- 720p30: ~57-90 GB raw (allocate ~80-110 GB with headroom).
|
||||
- 1080p30: ~110-175 GB raw (allocate ~130-180 GB with headroom).
|
||||
- Cost behavior:
|
||||
- Viewer scale is mostly constrained by cache efficiency and request volume, not by direct origin egress.
|
||||
|
||||
#### Benchmark Plan
|
||||
|
||||
- Playback path:
|
||||
- Measure time-to-first-frame and rebuffer events from multiple US regions.
|
||||
- Track manifest freshness and segment availability at edge.
|
||||
- Chat path:
|
||||
- Load-test concurrent websocket connections and message fan-out.
|
||||
- Measure p50/p95 send-to-receive latency and Redis fallback behavior.
|
||||
- Admin/API path:
|
||||
- Benchmark playlist/system endpoints for p95 latency and error rate.
|
||||
- Platform path:
|
||||
- Observe TSDB ingestion, query p99, and GC pause behavior in Grafana.
|
||||
|
||||
#### Capacity Triggers and Actions
|
||||
|
||||
- Trigger: sustained viewer growth with rising cache-miss ratio.
|
||||
- Action: tighten cache policy, enable/verify Tiered Cache, review manifest TTL.
|
||||
- Trigger: chat p95 latency drift or websocket error growth.
|
||||
- Action: scale chat service, tune Redis, separate noisy moderation workloads.
|
||||
- Trigger: Prometheus WAL/compaction failures or query saturation.
|
||||
- Action: tune retention/cardinality, increase resources, split dashboards/queries.
|
||||
- Trigger: origin CPU/memory pressure on playout host.
|
||||
- Action: optimize FFmpeg profile, isolate services, add secondary origin.
|
||||
|
||||
### Scaling Strategy
|
||||
|
||||
Scaling is designed to preserve cost efficiency first, then increase throughput and resilience in controlled phases.
|
||||
|
||||
#### Phase 1: Vertical + Cache-First (Current Baseline)
|
||||
|
||||
- Single primary origin in US-East with strong Cloudflare edge caching.
|
||||
- Optimize segment immutability and manifest TTL before adding infrastructure.
|
||||
- Scale compute vertically when CPU/memory pressure appears on playout/chat host.
|
||||
|
||||
#### Phase 2: Service Separation
|
||||
|
||||
- Split playout, chat, and observability workloads across dedicated nodes/VMs.
|
||||
- Isolate noisy workloads (analytics, heavy admin tasks) from latency-sensitive chat paths.
|
||||
- Introduce managed PostgreSQL/Redis or dedicated state nodes where operationally justified.
|
||||
|
||||
#### Phase 3: Multi-Origin Resilience
|
||||
|
||||
- Add warm standby or active-secondary origin.
|
||||
- Use health-aware DNS/origin failover strategy.
|
||||
- Keep HLS path and cache contract stable so viewer impact during failover is minimized.
|
||||
|
||||
#### Phase 4: Regional and Throughput Expansion
|
||||
|
||||
- Add additional origin region only when audience distribution and latency justify it.
|
||||
- Keep chat affinity strategy explicit (regional routing, state replication considerations).
|
||||
- Continuously evaluate cache miss economics vs additional origin complexity.
|
||||
|
||||
#### Scaling Triggers
|
||||
|
||||
- Rising cache miss ratio with sustained traffic growth.
|
||||
- Chat p95/p99 latency drift or websocket error growth.
|
||||
- Persistent origin resource saturation despite optimization.
|
||||
- Increased blast-radius concerns from single-host dependency.
|
||||
|
||||
### Disaster Recovery and Backup Procedures
|
||||
|
||||
#### Recovery Targets
|
||||
|
||||
- RPO target: <= 24h for chat/moderation/admin metadata.
|
||||
- RTO target: <= 60 minutes for restoring baseline streaming + chat operations.
|
||||
|
||||
#### Backup Scope
|
||||
|
||||
- Persistent service volumes:
|
||||
- Postgres data (`postgres-data` volume).
|
||||
- Redis append-only persistence (`redis-data` volume) where enabled.
|
||||
- Prometheus long-term TSDB (`prometheus-data` volume) if historical metrics retention is required.
|
||||
- Grafana state (`grafana-data` volume) if dashboards/users are managed in-instance.
|
||||
- Host-mounted state directories:
|
||||
- `data/chat`
|
||||
- `data/playlist`
|
||||
- `data/media` (authoritative source may be R2/object storage)
|
||||
- `data/hls` (optional; can be regenerated by playout)
|
||||
- Configuration and infra source of truth:
|
||||
- Compose files, nginx config, monitoring configs, and environment template.
|
||||
|
||||
#### Backup Cadence
|
||||
|
||||
- Daily logical Postgres backups (plus weekly full snapshot).
|
||||
- Daily sync of chat/playlist state directories.
|
||||
- Weekly integrity check by restoring backups into an isolated environment.
|
||||
- Retention policy baseline:
|
||||
- Daily backups: 14 days
|
||||
- Weekly backups: 8 weeks
|
||||
- Monthly backups: 6 months
|
||||
|
||||
#### Recovery Runbook (Minimum)
|
||||
|
||||
1. Restore infrastructure configuration (compose, nginx, env, monitoring config).
|
||||
2. Restore Postgres and Redis persisted state.
|
||||
3. Restore `data/playlist` and chat persistence directories.
|
||||
4. Rehydrate media from authoritative object storage if local media is missing.
|
||||
5. Start stack and validate in order:
|
||||
- `playout` health endpoint
|
||||
- `chat` health endpoint
|
||||
- nginx `/health`
|
||||
- HLS playlist availability
|
||||
- websocket chat connectivity
|
||||
6. Verify admin authentication and moderation audit visibility.
|
||||
7. Announce recovery and continue elevated monitoring for at least one playlist cycle.
|
||||
|
||||
#### Failure Modes and Strategy
|
||||
|
||||
- Single-host loss: redeploy stack on standby host, restore data backups, repoint DNS/edge origin.
|
||||
- Storage corruption: restore last known-good snapshot and replay latest logical backups.
|
||||
- Partial service failure: replace only impacted service and restore scoped data (for example Postgres only).
|
||||
|
||||
Document and rehearse this runbook quarterly; recovery that is not exercised is not reliable.
|
||||
|
||||
## Risks and Technical Debts
|
||||
|
||||
- Cloudflare ToS risk for heavy video on free tier (use R2 path; fallback to BunnyCDN if challenged).
|
||||
@@ -368,175 +888,16 @@ See **Quality Goals** table in the Introduction section. Primary scenarios focus
|
||||
|
||||
## Glossary
|
||||
|
||||
| Term | Definition |
|
||||
| ----------------- | ------------------------------------------------------------------------------------------ |
|
||||
| HLS | HTTP Live Streaming – protocol using `.m3u8` manifests and media segments (`.ts` or fMP4). |
|
||||
| Immutable Segment | HLS chunk with unique, unchanging name enabling long-lived caching. |
|
||||
| R2 | Cloudflare object storage ($0 egress, ideal origin for cached HLS). |
|
||||
| Edge Cache / POP | Cloudflare points-of-presence that cache and serve content close to viewers. |
|
||||
| Playout | Continuous generation of a scheduled linear program from a playlist. |
|
||||
| Normalization | Pre-processing videos to consistent codec/fps/keyframe settings for seamless playback. |
|
||||
| Cache Rule | Cloudflare configuration defining caching behavior, TTL, and headers per URL pattern. |
|
||||
| SPOF | Single Point of Failure (current single VPS origin). |
|
||||
| Tiered Cache | Cloudflare feature reducing origin requests by checking upper-tier caches first. |
|
||||
| Term | Definition |
|
||||
| ----------------- | -------------------------------------------------------------------------------------- |
|
||||
| HLS | HTTP Live Streaming – protocol using `.m3u8` manifests and media segments (fMP4). |
|
||||
| Immutable Segment | HLS chunk with unique, unchanging name enabling long-lived caching. |
|
||||
| R2 | Cloudflare object storage ($0 egress, ideal origin for cached HLS). |
|
||||
| Edge Cache / POP | Cloudflare points-of-presence that cache and serve content close to viewers. |
|
||||
| Playout | Continuous generation of a scheduled linear program from a playlist. |
|
||||
| Normalization | Pre-processing videos to consistent codec/fps/keyframe settings for seamless playback. |
|
||||
| Cache Rule | Cloudflare configuration defining caching behavior, TTL, and headers per URL pattern. |
|
||||
| SPOF | Single Point of Failure (current single VPS origin). |
|
||||
| Tiered Cache | Cloudflare feature reducing origin requests by checking upper-tier caches first. |
|
||||
|
||||
**References**: Cloudflare R2/Cache docs, MediaMTX/SRS/FFmpeg HLS guides, Hetzner pricing, hls.js documentation, standard video bitrate calculators. All original root `ADR-001-*.md` files have been consolidated and can be archived/deleted.
|
||||
|
||||
### Whitebox Overall System
|
||||
|
||||
**_\<Overview Diagram\>_**
|
||||
|
||||
Motivation
|
||||
|
||||
: _\<text explanation\>_
|
||||
|
||||
Contained Building Blocks
|
||||
|
||||
: _\<Description of contained building block (black boxes)\>_
|
||||
|
||||
Important Interfaces
|
||||
|
||||
: _\<Description of important interfaces\>_
|
||||
|
||||
#### \<Name black box 1\>
|
||||
|
||||
_\<Purpose/Responsibility\>_
|
||||
|
||||
_\<Interface(s)\>_
|
||||
|
||||
_\<(Optional) Quality/Performance Characteristics\>_
|
||||
|
||||
_\<(Optional) Directory/File Location\>_
|
||||
|
||||
_\<(Optional) Fulfilled Requirements\>_
|
||||
|
||||
_\<(optional) Open Issues/Problems/Risks\>_
|
||||
|
||||
#### \<Name black box 2\>
|
||||
|
||||
_\<black box template\>_
|
||||
|
||||
#### \<Name black box n\>
|
||||
|
||||
_\<black box template\>_
|
||||
|
||||
#### \<Name interface 1\>
|
||||
|
||||
...
|
||||
|
||||
#### \<Name interface m\>
|
||||
|
||||
### Level 2
|
||||
|
||||
#### White Box _\<building block 1\>_
|
||||
|
||||
_\<white box template\>_
|
||||
|
||||
#### White Box _\<building block 2\>_
|
||||
|
||||
_\<white box template\>_
|
||||
|
||||
...
|
||||
|
||||
#### White Box _\<building block m\>_
|
||||
|
||||
_\<white box template\>_
|
||||
|
||||
### Level 3
|
||||
|
||||
#### White Box \<\_building block x.1\_\>
|
||||
|
||||
_\<white box template\>_
|
||||
|
||||
#### White Box \<\_building block x.2\_\>
|
||||
|
||||
_\<white box template\>_
|
||||
|
||||
#### White Box \<\_building block y.1\_\>
|
||||
|
||||
_\<white box template\>_
|
||||
|
||||
## Runtime View
|
||||
|
||||
### \<Runtime Scenario 1\>
|
||||
|
||||
- _\<insert runtime diagram or textual description of the scenario\>_
|
||||
|
||||
- _\<insert description of the notable aspects of the interactions
|
||||
between the building block instances depicted in this diagram.\>_
|
||||
|
||||
### \<Runtime Scenario 2\>
|
||||
|
||||
### ...
|
||||
|
||||
### \<Runtime Scenario n\>
|
||||
|
||||
## Deployment View
|
||||
|
||||
### Infrastructure Level 1
|
||||
|
||||
**_\<Overview Diagram\>_**
|
||||
|
||||
Motivation
|
||||
|
||||
: _\<explanation in text form\>_
|
||||
|
||||
Quality and/or Performance Features
|
||||
|
||||
: _\<explanation in text form\>_
|
||||
|
||||
Mapping of Building Blocks to Infrastructure
|
||||
|
||||
: _\<description of the mapping\>_
|
||||
|
||||
### Infrastructure Level 2
|
||||
|
||||
#### _\<Infrastructure Element 1\>_
|
||||
|
||||
_\<diagram + explanation\>_
|
||||
|
||||
#### _\<Infrastructure Element 2\>_
|
||||
|
||||
_\<diagram + explanation\>_
|
||||
|
||||
...
|
||||
|
||||
#### _\<Infrastructure Element n\>_
|
||||
|
||||
_\<diagram + explanation\>_
|
||||
|
||||
## Cross-cutting Concepts
|
||||
|
||||
### _\<Concept 1\>_
|
||||
|
||||
_\<explanation\>_
|
||||
|
||||
### _\<Concept 2\>_
|
||||
|
||||
_\<explanation\>_
|
||||
|
||||
...
|
||||
|
||||
### _\<Concept n\>_
|
||||
|
||||
_\<explanation\>_
|
||||
|
||||
## Architecture Decisions
|
||||
|
||||
## Quality Requirements
|
||||
|
||||
### Quality Requirements Overview
|
||||
|
||||
### Quality Scenarios
|
||||
|
||||
## Risks and Technical Debts
|
||||
|
||||
## Glossary
|
||||
|
||||
+----------------------+-----------------------------------------------+
|
||||
| Term | Definition |
|
||||
+======================+===============================================+
|
||||
| _\<Term-1\>_ | _\<definition-1\>_ |
|
||||
+----------------------+-----------------------------------------------+
|
||||
| _\<Term-2\>_ | _\<definition-2\>_ |
|
||||
+----------------------+-----------------------------------------------+
|
||||
@@ -19,11 +19,11 @@ We require a completely independent 24/7 linear video channel that broadcasts a
|
||||
**Key distinctions**:
|
||||
|
||||
- **Playout**: Continuous scheduled program from playlist.
|
||||
- **Packaging**: FFmpeg → standard HLS (`.m3u8` + `.ts` segments, 4–6s duration).
|
||||
- **Packaging**: FFmpeg → standard HLS (`.m3u8` + `.m4s` segments, 4–6s duration).
|
||||
- **Delivery**: Immutable segments served via Cloudflare edge caching + R2 (zero egress cost).
|
||||
- **Interaction**: Separate lightweight WebSocket service for chat.
|
||||
|
||||
Bandwidth (not storage or compute) is the dominant cost driver. A single viewer at ~4 Mbps consumes ~1.3–1.8 TB/month; 100 concurrent viewers = ~130–180 TB/month. Direct VPS delivery is only viable for PoC/small audiences.
|
||||
Bandwidth (not storage or compute) is the dominant cost driver. A single viewer at ~4 Mbps consumes ~1.3–1.8 TB/month; 100 concurrent viewers = ~130–180 TB/month. Direct VPS delivery is only viable for PoC/small audiences. HLS segments are typically `.m4s` files, with `.m4s` being the default for fMP4 segments.
|
||||
|
||||
## Decision Drivers
|
||||
|
||||
@@ -31,7 +31,7 @@ Bandwidth (not storage or compute) is the dominant cost driver. A single viewer
|
||||
2. Full independence and ownership (no video platform lock-in).
|
||||
3. Strong US performance and low latency.
|
||||
4. True/near-true synchronization via live-style HLS (not pure VOD).
|
||||
5. Aggressive immutable caching of `.ts` segments to achieve near-zero egress.
|
||||
5. Aggressive immutable caching of `.m4s` segments to achieve near-zero egress.
|
||||
6. Browser-native playback (`hls.js` + native HLS).
|
||||
7. Lightweight, maintainable stack with modest operational complexity.
|
||||
8. Support for custom multi-room + private chat with persistence and moderation.
|
||||
@@ -58,9 +58,9 @@ Adopt a **self-managed US-East playout + Cloudflare R2 + edge-cached HLS** archi
|
||||
|
||||
### 2. Storage & Delivery (Cloudflare R2 + CDN)
|
||||
|
||||
- HLS segments (`.ts`) and manifests uploaded to **Cloudflare R2** (Eastern North America hint `enam`).
|
||||
- Custom domain (`media.example.com`) fronted by Cloudflare with **Cache Rules**:
|
||||
- `*.ts` (and init `.mp4` if using fMP4 later): `Cache Everything`, immutable, Edge TTL 24h–1yr (`Cache-Control: public, max-age=31536000, immutable`).
|
||||
- HLS segments (`.m4s`) and manifests uploaded to **Cloudflare R2** (Eastern North America hint `enam`).
|
||||
- Custom domain (`media.linearvoid.tv`) fronted by Cloudflare with **Cache Rules**:
|
||||
- `*.m4s` (and init `.mp4` if using fMP4 later): `Cache Everything`, immutable, Edge TTL 24h–1yr (`Cache-Control: public, max-age=31536000, immutable`).
|
||||
- `*.m3u8`: Short TTL (1–10s) or `must-revalidate` (`Cache-Control: public, max-age=1..10, must-revalidate`).
|
||||
- Master playlist: TTL ~30s.
|
||||
- Tiered Cache enabled where possible.
|
||||
@@ -90,7 +90,7 @@ Adopt a **self-managed US-East playout + Cloudflare R2 + edge-cached HLS** archi
|
||||
|
||||
**Initial Targets**:
|
||||
|
||||
- Protocol: Standard HLS (`.ts` segments). Defer LL-HLS/CMAF/WebRTC.
|
||||
- Protocol: Standard HLS (`.m4s` segments). Defer LL-HLS/CMAF/WebRTC.
|
||||
- Max rendition: 720p30 initially (upgrade to 1080p30 or ABR later).
|
||||
- Latency: 15–35s (acceptable for linear programming/chat).
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ We must explicitly document language/runtime choices per module and the assumpti
|
||||
|
||||
1. All video assets **must** be normalized before being added to any playlist (H.264, AAC, 30 fps constant, 2s keyframes, CRF encoding).
|
||||
2. Deployment is **Docker-first**; services will be containerized.
|
||||
3. Cloudflare is the primary CDN and will be configured with specific Cache Rules (`.ts` immutable long TTL, `.m3u8` short TTL).
|
||||
3. Cloudflare is the primary CDN and will be configured with specific [Cache Rules](../../../infrastructure/cloudflare/cache-rules.md).
|
||||
4. Chat does **not** require end-to-end encryption in v1 (separate ADR if needed).
|
||||
5. Single VPS origin initially; multi-region is a later evolution.
|
||||
6. Monitoring focuses on playout health, cache hit ratio, and R2 operation counts.
|
||||
|
||||
@@ -60,8 +60,6 @@ Create a dedicated **admin web interface** in a new `frontend/admin/` directory
|
||||
- Integration with existing playout service and R2 keeps the architecture consistent.
|
||||
- Admin actions will be fully auditable, paving the way for ADR-005 (moderation and user management).
|
||||
|
||||
This decision was planned in `TODO.md` before implementation, per AGENTS.md.
|
||||
|
||||
## Consequences
|
||||
|
||||
**Positive**:
|
||||
|
||||
@@ -59,8 +59,6 @@ Extend the admin interface started in ADR-004 with additional protected sections
|
||||
- Leveraging Prometheus/Grafana for analytics avoids duplicating metrics collection.
|
||||
- Immutable audit logging supports future compliance and moderation review (key requirement for a public chat system).
|
||||
|
||||
This ADR was planned in `TODO.md` before any implementation, per AGENTS.md.
|
||||
|
||||
## Consequences
|
||||
|
||||
**Positive**:
|
||||
@@ -92,8 +90,6 @@ This ADR was planned in `TODO.md` before any implementation, per AGENTS.md.
|
||||
7. Update authentication middleware and tests.
|
||||
8. Document API contracts and update `docker-compose.yml` to expose the admin interface securely.
|
||||
|
||||
**Next Steps**: After both admin ADRs are complete, move to implementation of the Playout Service (highest priority in the core modules section of TODO.md).
|
||||
|
||||
## References
|
||||
|
||||
- ADR-001: Chat features, moderation requirements, monitoring needs.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# ADR-006: Data Storage Strategy (SQLite vs PostgreSQL)
|
||||
|
||||
**Status:** Proposed
|
||||
**Date:** 2026-08-02
|
||||
**Status:** Accepted
|
||||
**Date:** 2026-08-04
|
||||
**Author:** AI Assistant (following AGENTS.md, ADR-002, ADR-003, ADR-004, ADR-005)
|
||||
**Related ADRs:** [ADR-001](./ADR-001.md), [ADR-003](./ADR-003-tech-stack.md), [ADR-005](./ADR-005-admin-moderation-analytics.md), [ADR-007](./ADR-007-redis-usage.md)
|
||||
|
||||
@@ -12,89 +12,78 @@ The system needs persistent storage for:
|
||||
- Chat messages and history (public rooms and private DMs).
|
||||
- User accounts, roles, and ban lists.
|
||||
- Moderation audit log (immutable record of admin and moderator actions).
|
||||
- Playlist metadata and schedule overrides.
|
||||
- System configuration (including Redis settings adjustable via admin UI per ADR-005/ADR-007).
|
||||
- Playlist and media metadata used by admin flows.
|
||||
- System configuration visibility in admin APIs.
|
||||
|
||||
From ADR-003 we prefer operational simplicity. ADR-005 requires a robust audit trail. We must decide between **SQLite** (embedded, zero-config) and **PostgreSQL** (full-featured, scalable) and document how the admin interface can view and adjust storage-related configuration.
|
||||
The codebase now ships with a PostgreSQL service in Docker Compose (`db`) and chat is configured with a PostgreSQL `DATABASE_URL` by default. The backend still supports SQLite for optional local fallback, but the deployed and compose-default path is PostgreSQL.
|
||||
|
||||
## Decision Drivers
|
||||
|
||||
1. **Operational Simplicity** – Minimize infrastructure (no separate DB server for SQLite).
|
||||
2. **Performance & Concurrency** – Chat and moderation may have high write volume.
|
||||
3. **Backup and Recovery** – Easy point-in-time recovery and replication.
|
||||
4. **Admin Capabilities** – Admin UI must be able to view storage stats, run vacuum/analyze, and switch between SQLite and PostgreSQL (with migration path).
|
||||
5. **Auditability** – Moderation log must be immutable and queryable.
|
||||
6. **Cost** – Avoid managed database costs during early stages.
|
||||
1. **Implemented Reality** - Docker Compose already provisions PostgreSQL and chat depends on it.
|
||||
2. **Concurrency & Throughput** - Chat, moderation, and media metadata writes require stronger concurrent write behavior.
|
||||
3. **Operational Traceability** - Structured backups, service health checks, and persistent volume management are already in place for PostgreSQL.
|
||||
4. **Admin Growth** - Upcoming admin features (media CRUD, analytics, moderation tooling) benefit from relational querying and indexing.
|
||||
5. **Fallback Simplicity** - Keep SQLite support for lightweight local or recovery scenarios without blocking developer workflows.
|
||||
|
||||
## Decision
|
||||
|
||||
**Primary storage for v1: SQLite** (embedded in the chat and admin services).
|
||||
**Primary storage for current implementation: PostgreSQL (Docker Compose `db` service).**
|
||||
|
||||
**Migration Path**: Provide a clear upgrade path to PostgreSQL when concurrency or replication needs exceed SQLite capabilities (target v2 or when concurrent users > 500).
|
||||
**Secondary/Fallback mode:** SQLite remains supported via `DATABASE_URL=sqlite://...` for local-only scenarios.
|
||||
|
||||
**Admin Interface Capabilities** (extension of ADR-005):
|
||||
**Current Backend Behavior**:
|
||||
|
||||
- "System Configuration" tab showing current storage engine, database size, connection stats, and vacuum status.
|
||||
- Buttons to run `VACUUM`, `ANALYZE`, or trigger a backup.
|
||||
- Configuration form to switch connection string (SQLite file path vs PostgreSQL URL) and restart services gracefully.
|
||||
- Audit log of all configuration changes.
|
||||
- Chat backend (`backend/chat/src/main.go`) resolves `DATABASE_URL` at startup.
|
||||
- `postgres://` or `postgresql://` selects PostgreSQL driver.
|
||||
- `sqlite://` selects SQLite driver.
|
||||
- Empty `DATABASE_URL` falls back to local `chat.db` for developer convenience.
|
||||
- GORM AutoMigrate is used for chat/admin data models.
|
||||
|
||||
**Technical Details**:
|
||||
**Media Model Decision (Admin UI Feature Track)**:
|
||||
|
||||
- Use SQLite with WAL mode for better concurrency.
|
||||
- Use GORM or sqlx (Go) or Prisma (if Node.js fallback) for ORM/migrations.
|
||||
- Store large chat history in a separate table with retention policy (e.g., 30 days).
|
||||
- Moderation log as an append-only table with triggers for immutability.
|
||||
|
||||
**Folder Placement** (per ADR-002):
|
||||
|
||||
- Database models and migrations in `backend/shared/db/` or `backend/chat/internal/db/`.
|
||||
- Admin UI pages for storage management in `frontend/admin/system/`.
|
||||
- Media metadata is persisted in the same database as messages and moderation logs.
|
||||
- Physical media files remain on the shared filesystem (`/app/media`, mapped from `data/media`).
|
||||
- The database is the source of truth for media library metadata and future runtime analytics.
|
||||
|
||||
## Rationale
|
||||
|
||||
- SQLite aligns with ADR-003's emphasis on operational simplicity and low resource usage for the initial deployment on a single Hetzner VPS.
|
||||
- It eliminates the need for a separate database container in `docker-compose.yml` during early development.
|
||||
- The admin interface can provide visibility and basic maintenance operations, fulfilling the requirement from the previous TODO plan.
|
||||
- PostgreSQL can be introduced later with a migration script when scale demands it (replication, advanced querying, full-text search on chat).
|
||||
- The repository already operationalizes PostgreSQL in both `docker-compose.yml` and `docker-compose.prod.yml` with persistent storage.
|
||||
- Default environment documentation (`.env.example`, setup docs) already points to PostgreSQL.
|
||||
- This aligns architecture documentation with implemented behavior and avoids drift.
|
||||
- Keeping SQLite support preserves local flexibility while avoiding a hard breaking change.
|
||||
|
||||
## Consequences
|
||||
|
||||
**Positive**:
|
||||
|
||||
- Zero-config database for quick local development and small-scale production.
|
||||
- Simple backups (just copy the `.db` file).
|
||||
- Admin UI can directly manage and monitor storage without external tools.
|
||||
- Better write concurrency and query flexibility for chat + admin workloads.
|
||||
- Clear operational model for containerized deployments (health checks, named volume, service dependency).
|
||||
- Easier extension for admin analytics and media metadata features.
|
||||
|
||||
**Negative / Risks**:
|
||||
|
||||
- SQLite has limited concurrency compared to PostgreSQL (WAL mode helps but is not a full replacement).
|
||||
- Large chat history or analytics queries may become slow – plan retention policy and archiving.
|
||||
- Migration to PostgreSQL will require downtime or dual-write strategy (documented in this ADR).
|
||||
- Additional service to operate versus embedded-only SQLite.
|
||||
- Misconfigured `DATABASE_URL` can still route to SQLite fallback unintentionally if left empty outside compose.
|
||||
- Requires backup/restore procedures for PostgreSQL volume in production runbooks.
|
||||
|
||||
**Alternatives Considered**:
|
||||
## Alternatives Considered
|
||||
|
||||
- PostgreSQL from day one – rejected for added operational complexity (separate service, connection pooling, managed service cost).
|
||||
- Pure key-value store (e.g., BoltDB) – rejected because SQL is better for audit logs and complex moderation queries.
|
||||
- Managed database (Neon, Supabase) – rejected to maintain full self-hosted independence per ADR-001.
|
||||
- SQLite-first in production: rejected for current implementation because compose and deployment already run PostgreSQL.
|
||||
- PostgreSQL-only with no fallback: rejected to preserve local dev and troubleshooting flexibility.
|
||||
- Managed external PostgreSQL: deferred; self-hosted compose remains the preferred operational model.
|
||||
|
||||
## Implementation Plan
|
||||
## Implementation Status
|
||||
|
||||
1. Create this ADR-006 (done).
|
||||
2. Add SQLite support in `backend/shared/db/` with GORM migrations.
|
||||
3. Implement admin UI pages for storage monitoring and configuration (linked to ADR-005).
|
||||
4. Add configuration reload capability without full restart where possible.
|
||||
5. Write migration script from SQLite to PostgreSQL.
|
||||
6. Update `docker-compose.yml` to optionally include PostgreSQL.
|
||||
7. Add tests for data access patterns and admin configuration changes.
|
||||
|
||||
**Next ADR**: ADR-007 (Redis usage and admin configuration capabilities).
|
||||
1. ADR updated to match implemented state. Done.
|
||||
2. Compose includes PostgreSQL service and persistent volume. Done.
|
||||
3. Chat backend supports PostgreSQL and SQLite based on `DATABASE_URL`. Done.
|
||||
4. Media metadata persistence model documented here for Admin UI track. In progress (implementation ongoing).
|
||||
5. Admin storage observability and operations UI (vacuum/analyze/backup flows). Planned.
|
||||
|
||||
## References
|
||||
|
||||
- ADR-001: Chat persistence, moderation log, monitoring requirements.
|
||||
- ADR-003: Preference for operational simplicity and Go backend.
|
||||
- ADR-005: Admin interface for user management, moderation, and analytics – this ADR extends it with storage and Redis config capabilities.
|
||||
- arc42 documentation: Cross-cutting Concepts (data management, monitoring) and Risks & Technical Debts.
|
||||
|
||||
This ADR ensures all data storage decisions are documented before code is written, maintaining full traceability.
|
||||
- ADR-005: Admin interface for user management, moderation, and analytics.
|
||||
- ADR-007: Redis usage for presence, rate limiting, pub/sub.
|
||||
- `docker-compose.yml` and `docker-compose.prod.yml` database service definitions.
|
||||
@@ -95,8 +95,6 @@ We must explicitly document:
|
||||
6. Add monitoring (Prometheus exporter) and admin audit logging for configuration changes.
|
||||
7. Write tests for rate limiting, presence, and admin configuration flows.
|
||||
|
||||
**Next Steps**: After ADR-006 and ADR-007 are reviewed, mark the "Create ADRs for Data Storage, Caching, and Admin Capabilities" section as completed in TODO.md and move it to DONE.md. Then proceed with implementation of the Playout Service.
|
||||
|
||||
## References
|
||||
|
||||
- ADR-001: Chat features requiring presence, rate limiting, and pub/sub.
|
||||
|
||||
@@ -24,13 +24,13 @@ contributors. See <https://arc42.org>.
|
||||
## Stakeholders {#_stakeholders}
|
||||
|
||||
+-------------+---------------------------+---------------------------+
|
||||
| Role/Name | Contact | Expectations |
|
||||
| Role/Name | Contact | Expectations |
|
||||
+=============+===========================+===========================+
|
||||
| * | *\<Contact-1\>* | *\<Expectation-1\>* |
|
||||
| \<Role-1\>* | | |
|
||||
| * | _\<Contact-1\>_ | _\<Expectation-1\>_ |
|
||||
| \<Role-1\>* | | |
|
||||
+-------------+---------------------------+---------------------------+
|
||||
| * | *\<Contact-2\>* | *\<Expectation-2\>* |
|
||||
| \<Role-2\>* | | |
|
||||
| * | _\<Contact-2\>_ | _\<Expectation-2\>_ |
|
||||
| \<Role-2\>* | | |
|
||||
+-------------+---------------------------+---------------------------+
|
||||
|
||||
# Architecture Constraints {#section-architecture-constraints}
|
||||
@@ -57,41 +57,41 @@ contributors. See <https://arc42.org>.
|
||||
|
||||
## Whitebox Overall System {#_whitebox_overall_system}
|
||||
|
||||
***\<Overview Diagram\>***
|
||||
_**\<Overview Diagram\>**_
|
||||
|
||||
Motivation
|
||||
|
||||
: *\<text explanation\>*
|
||||
: _\<text explanation\>_
|
||||
|
||||
Contained Building Blocks
|
||||
|
||||
: *\<Description of contained building block (black boxes)\>*
|
||||
: _\<Description of contained building block (black boxes)\>_
|
||||
|
||||
Important Interfaces
|
||||
|
||||
: *\<Description of important interfaces\>*
|
||||
: _\<Description of important interfaces\>_
|
||||
|
||||
### \<Name black box 1\> {#_name_black_box_1}
|
||||
|
||||
*\<Purpose/Responsibility\>*
|
||||
_\<Purpose/Responsibility\>_
|
||||
|
||||
*\<Interface(s)\>*
|
||||
_\<Interface(s)\>_
|
||||
|
||||
*\<(Optional) Quality/Performance Characteristics\>*
|
||||
_\<(Optional) Quality/Performance Characteristics\>_
|
||||
|
||||
*\<(Optional) Directory/File Location\>*
|
||||
_\<(Optional) Directory/File Location\>_
|
||||
|
||||
*\<(Optional) Fulfilled Requirements\>*
|
||||
_\<(Optional) Fulfilled Requirements\>_
|
||||
|
||||
*\<(optional) Open Issues/Problems/Risks\>*
|
||||
_\<(optional) Open Issues/Problems/Risks\>_
|
||||
|
||||
### \<Name black box 2\> {#_name_black_box_2}
|
||||
|
||||
*\<black box template\>*
|
||||
_\<black box template\>_
|
||||
|
||||
### \<Name black box n\> {#_name_black_box_n}
|
||||
|
||||
*\<black box template\>*
|
||||
_\<black box template\>_
|
||||
|
||||
### \<Name interface 1\> {#_name_interface_1}
|
||||
|
||||
@@ -101,42 +101,42 @@ Important Interfaces
|
||||
|
||||
## Level 2 {#_level_2}
|
||||
|
||||
### White Box *\<building block 1\>* {#_white_box_building_block_1}
|
||||
### White Box _\<building block 1\>_ {#_white_box_building_block_1}
|
||||
|
||||
*\<white box template\>*
|
||||
_\<white box template\>_
|
||||
|
||||
### White Box *\<building block 2\>* {#_white_box_building_block_2}
|
||||
### White Box _\<building block 2\>_ {#_white_box_building_block_2}
|
||||
|
||||
*\<white box template\>*
|
||||
_\<white box template\>_
|
||||
|
||||
...
|
||||
|
||||
### White Box *\<building block m\>* {#_white_box_building_block_m}
|
||||
### White Box _\<building block m\>_ {#_white_box_building_block_m}
|
||||
|
||||
*\<white box template\>*
|
||||
_\<white box template\>_
|
||||
|
||||
## Level 3 {#_level_3}
|
||||
|
||||
### White Box \<\_building block x.1\_\> {#_white_box_building_block_x_1}
|
||||
|
||||
*\<white box template\>*
|
||||
_\<white box template\>_
|
||||
|
||||
### White Box \<\_building block x.2\_\> {#_white_box_building_block_x_2}
|
||||
|
||||
*\<white box template\>*
|
||||
_\<white box template\>_
|
||||
|
||||
### White Box \<\_building block y.1\_\> {#_white_box_building_block_y_1}
|
||||
|
||||
*\<white box template\>*
|
||||
_\<white box template\>_
|
||||
|
||||
# Runtime View {#section-runtime-view}
|
||||
|
||||
## \<Runtime Scenario 1\> {#_runtime_scenario_1}
|
||||
|
||||
- *\<insert runtime diagram or textual description of the scenario\>*
|
||||
- _\<insert runtime diagram or textual description of the scenario\>_
|
||||
|
||||
- *\<insert description of the notable aspects of the interactions
|
||||
between the building block instances depicted in this diagram.\>*
|
||||
- _\<insert description of the notable aspects of the interactions
|
||||
between the building block instances depicted in this diagram.\>_
|
||||
|
||||
## \<Runtime Scenario 2\> {#_runtime_scenario_2}
|
||||
|
||||
@@ -148,51 +148,51 @@ Important Interfaces
|
||||
|
||||
## Infrastructure Level 1 {#_infrastructure_level_1}
|
||||
|
||||
***\<Overview Diagram\>***
|
||||
_**\<Overview Diagram\>**_
|
||||
|
||||
Motivation
|
||||
|
||||
: *\<explanation in text form\>*
|
||||
: _\<explanation in text form\>_
|
||||
|
||||
Quality and/or Performance Features
|
||||
|
||||
: *\<explanation in text form\>*
|
||||
: _\<explanation in text form\>_
|
||||
|
||||
Mapping of Building Blocks to Infrastructure
|
||||
|
||||
: *\<description of the mapping\>*
|
||||
: _\<description of the mapping\>_
|
||||
|
||||
## Infrastructure Level 2 {#_infrastructure_level_2}
|
||||
|
||||
### *\<Infrastructure Element 1\>* {#_infrastructure_element_1}
|
||||
### _\<Infrastructure Element 1\>_ {#_infrastructure_element_1}
|
||||
|
||||
*\<diagram + explanation\>*
|
||||
_\<diagram + explanation\>_
|
||||
|
||||
### *\<Infrastructure Element 2\>* {#_infrastructure_element_2}
|
||||
### _\<Infrastructure Element 2\>_ {#_infrastructure_element_2}
|
||||
|
||||
*\<diagram + explanation\>*
|
||||
_\<diagram + explanation\>_
|
||||
|
||||
...
|
||||
|
||||
### *\<Infrastructure Element n\>* {#_infrastructure_element_n}
|
||||
### _\<Infrastructure Element n\>_ {#_infrastructure_element_n}
|
||||
|
||||
*\<diagram + explanation\>*
|
||||
_\<diagram + explanation\>_
|
||||
|
||||
# Cross-cutting Concepts {#section-concepts}
|
||||
|
||||
## *\<Concept 1\>* {#_concept_1}
|
||||
## _\<Concept 1\>_ {#_concept_1}
|
||||
|
||||
*\<explanation\>*
|
||||
_\<explanation\>_
|
||||
|
||||
## *\<Concept 2\>* {#_concept_2}
|
||||
## _\<Concept 2\>_ {#_concept_2}
|
||||
|
||||
*\<explanation\>*
|
||||
_\<explanation\>_
|
||||
|
||||
...
|
||||
|
||||
## *\<Concept n\>* {#_concept_n}
|
||||
## _\<Concept n\>_ {#_concept_n}
|
||||
|
||||
*\<explanation\>*
|
||||
_\<explanation\>_
|
||||
|
||||
# Architecture Decisions {#section-design-decisions}
|
||||
|
||||
@@ -207,9 +207,9 @@ Mapping of Building Blocks to Infrastructure
|
||||
# Glossary {#section-glossary}
|
||||
|
||||
+----------------------+-----------------------------------------------+
|
||||
| Term | Definition |
|
||||
| Term | Definition |
|
||||
+======================+===============================================+
|
||||
| *\<Term-1\>* | *\<definition-1\>* |
|
||||
| _\<Term-1\>_ | _\<definition-1\>_ |
|
||||
+----------------------+-----------------------------------------------+
|
||||
| *\<Term-2\>* | *\<definition-2\>* |
|
||||
| _\<Term-2\>_ | _\<definition-2\>_ |
|
||||
+----------------------+-----------------------------------------------+
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 284 KiB |
@@ -0,0 +1,229 @@
|
||||
# Incident Runbook
|
||||
|
||||
Triage order for Linear Void incidents. Work top-to-bottom — each tier depends on the one above.
|
||||
|
||||
## Triage Order
|
||||
|
||||
1. Edge Reachability (Cloudflare)
|
||||
2. Origin Health (VPS/Docker)
|
||||
3. Playout Progression (HLS)
|
||||
4. Chat Connectivity (WebSocket)
|
||||
5. Data-Plane (Redis/DB)
|
||||
|
||||
## 1. Edge Reachability (Cloudflare)
|
||||
|
||||
**Symptoms:** `linearvoid.tv` not resolving, SSL errors, 5xx from Cloudflare.
|
||||
|
||||
```bash
|
||||
# Check DNS
|
||||
dig linearvoid.tv
|
||||
dig www.linearvoid.tv
|
||||
|
||||
# Check Cloudflare status
|
||||
curl -I https://linearvoid.tv/health
|
||||
```
|
||||
|
||||
**Common causes:**
|
||||
|
||||
- DNS misconfiguration (A record pointing to wrong IP)
|
||||
- Cloudflare proxy disabled (grey cloud → origin exposed directly)
|
||||
- SSL certificate expired (check Cloudflare dashboard → SSL/TLS)
|
||||
- Cloudflare outage → check [https://www.cloudflarestatus.com](https://www.cloudflarestatus.com)
|
||||
|
||||
**Mitigation:**
|
||||
|
||||
- Verify A record: `linearvoid.tv` → origin IP (proxied, orange cloud)
|
||||
- Bypass Cloudflare temporarily: `curl -H "Host: linearvoid.tv" http://<origin-ip>/health`
|
||||
|
||||
## 2. Origin Health (VPS/Docker)
|
||||
|
||||
**Symptoms:** Cloudflare returns 502/504, containers down.
|
||||
|
||||
```bash
|
||||
# Remote Docker (test lab)
|
||||
docker -H tcp://192.168.88.40:2375 ps
|
||||
docker -H tcp://192.168.88.40:2375 logs linearvoid-nginx --tail 50
|
||||
|
||||
# Direct health check
|
||||
curl http://192.168.88.40/health
|
||||
```
|
||||
|
||||
**Common causes:**
|
||||
|
||||
- Host out of disk space: `df -h`
|
||||
- Docker daemon stopped: `systemctl status docker`
|
||||
- Container restart loop: `docker ps -a --filter "status=exited"`
|
||||
- Resource exhaustion (OOM): `dmesg | grep -i oom`
|
||||
|
||||
**Mitigation:**
|
||||
|
||||
- Restart stack: `docker compose -f docker-compose.prod.yml up -d`
|
||||
- Free disk: `docker system prune -f`
|
||||
- Check logs per service: `docker logs linearvoid-<service> --tail 100`
|
||||
|
||||
## 3. Playout Progression (HLS)
|
||||
|
||||
**Symptoms:** Viewer sees slate or buffering, playlist not advancing.
|
||||
|
||||
```bash
|
||||
# Check playout health
|
||||
curl http://192.168.88.40:8081/health
|
||||
# Response includes "mode": "live" or "slate"
|
||||
|
||||
# Check HLS output
|
||||
curl -I http://192.168.88.40/hls/playlist.m3u8
|
||||
ls -la data/hls/
|
||||
|
||||
# Check playout logs
|
||||
docker logs linearvoid-playout --tail 100
|
||||
```
|
||||
|
||||
**Common causes:**
|
||||
|
||||
- FFmpeg crash → slate fallback active (health shows `degraded`)
|
||||
- Playlist file empty/missing: `cat data/playlist/playlist.txt`
|
||||
- Media file missing from playlist: `ls data/media/`
|
||||
- Disk full → HLS segments can't write
|
||||
|
||||
**Mitigation:**
|
||||
|
||||
- Verify playlist has valid entries
|
||||
- Check media files exist at paths in playlist
|
||||
- Restart playout: `docker compose restart playout`
|
||||
- Generate demo playlist if empty: see `backend/playout/start-playout.sh`
|
||||
|
||||
## 4. Chat Connectivity (WebSocket)
|
||||
|
||||
**Symptoms:** Chat not loading, messages not sending, presence broken.
|
||||
|
||||
```bash
|
||||
# Check chat health
|
||||
curl http://192.168.88.40:3000/health
|
||||
|
||||
# Test WebSocket
|
||||
wscat -c ws://192.168.88.40/ws?username=test&room=main
|
||||
|
||||
# Check chat logs
|
||||
docker logs linearvoid-chat --tail 100
|
||||
```
|
||||
|
||||
**Common causes:**
|
||||
|
||||
- Redis down → presence/rate-limiting degraded (chat still works)
|
||||
- Database down → messages not persisting
|
||||
- Port not exposed: `docker port linearvoid-chat`
|
||||
|
||||
**Mitigation:**
|
||||
|
||||
- Restart chat: `docker compose restart chat`
|
||||
- Check Redis: `docker exec linearvoid-redis redis-cli ping`
|
||||
- Check DB: `docker exec linearvoid-db pg_isready -U linearvoid`
|
||||
- Chat works without Redis (graceful degradation) — check logs for "Redis connection warning"
|
||||
|
||||
## 5. Data-Plane (Redis/DB)
|
||||
|
||||
**Symptoms:** Chat works but no persistence/presence, admin stats empty.
|
||||
|
||||
```bash
|
||||
# Redis
|
||||
docker exec linearvoid-redis redis-cli ping
|
||||
docker exec linearvoid-redis redis-cli info memory
|
||||
|
||||
# PostgreSQL
|
||||
docker exec linearvoid-db pg_isready -U linearvoid
|
||||
docker exec linearvoid-db psql -U linearvoid -c "SELECT count(*) FROM messages;"
|
||||
```
|
||||
|
||||
**Common causes:**
|
||||
|
||||
- Redis OOM (check `maxmemory` setting)
|
||||
- PostgreSQL connection limit reached
|
||||
- Disk full on volume mount
|
||||
|
||||
**Mitigation:**
|
||||
|
||||
- Redis: `docker compose restart redis` (AOF persist enabled)
|
||||
- PostgreSQL: `docker compose restart db`
|
||||
- Restore from backup: see `scripts/verify-backups.sh` for procedure
|
||||
|
||||
## Escalation
|
||||
|
||||
| Severity | When | Action |
|
||||
| -------- | ------------------------------------ | -------------------------------------------------- |
|
||||
| Critical | Slate active >30min, complete outage | Trigger DR plan, provision fallback host |
|
||||
| Warning | Redis down, high CPU >30min | Investigate during business hours, restart service |
|
||||
| Info | Single service blip | Monitor, no action unless recurring |
|
||||
|
||||
## Post-Incident
|
||||
|
||||
1. Document timeline in incident channel
|
||||
2. Identify root cause
|
||||
3. Add prevention to docs if actionable
|
||||
4. Update this runbook if new failure mode discovered
|
||||
|
||||
## Quarterly Disaster Recovery Drill
|
||||
|
||||
Goal: validate recovery targets under controlled host-loss simulation.
|
||||
|
||||
**Recovery targets:** RPO <= 24h, RTO <= 60min.
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Latest backup artifacts available from `scripts/backup-db.sh` and `scripts/backup-weekly.sh`
|
||||
- Operator access to Docker host and DNS controls
|
||||
- Current deployment variables available (`.env`, compose file, image tags)
|
||||
|
||||
### Drill Scope
|
||||
|
||||
- Simulate complete origin loss (all app containers unavailable)
|
||||
- Rebuild stack on clean host
|
||||
- Restore DB and media/playlist mounts
|
||||
- Verify viewer playback, chat, admin auth, monitoring targets
|
||||
|
||||
### Drill Steps
|
||||
|
||||
1. Record drill start timestamp and on-call participants.
|
||||
2. Capture pre-drill baseline:
|
||||
- `curl -fsS http://<origin>/health`
|
||||
- `curl -fsS http://<origin>:3000/health`
|
||||
- `curl -fsS http://<origin>:8081/health`
|
||||
3. Simulate origin outage:
|
||||
- `docker compose -f docker-compose.prod.yml down`
|
||||
4. Provision clean recovery host (or clean test-lab VM) and copy deployment bundle.
|
||||
5. Restore required data paths:
|
||||
- `data/playlist/`
|
||||
- `data/media/`
|
||||
- `data/chat/`
|
||||
- `data/hls/` (optional warm cache)
|
||||
6. Restore database from latest backup dump.
|
||||
7. Start stack:
|
||||
- `docker compose -f docker-compose.prod.yml up -d`
|
||||
8. Validate service health and runtime:
|
||||
- `docker ps --format "table {{.Names}}\t{{.Status}}"`
|
||||
- `curl -fsS http://<origin>/health`
|
||||
- `curl -fsS http://<origin>/hls/playlist.m3u8 | head -n 5`
|
||||
- `curl -fsS http://<origin>:9090/-/ready`
|
||||
9. Validate app behavior:
|
||||
- Viewer SPA loads and HLS plays
|
||||
- WebSocket chat connects and message round-trip works
|
||||
- Admin login/session endpoint returns success
|
||||
10. Run backup integrity check:
|
||||
- `scripts/verify-backups.sh`
|
||||
11. Record drill end timestamp and compute measured RTO/RPO.
|
||||
|
||||
### Pass/Fail Criteria
|
||||
|
||||
- Pass if all core services healthy and functional in <= 60 minutes.
|
||||
- Pass if latest recoverable data point is <= 24 hours old.
|
||||
- Fail if any critical path remains degraded (playout, chat, admin auth, monitoring).
|
||||
|
||||
### Evidence Template
|
||||
|
||||
Document each drill in incident tracker with:
|
||||
|
||||
- Date/time window
|
||||
- Environment (test-lab, staging, production)
|
||||
- Backup artifact IDs and timestamps
|
||||
- Measured RTO and RPO
|
||||
- Issues found and remediation owner
|
||||
- Follow-up ticket links
|
||||
@@ -0,0 +1,117 @@
|
||||
# Cloudflare and UptimeRobot Manual Completion Checklist
|
||||
|
||||
Purpose: execute and record all manual dashboard tasks that cannot be automated from repository workflows.
|
||||
|
||||
## Status Legend
|
||||
|
||||
- [ ] Pending
|
||||
- [x] Done
|
||||
|
||||
## 1) Cloudflare Pages Link and Build
|
||||
|
||||
- [ ] Connect repo to Cloudflare Pages project
|
||||
- [ ] Build command set to: `cd frontend && npm run build`
|
||||
- [ ] Output directory set to: `frontend/dist`
|
||||
- [ ] Production domain attached to `linearvoid.tv`
|
||||
|
||||
Evidence to capture:
|
||||
|
||||
- Screenshot of Pages deployment settings
|
||||
- Deployment URL and latest deployment timestamp
|
||||
|
||||
## 2) Tiered Cache Enablement and Validation
|
||||
|
||||
- [ ] Smart Tiered Cache enabled for zone
|
||||
- [ ] Warm request made to hot HLS segment
|
||||
- [ ] Warm request made to live manifest
|
||||
- [ ] `Cf-Cache-Status: HIT` observed on repeated requests
|
||||
|
||||
Validation commands:
|
||||
|
||||
```bash
|
||||
curl -I https://media.linearvoid.tv/hls/playlist.m3u8
|
||||
curl -I https://media.linearvoid.tv/hls/<segment>.m4s
|
||||
```
|
||||
|
||||
Evidence to capture:
|
||||
|
||||
- Headers from first request (expected MISS)
|
||||
- Headers from repeated request (expected HIT)
|
||||
|
||||
## 3) WAF and Bot Protection
|
||||
|
||||
- [ ] OWASP Managed Ruleset enabled
|
||||
- [ ] Mode set to Simulate
|
||||
- [ ] Simulate mode reviewed for 48h
|
||||
- [ ] Mode switched to Block after false-positive review
|
||||
- [ ] Bot Fight Mode enabled
|
||||
|
||||
Evidence to capture:
|
||||
|
||||
- WAF ruleset status screenshot
|
||||
- Count of blocked/challenged events after cutover
|
||||
|
||||
## 4) Rate Limiting Rules
|
||||
|
||||
- [ ] Rule created for `/api/admin/login`
|
||||
- [ ] Rule created for `/api/admin/*`
|
||||
- [ ] Threshold set to `10 requests / 10 seconds / IP`
|
||||
- [ ] Action set to Managed Challenge
|
||||
- [ ] Rule order validated
|
||||
|
||||
Validation approach:
|
||||
|
||||
- Use burst requests from a test client and verify challenge behavior without blocking legitimate admin flow.
|
||||
|
||||
Evidence to capture:
|
||||
|
||||
- Rule definitions screenshot
|
||||
- Test result notes (challenge seen / no lockout)
|
||||
|
||||
## 5) HTTPS and WSS Hardening
|
||||
|
||||
- [ ] Always Use HTTPS enabled
|
||||
- [ ] HSTS enabled with max-age 31536000
|
||||
- [ ] HSTS includeSubDomains enabled
|
||||
- [ ] HSTS preload enabled
|
||||
- [ ] Minimum TLS version set to 1.2+
|
||||
- [ ] SSL mode verified as Full (strict)
|
||||
- [ ] WSS endpoint validated through proxy (`wss://linearvoid.tv/ws`)
|
||||
|
||||
Validation commands:
|
||||
|
||||
```bash
|
||||
curl -I https://linearvoid.tv
|
||||
curl -I https://linearvoid.tv/health
|
||||
```
|
||||
|
||||
Evidence to capture:
|
||||
|
||||
- Response headers showing HSTS
|
||||
- Successful browser WebSocket connect in devtools
|
||||
|
||||
## 6) UptimeRobot Monitors
|
||||
|
||||
- [ ] HTTP monitor added for `https://linearvoid.tv/health` (5m)
|
||||
- [ ] Keyword monitor added for `https://linearvoid.tv/hls/playlist.m3u8` (keyword `#EXTM3U`, 5m)
|
||||
- [ ] Alert trigger set to 2 consecutive failures
|
||||
- [ ] Notification channels configured (email + Discord/Slack webhook)
|
||||
|
||||
Evidence to capture:
|
||||
|
||||
- Monitor dashboard screenshot
|
||||
- Test alert delivery confirmation
|
||||
|
||||
## 7) Sign-Off
|
||||
|
||||
- [ ] Cloudflare manual tasks complete
|
||||
- [ ] UptimeRobot tasks complete
|
||||
- [ ] TODO items updated to done
|
||||
- [ ] Changelog entry added for completion batch
|
||||
|
||||
Sign-off fields:
|
||||
|
||||
- Owner:
|
||||
- Date:
|
||||
- Environment (test-lab or production):
|
||||
- Notes:
|
||||
@@ -0,0 +1,203 @@
|
||||
# Scaling and Performance Playbook
|
||||
|
||||
Purpose: define repeatable method to measure performance, capture baseline data, and decide scaling actions.
|
||||
|
||||
Scope: planning and execution guide for TODO items under "Scaling & Performance (planning only)".
|
||||
|
||||
## 1) Manifest TTL Tuning Plan
|
||||
|
||||
Goal: tune `.m3u8` TTL to about `0.5x` segment duration while keeping playback stable.
|
||||
|
||||
Inputs:
|
||||
|
||||
- Segment duration (`HLS_SEGMENT_DURATION`)
|
||||
- Current manifest TTL (Cloudflare cache rule)
|
||||
- Cache outcome distribution (`Cf-Cache-Status`)
|
||||
|
||||
Procedure:
|
||||
|
||||
1. Start from current manifest TTL (2s).
|
||||
2. Capture 24h baseline headers from repeated `playlist.m3u8` requests.
|
||||
3. Test candidate TTLs: 1s, 2s, 3s.
|
||||
4. Compare:
|
||||
- rebuffer events from player logs
|
||||
- origin request increase/decrease
|
||||
- HIT ratio changes
|
||||
5. Select lowest TTL that keeps playback stable and origin load acceptable.
|
||||
|
||||
Acceptance:
|
||||
|
||||
- No material increase in rebuffer rate.
|
||||
- Manifest staleness not observable by users.
|
||||
|
||||
## 2) TTFB Baseline Plan
|
||||
|
||||
Goal: measure warm-cache TTFB from 3+ US regions with target `< 4s`.
|
||||
|
||||
Suggested regions:
|
||||
|
||||
- US East (Virginia)
|
||||
- US Central
|
||||
- US West (California/Oregon)
|
||||
|
||||
Method:
|
||||
|
||||
1. Use WebPageTest with repeat view enabled.
|
||||
2. Run at least 5 samples per region.
|
||||
3. Record median and p95 TTFB.
|
||||
4. Store results in table (date, region, median, p95, test URL).
|
||||
|
||||
Acceptance:
|
||||
|
||||
- Warm-cache median < 4s in all tested regions.
|
||||
- Outliers documented with likely cause.
|
||||
|
||||
## 3) WebSocket Load-Test Plan
|
||||
|
||||
Goal: characterize chat service behavior across 10 -> 100 -> 500 -> 1000 concurrent clients.
|
||||
|
||||
Tooling option A (Artillery):
|
||||
|
||||
```bash
|
||||
npx artillery run scripts/loadtest/ws-chat.yml
|
||||
```
|
||||
|
||||
Tooling option B (custom Go harness):
|
||||
|
||||
- open N concurrent WebSocket clients
|
||||
- send message every X seconds
|
||||
- measure round-trip latency and failures
|
||||
|
||||
Metrics to capture:
|
||||
|
||||
- p50/p95 message latency
|
||||
- error rate (connect, send, receive)
|
||||
- dropped connections
|
||||
- CPU/memory for `chat`, `redis`, `nginx`
|
||||
|
||||
Acceptance:
|
||||
|
||||
- p95 chat latency <= 1s at target concurrency tier.
|
||||
- error rate <= 1% during steady state.
|
||||
|
||||
## 4) Admin API Benchmark Plan
|
||||
|
||||
Goal: measure admin API p95 and identify slow endpoints.
|
||||
|
||||
Tooling option A (`k6`):
|
||||
|
||||
```bash
|
||||
k6 run scripts/loadtest/admin-api.js
|
||||
```
|
||||
|
||||
Tooling option B (`wrk`):
|
||||
|
||||
```bash
|
||||
wrk -t4 -c100 -d60s http://<host>/api/admin/overview
|
||||
```
|
||||
|
||||
Endpoints:
|
||||
|
||||
- `/api/admin/overview`
|
||||
- `/api/admin/analytics`
|
||||
- `/api/admin/chat-log`
|
||||
- `/api/admin/users`
|
||||
|
||||
Acceptance:
|
||||
|
||||
- p95 < 1s for primary admin endpoints.
|
||||
- slow endpoint list with remediation notes.
|
||||
|
||||
## 5) Cache Hit Ratio Tracking Plan
|
||||
|
||||
Goal: track Cloudflare cache hit ratio and alert when below threshold.
|
||||
|
||||
Implementation outline:
|
||||
|
||||
1. Pull Cloudflare GraphQL analytics on schedule.
|
||||
2. Export to Prometheus-compatible metric path.
|
||||
3. Add Grafana panel with 5m/1h/24h windows.
|
||||
4. Alert when hit ratio < 90% for 15m.
|
||||
|
||||
Metric target:
|
||||
|
||||
- `cache_hit_ratio >= 0.90`
|
||||
|
||||
## 6) Capacity Trigger Rules
|
||||
|
||||
Define operational triggers:
|
||||
|
||||
- Cache miss ratio > 10% for 15m
|
||||
- Chat p95 latency > 1s for 5m
|
||||
- Host CPU > 80% for 10m
|
||||
- Redis memory > 85% sustained
|
||||
|
||||
Action mapping:
|
||||
|
||||
- Level 1: investigate + restart non-critical components
|
||||
- Level 2: scale service resources (CPU/RAM)
|
||||
- Level 3: activate standby planning path
|
||||
|
||||
## 7) Phase 2 Service Separation Plan
|
||||
|
||||
Objective: separate bottlenecked services while keeping simple ops.
|
||||
|
||||
Boundaries:
|
||||
|
||||
- `playout` isolated first (CPU-heavy)
|
||||
- `chat` + `redis` grouped for latency
|
||||
- `db` isolated for IO stability
|
||||
- monitoring stack optionally separated after sustained load
|
||||
|
||||
Initial sizing guidance:
|
||||
|
||||
- playout host: 4 vCPU / 8 GB
|
||||
- chat host: 2-4 vCPU / 4-8 GB
|
||||
- db host: 2 vCPU / 4 GB + fast disk
|
||||
|
||||
Exit criteria for Phase 2:
|
||||
|
||||
- repeated trigger events over 7 days
|
||||
- capacity saturation despite tuning
|
||||
|
||||
## 8) Phase 3 Warm Standby Plan
|
||||
|
||||
Objective: second-region resilience with DNS failover.
|
||||
|
||||
Candidate layout:
|
||||
|
||||
- Primary: Hetzner Ashburn (US-East)
|
||||
- Standby: Hetzner Hillsboro (US-West)
|
||||
|
||||
Design:
|
||||
|
||||
- Warm standby stack deployed and health-checked
|
||||
- Redis replication or fallback mode documented
|
||||
- Postgres replication strategy selected (streaming replica or periodic snapshot restore)
|
||||
- Cloudflare DNS failover policy defined (health-based or manual emergency switch)
|
||||
|
||||
Failover drill requirements:
|
||||
|
||||
- periodic simulated cutover
|
||||
- measured RTO/RPO logged in incident records
|
||||
|
||||
## 9) Data Capture Template
|
||||
|
||||
For each benchmark run, capture:
|
||||
|
||||
- Date/time
|
||||
- Environment (test-lab/staging/production)
|
||||
- Commit SHA / image tag
|
||||
- Test scenario and duration
|
||||
- Raw metrics (latency, errors, resource usage)
|
||||
- Conclusion and next action
|
||||
|
||||
## 10) Execution Order
|
||||
|
||||
1. TTFB baseline
|
||||
2. WebSocket load profile
|
||||
3. Admin API benchmark
|
||||
4. Cache ratio panel + alerting
|
||||
5. Trigger tuning
|
||||
6. Phase 2 boundary decision
|
||||
7. Phase 3 standby design review
|
||||
+116
-793
@@ -1,807 +1,130 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Linear Void Admin</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
||||
<link rel="stylesheet" href="../src/style.css" />
|
||||
</head>
|
||||
</head>
|
||||
|
||||
<body class="bg-slate-950 text-slate-200" data-admin-default-moderator="%VITE_PUBLIC_ADMIN_DEFAULT_MODERATOR%" data-admin-default-room="%VITE_PUBLIC_ADMIN_DEFAULT_ROOM%" data-admin-moderation-log-limit="%VITE_PUBLIC_ADMIN_MODERATION_LOG_LIMIT%">
|
||||
<div class="flex h-screen" x-data="admin()">
|
||||
<!-- Sidebar -->
|
||||
<div class="w-64 bg-slate-900 p-6">
|
||||
<div class="flex items-center gap-3 mb-8">
|
||||
<div class="w-8 h-8 bg-blue-600 rounded-full flex items-center justify-center text-white font-bold">LV</div>
|
||||
<div class="text-xl font-bold">Admin</div>
|
||||
</div>
|
||||
<div class="mb-6">
|
||||
<button @click="logout()" class="w-full bg-slate-800 hover:bg-slate-700 border border-slate-700 px-4 py-2 rounded-xl text-sm text-left">Sign out</button>
|
||||
</div>
|
||||
<nav class="space-y-2">
|
||||
<a href="#" @click="currentTab = 'dashboard'" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'dashboard' }">Dashboard</a>
|
||||
<a href="#" @click="currentTab = 'upload'; refreshMediaLibrary()" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'upload' }">Upload Video</a>
|
||||
<a href="#" @click="currentTab = 'playlist'; refreshPlaylist()" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'playlist' }">Playlist</a>
|
||||
<a href="#" @click="currentTab = 'schedule'; refreshSchedule()" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'schedule' }">Schedule</a>
|
||||
<a href="#" @click="currentTab = 'moderation'; refreshModerationLogs()" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'moderation' }">Moderation</a>
|
||||
<a href="#" @click="currentTab = 'users'; refreshUsers()" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'users' }">Users</a>
|
||||
<a href="#" @click="currentTab = 'analytics'; refreshAnalytics()" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'analytics' }">Analytics</a>
|
||||
<a href="#" @click="currentTab = 'system'; refreshSystemSummary()" class="block px-4 py-2 rounded-lg" :class="{ 'bg-blue-600': currentTab === 'system' }">System (Redis)</a>
|
||||
</nav>
|
||||
<body class="admin-body">
|
||||
<div class="admin-shell">
|
||||
<aside class="admin-sidebar" aria-label="Primary">
|
||||
<div class="brand-block">
|
||||
<div class="brand-badge" aria-hidden="true">LV</div>
|
||||
<div class="brand-text">Admin</div>
|
||||
</div>
|
||||
|
||||
<!-- Main Content -->
|
||||
<div class="flex-1 p-8 overflow-auto">
|
||||
<template x-if="currentTab === 'dashboard'">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold mb-8">Dashboard</h1>
|
||||
<div class="grid grid-cols-3 gap-6">
|
||||
<div class="bg-slate-800 p-6 rounded-3xl">
|
||||
<div class="text-emerald-400 text-sm">Concurrent Viewers</div>
|
||||
<div class="text-5xl font-bold mt-2" x-text="stats.viewers"></div>
|
||||
</div>
|
||||
<div class="bg-slate-800 p-6 rounded-3xl">
|
||||
<div class="text-blue-400 text-sm">Stored Messages</div>
|
||||
<div class="text-5xl font-bold mt-2" x-text="stats.storedMessages"></div>
|
||||
</div>
|
||||
<div class="bg-slate-800 p-6 rounded-3xl">
|
||||
<div class="text-amber-400 text-sm">Moderation Actions</div>
|
||||
<div class="text-5xl font-bold mt-2" x-text="stats.moderationActions"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-6 text-sm text-slate-400" x-text="statusMessage"></div>
|
||||
</div>
|
||||
</template>
|
||||
<button id="logout-btn" type="button" class="btn btn-secondary btn-block">Sign out</button>
|
||||
|
||||
<template x-if="currentTab === 'upload'">
|
||||
<div>
|
||||
<div class="flex items-start justify-between gap-6 mb-8">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold">Upload Video</h1>
|
||||
<p class="text-sm text-slate-400 mt-2">Upload media files into the local media library mounted for playout.</p>
|
||||
</div>
|
||||
<button @click="refreshMediaLibrary()" class="bg-slate-700 hover:bg-slate-600 px-4 py-2 rounded-xl text-sm">Reload library</button>
|
||||
</div>
|
||||
<div class="border-2 border-dashed border-slate-600 rounded-3xl p-12 text-center">
|
||||
<input type="file" id="video-upload" class="hidden" accept="video/*,.mp4,.mov,.mkv,.webm" @change="uploadVideo">
|
||||
<button onclick="document.getElementById('video-upload').click()" class="bg-blue-600 hover:bg-blue-500 px-8 py-4 rounded-2xl text-lg font-medium" :disabled="media.uploading">
|
||||
Drag & Drop or Click to Upload
|
||||
</button>
|
||||
<div class="mt-6 text-sm" :class="media.error ? 'text-rose-400' : 'text-slate-400'" x-text="media.status"></div>
|
||||
</div>
|
||||
<div class="bg-slate-800 rounded-3xl p-6 mt-6">
|
||||
<div class="flex items-center justify-between gap-4 mb-4">
|
||||
<h2 class="text-lg font-semibold">Media Library</h2>
|
||||
<div class="text-xs text-slate-400" x-text="media.generatedAt"></div>
|
||||
</div>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<thead class="text-slate-400 border-b border-slate-700">
|
||||
<tr>
|
||||
<th class="text-left py-3 pr-4 font-medium">File</th>
|
||||
<th class="text-left py-3 pr-4 font-medium">Size</th>
|
||||
<th class="text-left py-3 font-medium">Modified</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template x-if="media.files.length === 0">
|
||||
<tr>
|
||||
<td colspan="3" class="py-6 text-slate-500">No media files available.</td>
|
||||
</tr>
|
||||
</template>
|
||||
<template x-for="file in media.files" :key="file.name">
|
||||
<tr class="border-b border-slate-800">
|
||||
<td class="py-3 pr-4" x-text="file.name"></td>
|
||||
<td class="py-3 pr-4" x-text="file.size"></td>
|
||||
<td class="py-3 font-mono text-xs" x-text="file.modifiedAt"></td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<nav class="admin-nav" aria-label="Admin sections" role="tablist" aria-orientation="vertical">
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-dashboard"
|
||||
role="tab"
|
||||
class="tab-trigger is-active"
|
||||
data-tab="dashboard"
|
||||
aria-controls="tab-dashboard"
|
||||
aria-selected="true"
|
||||
>
|
||||
Dashboard
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-upload"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="upload"
|
||||
aria-controls="tab-upload"
|
||||
aria-selected="false"
|
||||
>
|
||||
Upload Video
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-playlist"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="playlist"
|
||||
aria-controls="tab-playlist"
|
||||
aria-selected="false"
|
||||
>
|
||||
Playlist
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-schedule"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="schedule"
|
||||
aria-controls="tab-schedule"
|
||||
aria-selected="false"
|
||||
>
|
||||
Schedule
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-moderation"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="moderation"
|
||||
aria-controls="tab-moderation"
|
||||
aria-selected="false"
|
||||
>
|
||||
Moderation
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-chat-log"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="chat-log"
|
||||
aria-controls="tab-chat-log"
|
||||
aria-selected="false"
|
||||
>
|
||||
Chat Log
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-users"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="users"
|
||||
aria-controls="tab-users"
|
||||
aria-selected="false"
|
||||
>
|
||||
Users
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-analytics"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="analytics"
|
||||
aria-controls="tab-analytics"
|
||||
aria-selected="false"
|
||||
>
|
||||
Analytics
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
id="tab-trigger-system"
|
||||
role="tab"
|
||||
class="tab-trigger"
|
||||
data-tab="system"
|
||||
aria-controls="tab-system"
|
||||
aria-selected="false"
|
||||
>
|
||||
System
|
||||
</button>
|
||||
</nav>
|
||||
</aside>
|
||||
|
||||
<template x-if="currentTab === 'schedule'">
|
||||
<div>
|
||||
<div class="flex items-start justify-between gap-6 mb-8">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold">Schedule</h1>
|
||||
<p class="text-sm text-slate-400 mt-2">Read-only view of the currently configured playout order.</p>
|
||||
</div>
|
||||
<button @click="refreshSchedule()" class="bg-slate-700 hover:bg-slate-600 px-4 py-2 rounded-xl text-sm">Reload schedule</button>
|
||||
</div>
|
||||
<div class="bg-slate-800 rounded-3xl p-6">
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<thead class="text-slate-400 border-b border-slate-700">
|
||||
<tr>
|
||||
<th class="text-left py-3 pr-4 font-medium">#</th>
|
||||
<th class="text-left py-3 pr-4 font-medium">Label</th>
|
||||
<th class="text-left py-3 font-medium">Path</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template x-if="schedule.entries.length === 0">
|
||||
<tr>
|
||||
<td colspan="3" class="py-6 text-slate-500">No schedule entries found.</td>
|
||||
</tr>
|
||||
</template>
|
||||
<template x-for="entry in schedule.entries" :key="entry.index + ':' + entry.path">
|
||||
<tr class="border-b border-slate-800">
|
||||
<td class="py-3 pr-4" x-text="entry.index"></td>
|
||||
<td class="py-3 pr-4" x-text="entry.label"></td>
|
||||
<td class="py-3 font-mono text-xs" x-text="entry.path"></td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="mt-4 text-sm" :class="schedule.error ? 'text-rose-400' : 'text-slate-400'" x-text="schedule.status"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="currentTab === 'playlist'">
|
||||
<div>
|
||||
<div class="flex items-start justify-between gap-6 mb-8">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold">Playlist</h1>
|
||||
<p class="text-sm text-slate-400 mt-2">Edit the active concat playlist used by the playout service.</p>
|
||||
</div>
|
||||
<div class="flex gap-3">
|
||||
<button @click="refreshPlaylist()" class="bg-slate-700 hover:bg-slate-600 px-4 py-2 rounded-xl text-sm">Reload</button>
|
||||
<button @click="savePlaylist()" class="bg-blue-600 hover:bg-blue-500 px-4 py-2 rounded-xl text-sm font-medium" :disabled="playlist.saving">
|
||||
<span x-text="playlist.saving ? 'Saving...' : 'Save playlist'"></span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-slate-800 rounded-3xl p-6">
|
||||
<textarea x-model="playlist.content" class="w-full min-h-[24rem] bg-slate-950 border border-slate-700 rounded-2xl p-4 font-mono text-sm focus:outline-none focus:border-blue-500" spellcheck="false"></textarea>
|
||||
<div class="mt-4 text-sm" :class="playlist.error ? 'text-rose-400' : 'text-slate-400'" x-text="playlist.status"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="currentTab === 'moderation'">
|
||||
<div>
|
||||
<div class="flex items-start justify-between gap-6 mb-8">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold">Moderation</h1>
|
||||
<p class="text-sm text-slate-400 mt-2">Create moderation actions and review the recent immutable audit log.</p>
|
||||
</div>
|
||||
<button @click="refreshModerationLogs()" class="bg-slate-700 hover:bg-slate-600 px-4 py-2 rounded-xl text-sm">Reload logs</button>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 xl:grid-cols-[24rem,1fr] gap-6">
|
||||
<div class="bg-slate-800 rounded-3xl p-6">
|
||||
<h2 class="text-lg font-semibold mb-4">New Action</h2>
|
||||
<div class="space-y-4">
|
||||
<label class="block text-sm">
|
||||
<span class="block text-slate-400 mb-2">Action</span>
|
||||
<select x-model="moderation.form.action" class="w-full bg-slate-950 border border-slate-700 rounded-xl px-4 py-3 focus:outline-none focus:border-blue-500">
|
||||
<option value="mute">mute</option>
|
||||
<option value="ban">ban</option>
|
||||
<option value="delete">delete</option>
|
||||
<option value="timeout">timeout</option>
|
||||
</select>
|
||||
</label>
|
||||
<label class="block text-sm">
|
||||
<span class="block text-slate-400 mb-2">Target</span>
|
||||
<input x-model="moderation.form.target" type="text" class="w-full bg-slate-950 border border-slate-700 rounded-xl px-4 py-3 focus:outline-none focus:border-blue-500" placeholder="username or message id">
|
||||
</label>
|
||||
<label class="block text-sm">
|
||||
<span class="block text-slate-400 mb-2">Moderator</span>
|
||||
<input x-model="moderation.form.moderator" type="text" class="w-full bg-slate-950 border border-slate-700 rounded-xl px-4 py-3 focus:outline-none focus:border-blue-500" placeholder="admin">
|
||||
</label>
|
||||
<label class="block text-sm">
|
||||
<span class="block text-slate-400 mb-2">Room</span>
|
||||
<input x-model="moderation.form.room" type="text" class="w-full bg-slate-950 border border-slate-700 rounded-xl px-4 py-3 focus:outline-none focus:border-blue-500" placeholder="main">
|
||||
</label>
|
||||
<label class="block text-sm">
|
||||
<span class="block text-slate-400 mb-2">Reason</span>
|
||||
<textarea x-model="moderation.form.reason" class="w-full min-h-28 bg-slate-950 border border-slate-700 rounded-xl px-4 py-3 focus:outline-none focus:border-blue-500" placeholder="Explain why this action is needed"></textarea>
|
||||
</label>
|
||||
<button @click="submitModerationAction()" class="w-full bg-blue-600 hover:bg-blue-500 px-4 py-3 rounded-xl font-medium" :disabled="moderation.submitting">
|
||||
<span x-text="moderation.submitting ? 'Submitting...' : 'Submit action'"></span>
|
||||
</button>
|
||||
<div class="text-sm" :class="moderation.error ? 'text-rose-400' : 'text-slate-400'" x-text="moderation.status"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-slate-800 rounded-3xl p-6 overflow-hidden">
|
||||
<div class="flex items-center justify-between gap-4 mb-4">
|
||||
<h2 class="text-lg font-semibold">Recent Audit Log</h2>
|
||||
<div class="text-xs text-slate-400">Latest 50 actions</div>
|
||||
</div>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<thead class="text-slate-400 border-b border-slate-700">
|
||||
<tr>
|
||||
<th class="text-left py-3 pr-4 font-medium">When</th>
|
||||
<th class="text-left py-3 pr-4 font-medium">Action</th>
|
||||
<th class="text-left py-3 pr-4 font-medium">Target</th>
|
||||
<th class="text-left py-3 pr-4 font-medium">Moderator</th>
|
||||
<th class="text-left py-3 font-medium">Reason</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template x-if="moderation.logs.length === 0">
|
||||
<tr>
|
||||
<td colspan="5" class="py-6 text-slate-500">No moderation actions logged yet.</td>
|
||||
</tr>
|
||||
</template>
|
||||
<template x-for="entry in moderation.logs" :key="entry.id">
|
||||
<tr class="border-b border-slate-800 align-top">
|
||||
<td class="py-3 pr-4 font-mono text-xs" x-text="entry.timestamp"></td>
|
||||
<td class="py-3 pr-4" x-text="entry.action"></td>
|
||||
<td class="py-3 pr-4" x-text="entry.target"></td>
|
||||
<td class="py-3 pr-4" x-text="entry.moderator"></td>
|
||||
<td class="py-3" x-text="entry.reason"></td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="currentTab === 'analytics'">
|
||||
<div>
|
||||
<div class="flex items-start justify-between gap-6 mb-8">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold">Analytics</h1>
|
||||
<p class="text-sm text-slate-400 mt-2">Basic activity counters from chat history and moderation audit logs.</p>
|
||||
</div>
|
||||
<button @click="refreshAnalytics()" class="bg-slate-700 hover:bg-slate-600 px-4 py-2 rounded-xl text-sm">Reload analytics</button>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 xl:grid-cols-4 gap-6 mb-6">
|
||||
<div class="bg-slate-800 p-6 rounded-3xl">
|
||||
<div class="text-slate-400 text-sm">Total Messages</div>
|
||||
<div class="text-4xl font-bold mt-2" x-text="analytics.totalMessages"></div>
|
||||
</div>
|
||||
<div class="bg-slate-800 p-6 rounded-3xl">
|
||||
<div class="text-slate-400 text-sm">Last 24 Hours</div>
|
||||
<div class="text-4xl font-bold mt-2" x-text="analytics.messagesLast24Hours"></div>
|
||||
</div>
|
||||
<div class="bg-slate-800 p-6 rounded-3xl">
|
||||
<div class="text-slate-400 text-sm">Distinct Rooms</div>
|
||||
<div class="text-4xl font-bold mt-2" x-text="analytics.distinctRooms"></div>
|
||||
</div>
|
||||
<div class="bg-slate-800 p-6 rounded-3xl">
|
||||
<div class="text-slate-400 text-sm">Moderation Actions</div>
|
||||
<div class="text-4xl font-bold mt-2" x-text="analytics.moderationActions"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-slate-800 rounded-3xl p-6">
|
||||
<div class="flex items-center justify-between gap-4 mb-4">
|
||||
<h2 class="text-lg font-semibold">Top Rooms</h2>
|
||||
<div class="text-xs text-slate-400" x-text="analytics.generatedAt"></div>
|
||||
</div>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<thead class="text-slate-400 border-b border-slate-700">
|
||||
<tr>
|
||||
<th class="text-left py-3 pr-4 font-medium">Room</th>
|
||||
<th class="text-left py-3 font-medium">Messages</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template x-if="analytics.topRooms.length === 0">
|
||||
<tr>
|
||||
<td colspan="2" class="py-6 text-slate-500">No room analytics available yet.</td>
|
||||
</tr>
|
||||
</template>
|
||||
<template x-for="room in analytics.topRooms" :key="room.room">
|
||||
<tr class="border-b border-slate-800">
|
||||
<td class="py-3 pr-4" x-text="room.room"></td>
|
||||
<td class="py-3" x-text="room.messageCount"></td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="mt-4 text-sm" :class="analytics.error ? 'text-rose-400' : 'text-slate-400'" x-text="analytics.status"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="currentTab === 'system'">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold mb-8">System Configuration (Redis & Storage)</h1>
|
||||
<div class="bg-slate-800 p-8 rounded-3xl">
|
||||
<div class="grid grid-cols-2 gap-8">
|
||||
<div>
|
||||
<h3 class="font-medium mb-4">Redis Stats</h3>
|
||||
<div class="space-y-4">
|
||||
<div class="flex justify-between">
|
||||
<span>Memory Used</span>
|
||||
<span class="font-mono" x-text="system.redisMemoryUsed"></span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span>Connected Redis Clients</span>
|
||||
<span class="font-mono" x-text="system.redisClients"></span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span>Redis Enabled</span>
|
||||
<span class="font-mono" x-text="system.redisEnabled ? 'yes' : 'no'"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h3 class="font-medium mb-4">Storage</h3>
|
||||
<div class="space-y-4">
|
||||
<div class="flex justify-between">
|
||||
<span>Database Driver</span>
|
||||
<span class="font-mono" x-text="system.databaseDriver"></span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span>Stored Messages</span>
|
||||
<span class="font-mono" x-text="stats.storedMessages"></span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span>Last Refresh</span>
|
||||
<span class="font-mono" x-text="system.generatedAt"></span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span>Playlist Path</span>
|
||||
<span class="font-mono text-right" x-text="system.playlistPath"></span>
|
||||
</div>
|
||||
<div class="flex justify-between">
|
||||
<span>Playlist Present</span>
|
||||
<span class="font-mono" x-text="system.playlistExists ? 'yes' : 'no'"></span>
|
||||
</div>
|
||||
<button onclick="runVacuum()" class="bg-slate-700 hover:bg-slate-600 px-6 py-2 rounded-xl">Run VACUUM</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-slate-800 p-8 rounded-3xl mt-6">
|
||||
<div class="flex items-center justify-between gap-4 mb-4">
|
||||
<h2 class="text-lg font-semibold">Service Health</h2>
|
||||
<button @click="refreshSystemSummary()" class="bg-slate-700 hover:bg-slate-600 px-4 py-2 rounded-xl text-sm">Reload health</button>
|
||||
</div>
|
||||
<div class="space-y-4">
|
||||
<template x-for="service in system.services" :key="service.name">
|
||||
<div class="flex items-start justify-between gap-6 border-b border-slate-700 pb-4">
|
||||
<div>
|
||||
<div class="font-medium" x-text="service.name"></div>
|
||||
<div class="text-xs text-slate-400 mt-1" x-text="service.checked"></div>
|
||||
</div>
|
||||
<div class="text-right max-w-xl">
|
||||
<div class="uppercase tracking-wide text-xs" :class="service.status === 'healthy' ? 'text-emerald-400' : service.status === 'disabled' ? 'text-amber-400' : 'text-rose-400'" x-text="service.status"></div>
|
||||
<div class="text-sm text-slate-300 mt-1 break-all" x-text="service.detail"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
<div class="mt-4 text-sm" :class="system.error ? 'text-rose-400' : 'text-slate-400'" x-text="system.status"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="currentTab === 'users'">
|
||||
<div>
|
||||
<div class="flex items-start justify-between gap-6 mb-8">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold">Users</h1>
|
||||
<p class="text-sm text-slate-400 mt-2">Current room presence derived from Redis presence keys.</p>
|
||||
</div>
|
||||
<button @click="refreshUsers()" class="bg-slate-700 hover:bg-slate-600 px-4 py-2 rounded-xl text-sm">Reload users</button>
|
||||
</div>
|
||||
<div class="bg-slate-800 rounded-3xl p-6">
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm">
|
||||
<thead class="text-slate-400 border-b border-slate-700">
|
||||
<tr>
|
||||
<th class="text-left py-3 pr-4 font-medium">Username</th>
|
||||
<th class="text-left py-3 pr-4 font-medium">Room</th>
|
||||
<th class="text-left py-3 font-medium">Online</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template x-if="users.entries.length === 0">
|
||||
<tr>
|
||||
<td colspan="3" class="py-6 text-slate-500">No active users reported.</td>
|
||||
</tr>
|
||||
</template>
|
||||
<template x-for="entry in users.entries" :key="entry.room + ':' + entry.username">
|
||||
<tr class="border-b border-slate-800">
|
||||
<td class="py-3 pr-4" x-text="entry.username"></td>
|
||||
<td class="py-3 pr-4" x-text="entry.room"></td>
|
||||
<td class="py-3" x-text="entry.online ? 'yes' : 'no'"></td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="mt-4 text-sm" :class="users.error ? 'text-rose-400' : 'text-slate-400'" x-text="users.status"></div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
<main class="admin-main" id="admin-main" tabindex="-1">
|
||||
<div id="tab-content-container" aria-live="polite">Loading...</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const adminConfig = (() => {
|
||||
const { dataset } = document.body;
|
||||
const moderationLogLimit = Number.parseInt(dataset.adminModerationLogLimit || '', 10);
|
||||
|
||||
return {
|
||||
defaultModerator: dataset.adminDefaultModerator || 'admin',
|
||||
defaultRoom: dataset.adminDefaultRoom || 'main',
|
||||
moderationLogLimit: Number.isFinite(moderationLogLimit) && moderationLogLimit > 0 ? moderationLogLimit : 50
|
||||
};
|
||||
})();
|
||||
|
||||
function admin() {
|
||||
return {
|
||||
currentTab: 'dashboard',
|
||||
stats: {
|
||||
viewers: 0,
|
||||
storedMessages: 0,
|
||||
moderationActions: 0
|
||||
},
|
||||
system: {
|
||||
redisEnabled: false,
|
||||
redisClients: 0,
|
||||
redisMemoryUsed: 'loading...',
|
||||
databaseDriver: 'loading...',
|
||||
generatedAt: 'loading...',
|
||||
playlistPath: 'loading...',
|
||||
playlistExists: false,
|
||||
services: [],
|
||||
status: 'Loading system summary...',
|
||||
error: false
|
||||
},
|
||||
analytics: {
|
||||
totalMessages: 0,
|
||||
messagesLast24Hours: 0,
|
||||
distinctRooms: 0,
|
||||
moderationActions: 0,
|
||||
topRooms: [],
|
||||
generatedAt: 'loading...',
|
||||
status: 'Loading analytics...',
|
||||
error: false
|
||||
},
|
||||
schedule: {
|
||||
entries: [],
|
||||
status: 'Loading schedule...',
|
||||
error: false
|
||||
},
|
||||
playlist: {
|
||||
content: '',
|
||||
status: 'Loading playlist...',
|
||||
saving: false,
|
||||
error: false
|
||||
},
|
||||
media: {
|
||||
files: [],
|
||||
generatedAt: 'loading...',
|
||||
uploading: false,
|
||||
status: 'Loading media library...',
|
||||
error: false
|
||||
},
|
||||
moderation: {
|
||||
logs: [],
|
||||
submitting: false,
|
||||
error: false,
|
||||
status: 'Loading moderation logs...',
|
||||
form: {
|
||||
action: 'mute',
|
||||
target: '',
|
||||
moderator: adminConfig.defaultModerator,
|
||||
reason: '',
|
||||
room: adminConfig.defaultRoom
|
||||
}
|
||||
},
|
||||
users: {
|
||||
entries: [],
|
||||
status: 'Loading active users...',
|
||||
error: false
|
||||
},
|
||||
statusMessage: 'Loading live admin overview...',
|
||||
async refreshOverview() {
|
||||
try {
|
||||
const response = await fetch('/api/admin/overview');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const overview = await response.json();
|
||||
this.stats.viewers = overview.viewers;
|
||||
this.stats.storedMessages = overview.storedMessages;
|
||||
this.stats.moderationActions = overview.moderationActions;
|
||||
this.system.redisEnabled = overview.redisEnabled;
|
||||
this.system.redisClients = overview.redisClients;
|
||||
this.system.redisMemoryUsed = overview.redisMemoryUsed;
|
||||
this.system.databaseDriver = overview.databaseDriver;
|
||||
this.system.generatedAt = overview.generatedAt;
|
||||
this.statusMessage = `Live data loaded at ${overview.generatedAt}`;
|
||||
} catch (error) {
|
||||
console.error('Failed to load admin overview', error);
|
||||
this.statusMessage = 'Failed to load live admin data.';
|
||||
this.system.redisMemoryUsed = 'unavailable';
|
||||
this.system.databaseDriver = 'unavailable';
|
||||
this.system.generatedAt = 'unavailable';
|
||||
}
|
||||
},
|
||||
async refreshAnalytics() {
|
||||
this.analytics.status = 'Loading analytics...';
|
||||
this.analytics.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/analytics');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.analytics.totalMessages = payload.totalMessages;
|
||||
this.analytics.messagesLast24Hours = payload.messagesLast24Hours;
|
||||
this.analytics.distinctRooms = payload.distinctRooms;
|
||||
this.analytics.moderationActions = payload.moderationActions;
|
||||
this.analytics.topRooms = payload.topRooms || [];
|
||||
this.analytics.generatedAt = payload.generatedAt;
|
||||
this.analytics.status = 'Analytics loaded.';
|
||||
} catch (error) {
|
||||
console.error('Failed to load analytics', error);
|
||||
this.analytics.status = 'Failed to load analytics.';
|
||||
this.analytics.error = true;
|
||||
}
|
||||
},
|
||||
async refreshPlaylist() {
|
||||
this.playlist.status = 'Loading playlist...';
|
||||
this.playlist.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/playlist');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.playlist.content = payload.content;
|
||||
this.playlist.status = 'Playlist loaded.';
|
||||
} catch (error) {
|
||||
console.error('Failed to load playlist', error);
|
||||
this.playlist.status = 'Failed to load playlist.';
|
||||
this.playlist.error = true;
|
||||
}
|
||||
},
|
||||
async refreshSchedule() {
|
||||
this.schedule.status = 'Loading schedule...';
|
||||
this.schedule.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/schedule');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.schedule.entries = payload.entries || [];
|
||||
this.schedule.status = 'Schedule loaded.';
|
||||
} catch (error) {
|
||||
console.error('Failed to load schedule', error);
|
||||
this.schedule.status = 'Failed to load schedule.';
|
||||
this.schedule.error = true;
|
||||
}
|
||||
},
|
||||
async refreshSystemSummary() {
|
||||
this.system.status = 'Loading system summary...';
|
||||
this.system.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/system');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.system.databaseDriver = payload.databaseDriver;
|
||||
this.system.playlistPath = payload.playlistPath;
|
||||
this.system.playlistExists = payload.playlistExists;
|
||||
this.system.redisEnabled = payload.redisEnabled;
|
||||
this.system.redisMemoryUsed = payload.redisMemoryUsed;
|
||||
this.system.services = payload.services || [];
|
||||
this.system.generatedAt = payload.generatedAt;
|
||||
this.system.status = 'System summary loaded.';
|
||||
} catch (error) {
|
||||
console.error('Failed to load system summary', error);
|
||||
this.system.status = 'Failed to load system summary.';
|
||||
this.system.error = true;
|
||||
}
|
||||
},
|
||||
async refreshMediaLibrary() {
|
||||
this.media.status = 'Loading media library...';
|
||||
this.media.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/media');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.media.files = payload.files || [];
|
||||
this.media.generatedAt = payload.generatedAt;
|
||||
this.media.status = 'Media library loaded.';
|
||||
} catch (error) {
|
||||
console.error('Failed to load media library', error);
|
||||
this.media.status = 'Failed to load media library.';
|
||||
this.media.error = true;
|
||||
}
|
||||
},
|
||||
async savePlaylist() {
|
||||
this.playlist.saving = true;
|
||||
this.playlist.status = 'Saving playlist...';
|
||||
this.playlist.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/playlist', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ content: this.playlist.content })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.playlist.content = payload.content;
|
||||
this.playlist.status = 'Playlist saved.';
|
||||
} catch (error) {
|
||||
console.error('Failed to save playlist', error);
|
||||
this.playlist.status = 'Failed to save playlist.';
|
||||
this.playlist.error = true;
|
||||
} finally {
|
||||
this.playlist.saving = false;
|
||||
}
|
||||
},
|
||||
async refreshModerationLogs() {
|
||||
this.moderation.status = 'Loading moderation logs...';
|
||||
this.moderation.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/admin/moderation/logs?limit=${adminConfig.moderationLogLimit}`);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.moderation.logs = payload.logs || [];
|
||||
this.moderation.status = 'Moderation logs loaded.';
|
||||
} catch (error) {
|
||||
console.error('Failed to load moderation logs', error);
|
||||
this.moderation.status = 'Failed to load moderation logs.';
|
||||
this.moderation.error = true;
|
||||
}
|
||||
},
|
||||
async submitModerationAction() {
|
||||
this.moderation.submitting = true;
|
||||
this.moderation.error = false;
|
||||
this.moderation.status = 'Submitting moderation action...';
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/moderation/actions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(this.moderation.form)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
this.moderation.status = 'Moderation action recorded.';
|
||||
this.moderation.form.target = '';
|
||||
this.moderation.form.reason = '';
|
||||
await this.refreshModerationLogs();
|
||||
await this.refreshOverview();
|
||||
} catch (error) {
|
||||
console.error('Failed to submit moderation action', error);
|
||||
this.moderation.status = 'Failed to submit moderation action.';
|
||||
this.moderation.error = true;
|
||||
} finally {
|
||||
this.moderation.submitting = false;
|
||||
}
|
||||
},
|
||||
async refreshUsers() {
|
||||
this.users.status = 'Loading active users...';
|
||||
this.users.error = false;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/users');
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
this.users.entries = payload.users || [];
|
||||
this.users.status = payload.redisEnabled ? 'Active users loaded.' : 'Redis disabled; no live presence data available.';
|
||||
} catch (error) {
|
||||
console.error('Failed to load users', error);
|
||||
this.users.status = 'Failed to load active users.';
|
||||
this.users.error = true;
|
||||
}
|
||||
},
|
||||
async logout() {
|
||||
try {
|
||||
await fetch('/api/admin/logout', { method: 'POST' });
|
||||
} finally {
|
||||
window.location.href = '/admin/login/';
|
||||
}
|
||||
},
|
||||
init() {
|
||||
this.refreshOverview();
|
||||
this.refreshAnalytics();
|
||||
this.refreshMediaLibrary();
|
||||
this.refreshPlaylist();
|
||||
this.refreshSchedule();
|
||||
this.refreshModerationLogs();
|
||||
this.refreshSystemSummary();
|
||||
this.refreshUsers();
|
||||
console.log('Admin Interface initialized (ADR-004/ADR-005)');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadVideo(event) {
|
||||
const file = event.target.files[0];
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
|
||||
const component = Alpine.$data(document.querySelector('[x-data="admin()"]'));
|
||||
component.media.uploading = true;
|
||||
component.media.error = false;
|
||||
component.media.status = `Uploading ${file.name}...`;
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
const response = await fetch('/api/admin/media', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
component.media.files = payload.files || [];
|
||||
component.media.generatedAt = payload.generatedAt;
|
||||
component.media.status = `${file.name} uploaded.`;
|
||||
document.getElementById('video-upload').value = '';
|
||||
} catch (error) {
|
||||
console.error('Failed to upload media file', error);
|
||||
component.media.status = 'Failed to upload media file.';
|
||||
component.media.error = true;
|
||||
} finally {
|
||||
component.media.uploading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function runVacuum() {
|
||||
alert('VACUUM executed on SQLite database (per ADR-006). Audit log entry created.');
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
<script type="module" src="/src/admin.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,70 +1,65 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Linear Void Admin Login</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<link rel="stylesheet" href="../../src/style.css" />
|
||||
</head>
|
||||
</head>
|
||||
|
||||
<body class="min-h-screen bg-slate-950 text-slate-100 flex items-center justify-center px-6">
|
||||
<main class="w-full max-w-md bg-slate-900 border border-slate-800 rounded-[2rem] shadow-2xl p-8">
|
||||
<div class="mb-8">
|
||||
<div class="text-xs uppercase tracking-[0.3em] text-slate-500 mb-3">Linear Void</div>
|
||||
<h1 class="text-4xl font-semibold text-white mb-2">Admin Login</h1>
|
||||
<p class="text-sm text-slate-400">Authenticate to access protected admin controls.</p>
|
||||
</div>
|
||||
<body class="auth-body">
|
||||
<main class="auth-shell" aria-label="Admin authentication">
|
||||
<section class="auth-card" aria-labelledby="auth-title">
|
||||
<p class="auth-kicker">Linear Void</p>
|
||||
<h1 id="auth-title">Admin Login</h1>
|
||||
<p class="auth-subtitle">Authenticate to access protected admin controls.</p>
|
||||
|
||||
<form id="login-form" class="space-y-4">
|
||||
<label class="block text-sm">
|
||||
<span class="block text-slate-400 mb-2">Username</span>
|
||||
<input id="username" type="text" class="w-full bg-slate-950 border border-slate-700 rounded-xl px-4 py-3 focus:outline-none focus:border-blue-500" autocomplete="username" required />
|
||||
</label>
|
||||
<label class="block text-sm">
|
||||
<span class="block text-slate-400 mb-2">Password</span>
|
||||
<input id="password" type="password" class="w-full bg-slate-950 border border-slate-700 rounded-xl px-4 py-3 focus:outline-none focus:border-blue-500" autocomplete="current-password" required />
|
||||
</label>
|
||||
<button id="submit" type="submit" class="w-full bg-blue-600 hover:bg-blue-500 px-4 py-3 rounded-xl font-medium text-white">Sign in</button>
|
||||
<div id="status" class="text-sm text-slate-400 min-h-6"></div>
|
||||
<form id="login-form" class="form-stack" novalidate>
|
||||
<label for="username">Username</label>
|
||||
<input id="username" type="text" class="text-input" autocomplete="username" required />
|
||||
|
||||
<label for="password">Password</label>
|
||||
<input id="password" type="password" class="text-input" autocomplete="current-password" required />
|
||||
|
||||
<button id="submit" type="submit" class="btn btn-primary btn-block">Sign in</button>
|
||||
<p id="status" class="status-line" aria-live="polite"></p>
|
||||
</form>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<script>
|
||||
const form = document.getElementById('login-form');
|
||||
const username = document.getElementById('username');
|
||||
const password = document.getElementById('password');
|
||||
const submit = document.getElementById('submit');
|
||||
const status = document.getElementById('status');
|
||||
const form = document.getElementById("login-form");
|
||||
const username = document.getElementById("username");
|
||||
const password = document.getElementById("password");
|
||||
const submit = document.getElementById("submit");
|
||||
const status = document.getElementById("status");
|
||||
|
||||
form.addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
submit.disabled = true;
|
||||
status.textContent = 'Signing in...';
|
||||
status.className = 'text-sm text-slate-400 min-h-6';
|
||||
form.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
submit.disabled = true;
|
||||
status.textContent = "Signing in...";
|
||||
status.className = "status-line";
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/admin/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username: username.value, password: password.value })
|
||||
});
|
||||
try {
|
||||
const response = await fetch("/api/admin/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username: username.value, password: password.value }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
window.location.href = '/admin/';
|
||||
} catch (error) {
|
||||
console.error('Failed to sign in', error);
|
||||
status.textContent = 'Login failed. Check your credentials.';
|
||||
status.className = 'text-sm text-rose-400 min-h-6';
|
||||
} finally {
|
||||
submit.disabled = false;
|
||||
}
|
||||
});
|
||||
window.location.href = "/admin/";
|
||||
} catch (error) {
|
||||
console.error("Failed to sign in", error);
|
||||
status.textContent = "Login failed. Check your credentials.";
|
||||
status.className = "status-line status-error";
|
||||
} finally {
|
||||
submit.disabled = false;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+62
-68
@@ -1,82 +1,76 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Linear Void - 24/7 Synchronized Channel</title>
|
||||
<link rel="stylesheet" href="/src/style.css" />
|
||||
</head>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Linear Void - 24/7 Synchronized Channel</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
|
||||
<link rel="stylesheet" href="/src/style.css" />
|
||||
</head>
|
||||
|
||||
<body class="bg-slate-950 text-slate-200">
|
||||
<div class="flex h-screen">
|
||||
<!-- Video Player -->
|
||||
<div class="flex-1 flex flex-col">
|
||||
<div class="p-4 border-b border-slate-800 flex items-center justify-between bg-slate-900">
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="w-8 h-8 bg-blue-600 rounded-full flex items-center justify-center text-white font-bold">LV</div>
|
||||
<body class="lv-body">
|
||||
<main class="viewer-shell" aria-label="Live player and chat">
|
||||
<header class="viewer-header">
|
||||
<div class="viewer-brand-group">
|
||||
<div class="brand-badge" aria-hidden="true">LV</div>
|
||||
<div>
|
||||
<h1 class="text-2xl font-bold text-white">Linear Void</h1>
|
||||
<p class="text-xs text-emerald-400">24/7 Synchronized Channel • Live</p>
|
||||
<h1 class="viewer-title">Linear Void</h1>
|
||||
<p class="viewer-subtitle">24/7 Synchronized Channel • Live</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="now-playing px-4 py-1 rounded-full text-sm font-medium flex items-center gap-2">
|
||||
<div class="w-2 h-2 bg-emerald-400 rounded-full animate-pulse"></div>
|
||||
<span id="now-playing">Loading schedule...</span>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
id="chat-toggle"
|
||||
type="button"
|
||||
class="btn btn-secondary chat-toggle-btn"
|
||||
onclick="toggleChatPanel()"
|
||||
aria-controls="chat-panel"
|
||||
aria-expanded="true"
|
||||
>
|
||||
Hide Chat
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div class="flex-1 flex items-center justify-center p-8 bg-black relative">
|
||||
<video id="player" class="max-h-full rounded-xl shadow-2xl" controls autoplay muted playsinline></video>
|
||||
<div id="now-playing-overlay" class="absolute bottom-8 left-8 bg-black/70 backdrop-blur-md px-6 py-3 rounded-2xl text-sm hidden">
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="text-emerald-400">● LIVE</div>
|
||||
<div id="overlay-title" class="font-medium"></div>
|
||||
<section class="viewer-content" aria-label="Live stream and chat layout">
|
||||
<section class="player-stage" aria-label="Video stage">
|
||||
<video id="player" class="player" controls autoplay muted playsinline></video>
|
||||
<div id="now-playing" class="now-playing-overlay" aria-live="polite">
|
||||
<span id="np-title">Loading...</span>
|
||||
<span id="np-next" class="np-next"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="p-4 bg-slate-900 border-t border-slate-800 text-xs text-slate-400 flex justify-between">
|
||||
<div>Latency: ~25s • Synced for all viewers (per ADR-001)</div>
|
||||
<div class="flex gap-6">
|
||||
<a href="#" onclick="switchRoom('main')" class="hover:text-white">Main Channel</a>
|
||||
<a href="#" onclick="switchRoom('chill')" class="hover:text-white">Chill Room</a>
|
||||
<a href="#" onclick="switchRoom('talk')" class="hover:text-white">Talk Room</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<aside id="chat-panel" class="chat-sidebar" aria-label="Chat panel">
|
||||
<header class="chat-header">
|
||||
<div class="chat-room-title">
|
||||
<span class="chat-live-marker" aria-hidden="true">●</span>
|
||||
<span id="room-name">Main Channel</span>
|
||||
</div>
|
||||
<button type="button" class="btn btn-secondary" onclick="togglePrivateDM()">New DM</button>
|
||||
</header>
|
||||
|
||||
<!-- Chat Sidebar -->
|
||||
<div class="w-96 flex flex-col border-l border-slate-800 bg-slate-900 chat-sidebar">
|
||||
<div class="p-4 border-b border-slate-700 font-medium flex items-center justify-between">
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="text-emerald-400">●</span>
|
||||
<span id="room-name">Main Channel</span>
|
||||
</div>
|
||||
<button onclick="togglePrivateDM()" class="text-xs px-3 py-1 bg-slate-700 hover:bg-slate-600 rounded-lg">New DM</button>
|
||||
</div>
|
||||
<section id="messages" class="chat-messages" aria-live="polite" aria-label="Message stream"></section>
|
||||
|
||||
<!-- Messages -->
|
||||
<div id="messages" class="flex-1 p-4 overflow-y-auto space-y-4 text-sm">
|
||||
<!-- Messages populated by JS -->
|
||||
</div>
|
||||
<section id="presence-panel" class="presence-panel" aria-label="Online users">
|
||||
<div id="presence-list" class="presence-list"></div>
|
||||
</section>
|
||||
|
||||
<!-- Input -->
|
||||
<div class="p-4 border-t border-slate-700">
|
||||
<div class="flex gap-2">
|
||||
<input id="message-input" type="text" placeholder="Type a message..." class="input flex-1 px-4 py-3 rounded-2xl text-sm focus:outline-none" onkeydown="if(event.key === 'Enter') sendMessage()">
|
||||
<button onclick="sendMessage()" class="bg-blue-600 hover:bg-blue-500 px-6 rounded-2xl text-sm font-medium">Send</button>
|
||||
</div>
|
||||
<div class="text-[10px] text-slate-500 mt-2 text-center">
|
||||
Synced with video • Moderation active
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
<div class="chat-input-area">
|
||||
<div class="chat-input-row">
|
||||
<label for="message-input" class="sr-only">Message input</label>
|
||||
<input
|
||||
id="message-input"
|
||||
type="text"
|
||||
placeholder="Type a message..."
|
||||
class="input"
|
||||
onkeydown="if (event.key === 'Enter') sendMessage();"
|
||||
/>
|
||||
<button type="button" class="btn btn-primary" onclick="sendMessage()">Send</button>
|
||||
</div>
|
||||
<p class="chat-note">Synced with video • Moderation active</p>
|
||||
</div>
|
||||
</aside>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+972
-174
File diff suppressed because it is too large.
Load diff
+10
-8
@@ -1,22 +1,24 @@
|
||||
{
|
||||
"name": "frontend",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"version": "1.0.10",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc && vite build",
|
||||
"preview": "vite preview"
|
||||
"preview": "vite preview",
|
||||
"lint": "tsc --noEmit",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitest/ui": "^4.1.10",
|
||||
"jsdom": "^30.0.1",
|
||||
"typescript": "~6.0.2",
|
||||
"vite": "^8.2.0"
|
||||
"vite": "^8.2.0",
|
||||
"vitest": "^4.1.10"
|
||||
},
|
||||
"dependencies": {
|
||||
"alpinejs": "^3.15.12",
|
||||
"autoprefixer": "^10.5.4",
|
||||
"hls.js": "^1.6.16",
|
||||
"postcss": "^8.5.25",
|
||||
"tailwindcss": "^4.3.3"
|
||||
"hls.js": "^1.6.16"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<div class="panel-header-row">
|
||||
<div>
|
||||
<h1>Analytics</h1>
|
||||
<p>Basic activity counters from chat history and moderation audit logs.</p>
|
||||
</div>
|
||||
<div class="button-row">
|
||||
<select id="analytics-range" class="text-input" style="width: auto">
|
||||
<option value="1h">Last hour</option>
|
||||
<option value="6h">Last 6 hours</option>
|
||||
<option value="24h" selected>Last 24 hours</option>
|
||||
<option value="7d">Last 7 days</option>
|
||||
</select>
|
||||
<button id="reload-analytics-btn" type="button" class="btn btn-secondary">Reload analytics</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="stat-grid">
|
||||
<article class="stat-card">
|
||||
<h2>Total Messages</h2>
|
||||
<p id="analytics-total-messages" class="stat-value">0</p>
|
||||
</article>
|
||||
<article class="stat-card">
|
||||
<h2>Last 24 Hours</h2>
|
||||
<p id="analytics-messages-24h" class="stat-value">0</p>
|
||||
</article>
|
||||
<article class="stat-card">
|
||||
<h2>Distinct Rooms</h2>
|
||||
<p id="analytics-distinct-rooms" class="stat-value">0</p>
|
||||
</article>
|
||||
<article class="stat-card">
|
||||
<h2>Moderation Actions</h2>
|
||||
<p id="analytics-moderation-actions" class="stat-value">0</p>
|
||||
</article>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<div class="panel-header-row">
|
||||
<h2>Top Rooms</h2>
|
||||
<p id="analytics-generated-at" class="small-muted">loading...</p>
|
||||
</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Room</th>
|
||||
<th>Messages</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="analytics-table-body"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p id="analytics-status" class="status-line" aria-live="polite">Loading analytics...</p>
|
||||
</div>
|
||||
@@ -0,0 +1,46 @@
|
||||
<div class="panel-header-row">
|
||||
<div>
|
||||
<h1>Chat Log</h1>
|
||||
<p>Browse and manage stored chat messages.</p>
|
||||
</div>
|
||||
<div class="button-row">
|
||||
<button id="reload-chat-log-btn" type="button" class="btn btn-secondary">Reload</button>
|
||||
<button id="bulk-delete-chat-log-btn" type="button" class="btn btn-danger" disabled>Delete Selected</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<div class="chat-log-filters">
|
||||
<input id="chat-log-filter-room" class="text-input" type="text" placeholder="Room" />
|
||||
<input id="chat-log-filter-username" class="text-input" type="text" placeholder="Username" />
|
||||
<input id="chat-log-filter-text" class="text-input" type="text" placeholder="Message text" />
|
||||
<button id="chat-log-filter-btn" type="button" class="btn btn-secondary">Filter</button>
|
||||
<button id="chat-log-clear-filter-btn" type="button" class="btn btn-secondary">Clear</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><input type="checkbox" id="chat-log-select-all" title="Select all" /></th>
|
||||
<th>ID</th>
|
||||
<th>Room</th>
|
||||
<th>Username</th>
|
||||
<th>Message</th>
|
||||
<th>Type</th>
|
||||
<th>Timestamp</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="chat-log-table-body"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="chat-log-pagination">
|
||||
<button id="chat-log-prev-btn" type="button" class="btn btn-secondary" disabled>Previous</button>
|
||||
<span id="chat-log-page-info" class="small-muted">Page 1</span>
|
||||
<button id="chat-log-next-btn" type="button" class="btn btn-secondary" disabled>Next</button>
|
||||
</div>
|
||||
<p id="chat-log-status" class="status-line" aria-live="polite">Loading chat log...</p>
|
||||
</div>
|
||||
@@ -0,0 +1,21 @@
|
||||
<h1>Dashboard</h1>
|
||||
<div class="stat-grid">
|
||||
<article class="stat-card">
|
||||
<h2>Concurrent Viewers</h2>
|
||||
<p id="stat-viewers" class="stat-value">0</p>
|
||||
</article>
|
||||
<article class="stat-card">
|
||||
<h2>Stored Messages</h2>
|
||||
<p id="stat-stored-messages" class="stat-value">0</p>
|
||||
</article>
|
||||
<article class="stat-card">
|
||||
<h2>Moderation Actions</h2>
|
||||
<p id="stat-moderation-actions" class="stat-value">0</p>
|
||||
</article>
|
||||
<article class="stat-card">
|
||||
<h2>Media Runtime</h2>
|
||||
<p id="stat-media-runtime" class="stat-value">00:00:00</p>
|
||||
<p id="stat-media-count" class="stat-caption">0 files</p>
|
||||
</article>
|
||||
</div>
|
||||
<p id="overview-status" class="status-line" aria-live="polite">Loading live admin overview...</p>
|
||||
@@ -0,0 +1,71 @@
|
||||
<div class="panel-header-row">
|
||||
<div>
|
||||
<h1>Moderation</h1>
|
||||
<p>Create moderation actions and review the recent immutable audit log.</p>
|
||||
</div>
|
||||
<div class="button-row">
|
||||
<button id="reload-moderation-btn" type="button" class="btn btn-secondary">Reload logs</button>
|
||||
<button id="export-chat-log-btn" type="button" class="btn btn-secondary">Export CSV</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="moderation-grid">
|
||||
<section class="panel-card">
|
||||
<h2>New Action</h2>
|
||||
<form id="moderation-form" class="form-stack">
|
||||
<label
|
||||
>Action
|
||||
<select id="mod-action" class="text-input">
|
||||
<option value="mute">mute</option>
|
||||
<option value="ban">ban</option>
|
||||
<option value="delete">delete</option>
|
||||
<option value="timeout">timeout</option>
|
||||
</select>
|
||||
</label>
|
||||
<label
|
||||
>Target
|
||||
<input id="mod-target" class="text-input" type="text" placeholder="username or message id" required />
|
||||
</label>
|
||||
<label
|
||||
>Moderator
|
||||
<input id="mod-moderator" class="text-input" type="text" placeholder="admin" />
|
||||
</label>
|
||||
<label
|
||||
>Room
|
||||
<input id="mod-room" class="text-input" type="text" placeholder="main" />
|
||||
</label>
|
||||
<label
|
||||
>Reason
|
||||
<textarea
|
||||
id="mod-reason"
|
||||
class="text-input text-area"
|
||||
placeholder="Explain why this action is needed"
|
||||
required
|
||||
></textarea>
|
||||
</label>
|
||||
<button id="mod-submit" type="submit" class="btn btn-primary">Submit action</button>
|
||||
<p id="moderation-status" class="status-line" aria-live="polite">Loading moderation logs...</p>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="panel-card">
|
||||
<div class="panel-header-row">
|
||||
<h2>Recent Audit Log</h2>
|
||||
<p class="small-muted">Latest entries</p>
|
||||
</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>When</th>
|
||||
<th>Action</th>
|
||||
<th>Target</th>
|
||||
<th>Moderator</th>
|
||||
<th>Reason</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="moderation-table-body"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
@@ -0,0 +1,18 @@
|
||||
<div class="panel-header-row">
|
||||
<div>
|
||||
<h1>Playlist</h1>
|
||||
<p>Edit the active concat playlist. Drag entries to reorder.</p>
|
||||
</div>
|
||||
<div class="button-row">
|
||||
<button id="reload-playlist-btn" type="button" class="btn btn-secondary">Reload</button>
|
||||
<button id="save-playlist-btn" type="button" class="btn btn-primary">Save playlist</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<div id="playlist-warnings" class="status-warnings" hidden></div>
|
||||
<ul id="playlist-sortable" class="sortable-list" aria-label="Playlist entries"></ul>
|
||||
<label for="playlist-content" class="sr-only">Playlist content (raw)</label>
|
||||
<textarea id="playlist-content" class="text-input text-area" spellcheck="false" rows="6"></textarea>
|
||||
<p id="playlist-status" class="status-line" aria-live="polite">Loading playlist...</p>
|
||||
</div>
|
||||
@@ -0,0 +1,56 @@
|
||||
<div class="panel-header-row">
|
||||
<div>
|
||||
<h1>Schedule</h1>
|
||||
<p>Read-only view of the currently configured playout order.</p>
|
||||
</div>
|
||||
<button id="reload-schedule-btn" type="button" class="btn btn-secondary">Reload schedule</button>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<p class="small-muted">
|
||||
<span id="schedule-matched-entries">0</span>/<span id="schedule-total-entries">0</span> matched • total runtime
|
||||
<span id="schedule-total-runtime">00:00:00</span>
|
||||
</p>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>Label</th>
|
||||
<th>Runtime</th>
|
||||
<th>Status</th>
|
||||
<th>Path</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="schedule-table-body"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p id="schedule-status" class="status-line" aria-live="polite">Loading schedule...</p>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<h2>Schedule Overrides</h2>
|
||||
<p>Insert scheduled overrides to play specific videos at specific times.</p>
|
||||
<form id="override-form" class="form-stack" style="max-width: 500px">
|
||||
<label>Media ID <input id="override-media-id" type="number" class="text-input" required /></label>
|
||||
<label>Start Time <input id="override-start" type="datetime-local" class="text-input" required /></label>
|
||||
<label>End Time <input id="override-end" type="datetime-local" class="text-input" required /></label>
|
||||
<label>Reason <input id="override-reason" type="text" class="text-input" placeholder="e.g. special event" /></label>
|
||||
<button type="submit" class="btn btn-primary">Add Override</button>
|
||||
</form>
|
||||
<p id="override-status" class="status-line" aria-live="polite"></p>
|
||||
<div class="table-wrap" style="margin-top: 1rem">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Media ID</th>
|
||||
<th>Start</th>
|
||||
<th>End</th>
|
||||
<th>Reason</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="override-table-body"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,75 @@
|
||||
<h1>System Configuration</h1>
|
||||
<div class="panel-card split-grid">
|
||||
<section>
|
||||
<h2>Redis Stats</h2>
|
||||
<dl class="kv-list">
|
||||
<div>
|
||||
<dt>Memory Used</dt>
|
||||
<dd id="system-redis-memory">loading...</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Connected Clients</dt>
|
||||
<dd id="system-redis-clients">0</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Redis Enabled</dt>
|
||||
<dd id="system-redis-enabled">no</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Storage</h2>
|
||||
<dl class="kv-list">
|
||||
<div>
|
||||
<dt>Database Driver</dt>
|
||||
<dd id="system-db-driver">loading...</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Stored Messages</dt>
|
||||
<dd id="system-stored-messages">0</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Last Refresh</dt>
|
||||
<dd id="system-generated-at">loading...</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Playlist Path</dt>
|
||||
<dd id="system-playlist-path">loading...</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Playlist Present</dt>
|
||||
<dd id="system-playlist-present">no</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<div class="panel-header-row">
|
||||
<h2>Service Health</h2>
|
||||
<button id="reload-system-btn" type="button" class="btn btn-secondary">Reload health</button>
|
||||
</div>
|
||||
<div id="system-services" class="service-list"></div>
|
||||
<p id="system-status" class="status-line" aria-live="polite">Loading system summary...</p>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<h2>Runtime Configuration</h2>
|
||||
<form id="config-form" class="form-stack" style="max-width: 400px">
|
||||
<label>Rate Limit Max <input id="config-rate-max" type="number" class="text-input" /></label>
|
||||
<label>Rate Limit Window (seconds) <input id="config-rate-window" type="number" class="text-input" /></label>
|
||||
<button type="submit" class="btn btn-primary">Save Config</button>
|
||||
</form>
|
||||
<p id="config-status" class="status-line" aria-live="polite"></p>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<h2>Service Control</h2>
|
||||
<p class="small-muted">Restart individual services (manual — container restart commands returned by API).</p>
|
||||
<div class="button-row">
|
||||
<button class="btn btn-secondary restart-btn" data-service="playout">Restart Playout</button>
|
||||
<button class="btn btn-secondary restart-btn" data-service="chat">Restart Chat</button>
|
||||
<button class="btn btn-secondary restart-btn" data-service="nginx">Restart Nginx</button>
|
||||
</div>
|
||||
<p id="restart-status" class="status-line" aria-live="polite"></p>
|
||||
</div>
|
||||
@@ -0,0 +1,43 @@
|
||||
<div class="panel-header-row">
|
||||
<div>
|
||||
<h1>Upload Video</h1>
|
||||
<p>Upload media files into the local media library mounted for playout.</p>
|
||||
</div>
|
||||
<button id="reload-media-btn" type="button" class="btn btn-secondary">Reload library</button>
|
||||
</div>
|
||||
|
||||
<div id="upload-dropzone" class="upload-dropzone">
|
||||
<input type="file" id="video-upload" accept="video/*,.mp4,.mov,.mkv,.webm" hidden />
|
||||
<button id="upload-trigger-btn" type="button" class="btn btn-primary btn-large">
|
||||
Drag and Drop or Click to Upload
|
||||
</button>
|
||||
<p class="small-muted">Resumable chunk upload enabled • up to 2GB per file.</p>
|
||||
<div id="media-upload-progress-wrap" class="upload-progress" hidden>
|
||||
<progress id="media-upload-progress" value="0" max="100"></progress>
|
||||
<p id="media-upload-progress-text" class="small-muted">Uploading (0%)</p>
|
||||
</div>
|
||||
<p id="media-status" class="status-line" aria-live="polite">Loading media library...</p>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<div class="panel-header-row">
|
||||
<h2>Media Library</h2>
|
||||
<p id="media-generated-at" class="small-muted">loading...</p>
|
||||
</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>File</th>
|
||||
<th>Title</th>
|
||||
<th>Runtime</th>
|
||||
<th>Status</th>
|
||||
<th>Size</th>
|
||||
<th>Modified</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="media-table-body"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,24 @@
|
||||
<div class="panel-header-row">
|
||||
<div>
|
||||
<h1>Users</h1>
|
||||
<p>Current room presence derived from Redis presence keys.</p>
|
||||
</div>
|
||||
<button id="reload-users-btn" type="button" class="btn btn-secondary">Reload users</button>
|
||||
</div>
|
||||
|
||||
<div class="panel-card">
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Username</th>
|
||||
<th>Room</th>
|
||||
<th>Online</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="users-table-body"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p id="users-status" class="status-line" aria-live="polite">Loading active users...</p>
|
||||
</div>
|
||||
@@ -0,0 +1,142 @@
|
||||
/// <reference types="vitest" />
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
|
||||
// Mock fetch globally
|
||||
let fetchCalls: { url: string; options?: RequestInit }[] = [];
|
||||
let mockFetchResponse: any = { ok: true, json: () => Promise.resolve({}) };
|
||||
|
||||
beforeEach(() => {
|
||||
fetchCalls = [];
|
||||
globalThis.fetch = vi.fn((url: string, options?: RequestInit) => {
|
||||
fetchCalls.push({ url, options });
|
||||
return Promise.resolve(
|
||||
new Response(JSON.stringify(mockFetchResponse.json ? mockFetchResponse.json() : mockFetchResponse), {
|
||||
status: mockFetchResponse.ok ? 200 : mockFetchResponse.status || 500,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}),
|
||||
);
|
||||
}) as any;
|
||||
|
||||
// Mock localStorage
|
||||
const storage = new Map<string, string>();
|
||||
Object.defineProperty(window, "localStorage", {
|
||||
value: {
|
||||
getItem: vi.fn((key: string) => storage.get(key) ?? null),
|
||||
setItem: vi.fn((key: string, val: string) => storage.set(key, val)),
|
||||
removeItem: vi.fn((key: string) => storage.delete(key)),
|
||||
},
|
||||
writable: true,
|
||||
});
|
||||
|
||||
// Mock window.confirm/prompt
|
||||
vi.spyOn(window, "confirm").mockReturnValue(true);
|
||||
vi.spyOn(window, "prompt").mockReturnValue(null);
|
||||
vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
// Import after mocks are set
|
||||
import "../src/admin";
|
||||
|
||||
describe("admin.ts initialization", () => {
|
||||
it("loads and exposes tab switching", () => {
|
||||
// After import, init() runs on DOMContentLoaded
|
||||
// Test that the document has admin page structure
|
||||
expect(document.body).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("upload progress helpers", () => {
|
||||
it("renders upload progress correctly", async () => {
|
||||
// Set up DOM for upload page
|
||||
document.body.innerHTML = `
|
||||
<div id="media-upload-progress-wrap" hidden>
|
||||
<progress id="media-upload-progress" max="100" value="0"></progress>
|
||||
<span id="media-upload-progress-text"></span>
|
||||
</div>
|
||||
<div id="tab-content"></div>
|
||||
`;
|
||||
|
||||
// Re-import triggers init again — but we need fresh module state
|
||||
// For now test internal helper access via body
|
||||
const wrap = document.getElementById("media-upload-progress-wrap")!;
|
||||
const bar = document.getElementById("media-upload-progress") as HTMLProgressElement;
|
||||
const text = document.getElementById("media-upload-progress-text")!;
|
||||
|
||||
expect(wrap.hidden).toBe(true);
|
||||
|
||||
// Simulate what setUploadProgress would do
|
||||
wrap.hidden = false;
|
||||
bar.max = 100;
|
||||
bar.value = 50;
|
||||
text.textContent = "Uploading (50%)";
|
||||
|
||||
expect(wrap.hidden).toBe(false);
|
||||
expect(bar.value).toBe(50);
|
||||
expect(text.textContent).toBe("Uploading (50%)");
|
||||
});
|
||||
});
|
||||
|
||||
describe("file fingerprint", () => {
|
||||
it("generates unique fingerprints for different files", () => {
|
||||
const f1 = new File(["a"], "video1.mp4", { lastModified: 1 });
|
||||
const f2 = new File(["b"], "video2.mp4", { lastModified: 2 });
|
||||
|
||||
const fp1 = `${f1.name}:${f1.size}:${f1.lastModified}`;
|
||||
const fp2 = `${f2.name}:${f2.size}:${f2.lastModified}`;
|
||||
|
||||
expect(fp1).not.toBe(fp2);
|
||||
expect(fp1).toBe("video1.mp4:1:1");
|
||||
expect(fp2).toBe("video2.mp4:1:2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("MAX_UPLOAD_BYTES constant", () => {
|
||||
it("equals 2GB", () => {
|
||||
expect(2 * 1024 * 1024 * 1024).toBe(2147483648);
|
||||
});
|
||||
});
|
||||
|
||||
describe("API calls on tab load", () => {
|
||||
beforeEach(() => {
|
||||
document.body.innerHTML = `
|
||||
<div id="tab-content"><div id="overview-content"></div></div>
|
||||
<nav class="admin-nav">
|
||||
<button data-tab="dashboard">Dashboard</button>
|
||||
<button data-tab="analytics">Analytics</button>
|
||||
<button data-tab="playlist">Playlist</button>
|
||||
<button data-tab="schedule">Schedule</button>
|
||||
<button data-tab="system">System</button>
|
||||
<button data-tab="upload">Upload</button>
|
||||
<button data-tab="users">Users</button>
|
||||
<button data-tab="moderation">Moderation</button>
|
||||
<button data-tab="chat-log">Chat Log</button>
|
||||
</nav>
|
||||
<div id="overview-stats"></div>
|
||||
<div id="analytics-stats"></div>
|
||||
<div id="playlist-editor"></div>
|
||||
<div id="schedule-list"></div>
|
||||
<div id="system-summary"></div>
|
||||
<div id="media-upload-progress-wrap" hidden>
|
||||
<progress id="media-upload-progress"></progress>
|
||||
<span id="media-upload-progress-text"></span>
|
||||
</div>
|
||||
<div id="media-table-body"></div>
|
||||
<div id="users-table-body"></div>
|
||||
<div id="moderation-log-table-body"></div>
|
||||
<div id="chat-log-table-body"></div>
|
||||
<div id="chat-log-pagination"></div>
|
||||
<div id="config-editor"></div>
|
||||
<span id="restart-status"></span>
|
||||
<div id="schedule-override-list"></div>
|
||||
`;
|
||||
});
|
||||
|
||||
it("can be imported without crashing", () => {
|
||||
// admin.ts initialization should not throw
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,9 @@
|
||||
export function setupCounter(element: HTMLButtonElement) {
|
||||
let counter = 0
|
||||
let counter = 0;
|
||||
const setCounter = (count: number) => {
|
||||
counter = count
|
||||
element.innerHTML = `Count is ${counter}`
|
||||
}
|
||||
element.addEventListener('click', () => setCounter(counter + 1))
|
||||
setCounter(0)
|
||||
counter = count;
|
||||
element.innerHTML = `Count is ${counter}`;
|
||||
};
|
||||
element.addEventListener("click", () => setCounter(counter + 1));
|
||||
setCounter(0);
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
/// <reference types="vitest" />
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
|
||||
// Mock hls.js
|
||||
vi.mock("hls.js", () => ({
|
||||
default: vi.fn().mockImplementation(() => ({
|
||||
loadSource: vi.fn(),
|
||||
attachMedia: vi.fn(),
|
||||
on: vi.fn(),
|
||||
destroy: vi.fn(),
|
||||
})),
|
||||
Events: { MANIFEST_PARSED: "hlsManifestParsed" },
|
||||
}));
|
||||
|
||||
// Mock WebSocket
|
||||
class MockWebSocket {
|
||||
url: string;
|
||||
readyState = 0;
|
||||
onopen: (() => void) | null = null;
|
||||
onmessage: ((e: { data: string }) => void) | null = null;
|
||||
onclose: (() => void) | null = null;
|
||||
send = vi.fn();
|
||||
close = vi.fn();
|
||||
|
||||
constructor(url: string) {
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
|
||||
// Suppress console noise
|
||||
beforeEach(() => {
|
||||
vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
});
|
||||
|
||||
let origWebSocket: any;
|
||||
let origFetch: any;
|
||||
|
||||
beforeEach(() => {
|
||||
origWebSocket = (globalThis as any).WebSocket;
|
||||
origFetch = globalThis.fetch;
|
||||
(globalThis as any).WebSocket = MockWebSocket;
|
||||
|
||||
// Mock fetch (schedule, etc.) — return empty schedule
|
||||
globalThis.fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ entries: [], total: 0, generatedAt: "" }),
|
||||
});
|
||||
|
||||
// Set up DOM from index.html
|
||||
document.body.innerHTML = `
|
||||
<main class="viewer-shell">
|
||||
<header class="viewer-header">
|
||||
<button id="chat-toggle" type="button">Hide Chat</button>
|
||||
</header>
|
||||
<section class="viewer-content">
|
||||
<section class="player-stage">
|
||||
<video id="player" class="player" controls autoplay muted playsinline></video>
|
||||
<div id="now-playing">
|
||||
<span id="np-title">Loading...</span>
|
||||
<span id="np-next" class="np-next"></span>
|
||||
</div>
|
||||
</section>
|
||||
<aside id="chat-panel" class="chat-sidebar">
|
||||
<div class="chat-header">
|
||||
<span id="room-name">Main Channel</span>
|
||||
<button type="button" onclick="togglePrivateDM()">New DM</button>
|
||||
</div>
|
||||
<section id="messages" class="chat-messages"></section>
|
||||
<section id="presence-panel" class="presence-panel">
|
||||
<div id="presence-list" class="presence-list"></div>
|
||||
</section>
|
||||
<div class="chat-input-area">
|
||||
<input id="message-input" type="text" placeholder="Type a message..." />
|
||||
<button type="button" onclick="sendMessage()">Send</button>
|
||||
</div>
|
||||
</aside>
|
||||
</section>
|
||||
</main>
|
||||
`;
|
||||
|
||||
// Stub prompt for DM
|
||||
(globalThis as any).prompt = vi.fn();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
(globalThis as any).WebSocket = origWebSocket;
|
||||
globalThis.fetch = origFetch;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
// Must import after DOM setup
|
||||
import "../src/main";
|
||||
|
||||
describe("chat toggle", () => {
|
||||
it("toggles chat panel visibility", () => {
|
||||
const panel = document.getElementById("chat-panel")!;
|
||||
const toggle = document.getElementById("chat-toggle")!;
|
||||
|
||||
expect(toggle.textContent).toBe("Hide Chat");
|
||||
|
||||
window.toggleChatPanel();
|
||||
expect(toggle.textContent).toBe("Show Chat");
|
||||
|
||||
window.toggleChatPanel();
|
||||
expect(toggle.textContent).toBe("Hide Chat");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendMessage", () => {
|
||||
it("clears input after sending non-empty text", () => {
|
||||
const input = document.getElementById("message-input") as HTMLInputElement;
|
||||
input.value = "hello!";
|
||||
|
||||
// sendMessage requires ws to be non-null — ws set in initPlayerAndChat
|
||||
// The DOMContentLoaded triggers initPlayerAndChat which creates ws
|
||||
// Wait for async init, then set ws manually
|
||||
// For now just verify input state logic
|
||||
window.sendMessage();
|
||||
// ws is null at this point, so message won't send but input won't clear either
|
||||
// Verify input not cleared (ws check fails before clear)
|
||||
expect(input.value).toBe("hello!");
|
||||
});
|
||||
|
||||
it("does nothing with empty input", () => {
|
||||
const input = document.getElementById("message-input") as HTMLInputElement;
|
||||
input.value = " ";
|
||||
window.sendMessage();
|
||||
// No error thrown = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe("switchRoom", () => {
|
||||
it("updates room name on switch", () => {
|
||||
window.switchRoom("lobby");
|
||||
const roomName = document.getElementById("room-name")!;
|
||||
expect(roomName.textContent).toBe("Lobby Room");
|
||||
});
|
||||
|
||||
it("shows Main Channel for main room", () => {
|
||||
window.switchRoom("main");
|
||||
const roomName = document.getElementById("room-name")!;
|
||||
expect(roomName.textContent).toBe("Main Channel");
|
||||
});
|
||||
});
|
||||
|
||||
describe("togglePrivateDM", () => {
|
||||
it("prompts for username and switches room", () => {
|
||||
// Stub prompt to return a value this time
|
||||
vi.spyOn(window, "prompt").mockReturnValue("bob");
|
||||
|
||||
const switchRoomSpy = vi.spyOn(window, "switchRoom");
|
||||
window.togglePrivateDM();
|
||||
|
||||
expect(window.prompt).toHaveBeenCalled();
|
||||
expect(switchRoomSpy).not.toHaveBeenCalled(); // username is "viewerNNN" which gets set randomly
|
||||
|
||||
switchRoomSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
+105
-86
@@ -1,20 +1,22 @@
|
||||
import Hls from "hls.js";
|
||||
import "./style.css";
|
||||
|
||||
// Main application state with Alpine.js
|
||||
type HlsClass = (typeof import("hls.js"))["default"];
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
app: () => any;
|
||||
switchRoom: (room: string) => void;
|
||||
sendMessage: () => void;
|
||||
togglePrivateDM: () => void;
|
||||
toggleChatPanel: () => void;
|
||||
}
|
||||
}
|
||||
|
||||
let hls: Hls | null = null;
|
||||
let hls: InstanceType<HlsClass> | null = null;
|
||||
let ws: WebSocket | null = null;
|
||||
let currentRoom = "main";
|
||||
let username = "viewer" + Math.floor(Math.random() * 1000);
|
||||
let chatVisible = true;
|
||||
const onlineUsers = new Set<string>();
|
||||
|
||||
function resolveWebSocketBaseUrl() {
|
||||
const configuredUrl = import.meta.env.VITE_PUBLIC_WS_URL?.trim();
|
||||
@@ -30,24 +32,16 @@ function resolveWebSocketBaseUrl() {
|
||||
function resolveManifestUrl() {
|
||||
const configuredUrl = import.meta.env.VITE_PUBLIC_HLS_URL?.trim();
|
||||
const cacheBust = `stream=${Date.now()}`;
|
||||
const sameOriginManifestUrl = new URL(
|
||||
"/hls/playlist.m3u8",
|
||||
window.location.origin,
|
||||
);
|
||||
const sameOriginManifestUrl = new URL("/hls/playlist.m3u8", window.location.origin);
|
||||
sameOriginManifestUrl.searchParams.set("stream", cacheBust);
|
||||
|
||||
const allowCrossOriginManifest =
|
||||
import.meta.env.VITE_PUBLIC_HLS_CROSS_ORIGIN?.trim().toLowerCase() ===
|
||||
"true";
|
||||
const allowCrossOriginManifest = import.meta.env.VITE_PUBLIC_HLS_CROSS_ORIGIN?.trim().toLowerCase() === "true";
|
||||
|
||||
if (configuredUrl) {
|
||||
const manifestUrl = new URL(configuredUrl);
|
||||
manifestUrl.searchParams.set("stream", cacheBust);
|
||||
|
||||
if (
|
||||
!allowCrossOriginManifest &&
|
||||
manifestUrl.origin !== window.location.origin
|
||||
) {
|
||||
if (!allowCrossOriginManifest && manifestUrl.origin !== window.location.origin) {
|
||||
return sameOriginManifestUrl.toString();
|
||||
}
|
||||
|
||||
@@ -73,6 +67,19 @@ function connectWebSocket(room: string) {
|
||||
ws.onmessage = (event) => {
|
||||
const msg = JSON.parse(event.data);
|
||||
addMessage(msg.type || "public", msg.text, msg.username, msg.timestamp);
|
||||
|
||||
// Track presence from system join/leave messages
|
||||
if (msg.type === "system" && msg.username === "system") {
|
||||
const joined = msg.text?.match(/^(\S+) joined /);
|
||||
const left = msg.text?.match(/^(\S+) left /);
|
||||
if (joined) {
|
||||
onlineUsers.add(joined[1]);
|
||||
renderPresence();
|
||||
} else if (left) {
|
||||
onlineUsers.delete(left[1]);
|
||||
renderPresence();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
@@ -81,25 +88,22 @@ function connectWebSocket(room: string) {
|
||||
};
|
||||
}
|
||||
|
||||
function addMessage(
|
||||
type: string,
|
||||
text: string,
|
||||
sender = "",
|
||||
timestamp = new Date(),
|
||||
) {
|
||||
function addMessage(type: string, text: string, sender = "", timestamp = new Date()) {
|
||||
const messages = document.getElementById("messages")!;
|
||||
const div = document.createElement("div");
|
||||
div.className = `message flex ${type === "system" ? "justify-center" : sender === username ? "justify-end" : "justify-start"}`;
|
||||
div.className = "message-row";
|
||||
|
||||
if (type === "system") {
|
||||
div.innerHTML = `<div class="text-xs text-slate-400 bg-slate-800 px-3 py-1 rounded-full">${text}</div>`;
|
||||
div.classList.add("message-system-row");
|
||||
div.innerHTML = `<div class="message-system">${text}</div>`;
|
||||
} else {
|
||||
div.classList.add(sender === username ? "message-self-row" : "message-other-row");
|
||||
div.innerHTML = `
|
||||
<div class="${sender === username ? "bg-blue-600" : "bg-slate-700"} px-4 py-2 rounded-3xl max-w-[75%]">
|
||||
${sender && sender !== username ? `<div class="text-xs text-blue-300">${sender}</div>` : ""}
|
||||
<div>${text}</div>
|
||||
<div class="text-[10px] text-slate-400 mt-1">${new Date(timestamp).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}</div>
|
||||
</div>
|
||||
<article class="message-bubble ${sender === username ? "message-self" : "message-other"}">
|
||||
${sender && sender !== username ? `<div class="message-sender">${sender}</div>` : ""}
|
||||
<div class="message-text">${text}</div>
|
||||
<time class="message-time">${new Date(timestamp).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}</time>
|
||||
</article>
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -107,6 +111,76 @@ function addMessage(
|
||||
messages.scrollTop = messages.scrollHeight;
|
||||
}
|
||||
|
||||
function renderPresence() {
|
||||
const list = document.getElementById("presence-list");
|
||||
if (!list) return;
|
||||
const sorted = [...onlineUsers].sort();
|
||||
list.innerHTML = sorted
|
||||
.map((u) => `<span class="presence-dot" title="${u}"><span class="presence-indicator"></span>${u}</span>`)
|
||||
.join("");
|
||||
}
|
||||
|
||||
async function refreshSchedule() {
|
||||
try {
|
||||
const res = await fetch("/api/schedule");
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
const entries = data.entries as Array<{ title: string; status: string }>;
|
||||
if (entries.length > 0) {
|
||||
const np = document.getElementById("np-title");
|
||||
const nn = document.getElementById("np-next");
|
||||
if (np) np.textContent = `Now: ${entries[0].title}`;
|
||||
if (nn && entries.length > 1) {
|
||||
nn.textContent = `Up next: ${entries[1].title}`;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Schedule unavailable — non-critical
|
||||
}
|
||||
}
|
||||
|
||||
async function initPlayerAndChat() {
|
||||
const video = document.getElementById("player") as HTMLVideoElement;
|
||||
const manifestUrl = resolveManifestUrl();
|
||||
|
||||
if (video.canPlayType("application/vnd.apple.mpegurl")) {
|
||||
video.src = manifestUrl;
|
||||
video.addEventListener("loadedmetadata", () => video.play());
|
||||
} else {
|
||||
try {
|
||||
const { default: Hls } = await import("hls.js");
|
||||
if (Hls.isSupported()) {
|
||||
hls = new Hls();
|
||||
hls.loadSource(manifestUrl);
|
||||
hls.attachMedia(video);
|
||||
hls.on(Hls.Events.MANIFEST_PARSED, () => video.play());
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to load HLS player", error);
|
||||
addMessage("system", "Unable to initialize video player.");
|
||||
}
|
||||
}
|
||||
|
||||
connectWebSocket("main");
|
||||
|
||||
// Start schedule polling
|
||||
refreshSchedule();
|
||||
setInterval(refreshSchedule, 30000);
|
||||
|
||||
console.log("Linear Void Frontend initialized - HLS Player + Chat Sidebar ready");
|
||||
}
|
||||
|
||||
function toggleChatPanel() {
|
||||
const shell = document.querySelector(".viewer-shell");
|
||||
const toggle = document.getElementById("chat-toggle");
|
||||
if (!shell || !toggle) return;
|
||||
|
||||
chatVisible = !chatVisible;
|
||||
shell.classList.toggle("chat-collapsed", !chatVisible);
|
||||
toggle.textContent = chatVisible ? "Hide Chat" : "Show Chat";
|
||||
toggle.setAttribute("aria-expanded", String(chatVisible));
|
||||
}
|
||||
|
||||
function sendMessage() {
|
||||
const input = document.getElementById("message-input") as HTMLInputElement;
|
||||
const text = input.value.trim();
|
||||
@@ -126,9 +200,7 @@ function sendMessage() {
|
||||
function switchRoom(room: string) {
|
||||
currentRoom = room;
|
||||
document.getElementById("room-name")!.textContent =
|
||||
room === "main"
|
||||
? "Main Channel"
|
||||
: room.charAt(0).toUpperCase() + room.slice(1) + " Room";
|
||||
room === "main" ? "Main Channel" : room.charAt(0).toUpperCase() + room.slice(1) + " Room";
|
||||
addMessage("system", `Switched to ${room} room`);
|
||||
connectWebSocket(room);
|
||||
}
|
||||
@@ -141,64 +213,11 @@ function togglePrivateDM() {
|
||||
}
|
||||
}
|
||||
|
||||
// Alpine.js app
|
||||
window.app = () => {
|
||||
return {
|
||||
init() {
|
||||
// Initialize Tailwind
|
||||
// @ts-ignore
|
||||
tailwind.config = {
|
||||
content: [],
|
||||
theme: { extend: {} },
|
||||
};
|
||||
|
||||
// Initialize HLS Player
|
||||
const video = document.getElementById("player") as HTMLVideoElement;
|
||||
const manifestUrl = resolveManifestUrl();
|
||||
|
||||
if (Hls.isSupported()) {
|
||||
hls = new Hls();
|
||||
hls.loadSource(manifestUrl);
|
||||
hls.attachMedia(video);
|
||||
hls.on(Hls.Events.MANIFEST_PARSED, () => video.play());
|
||||
} else if (video.canPlayType("application/vnd.apple.mpegurl")) {
|
||||
video.src = manifestUrl;
|
||||
video.addEventListener("loadedmetadata", () => video.play());
|
||||
}
|
||||
|
||||
// Connect to default chat room
|
||||
connectWebSocket("main");
|
||||
|
||||
// Simulate now playing
|
||||
setInterval(() => {
|
||||
const titles = [
|
||||
"Midnight Jazz Lounge",
|
||||
"Synthwave Nights",
|
||||
"Classic Cinema",
|
||||
"Ambient Void",
|
||||
];
|
||||
document.getElementById("now-playing")!.textContent =
|
||||
titles[Math.floor(Math.random() * titles.length)];
|
||||
const overlay = document.getElementById("now-playing-overlay")!;
|
||||
overlay.classList.remove("hidden");
|
||||
document.getElementById("overlay-title")!.textContent =
|
||||
titles[Math.floor(Math.random() * titles.length)];
|
||||
}, 15000);
|
||||
|
||||
console.log(
|
||||
"Linear Void Frontend initialized - HLS Player + Chat Sidebar ready",
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
// Make functions global for onclick handlers
|
||||
window.switchRoom = switchRoom;
|
||||
window.sendMessage = sendMessage;
|
||||
window.togglePrivateDM = togglePrivateDM;
|
||||
window.toggleChatPanel = toggleChatPanel;
|
||||
|
||||
// Auto initialize when loaded
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// @ts-ignore
|
||||
window.app().init();
|
||||
void initPlayerAndChat();
|
||||
});
|
||||
+1023
-289
File diff suppressed because it is too large.
Load diff
@@ -1,8 +0,0 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
@@ -3,6 +3,7 @@ import { defineConfig } from "vite";
|
||||
export default defineConfig({
|
||||
envDir: "..",
|
||||
build: {
|
||||
chunkSizeWarningLimit: 600,
|
||||
rollupOptions: {
|
||||
input: {
|
||||
main: "index.html",
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
environment: "jsdom",
|
||||
include: ["src/**/*.test.ts"],
|
||||
},
|
||||
});
|
||||
@@ -1,25 +1,25 @@
|
||||
# Cloudflare Cache Rules for Linear Void (per ADR-001)
|
||||
|
||||
## Recommended Cache Rules for media.example.com
|
||||
## Recommended Cache Rules for media.linearvoid.tv
|
||||
|
||||
1. **.ts segments (immutable)**
|
||||
- Expression: (http.host eq "media.example.com" and http.request.uri.path contains ".ts")
|
||||
1. **.m4s and .ts segments (immutable)**
|
||||
- Expression: (http.host eq "media.linearvoid.tv" and (http.request.uri.path contains ".ts" or http.request.uri.path contains ".m4s"))
|
||||
- Cache status: Eligible for cache
|
||||
- Edge Cache TTL: 1 month (or `Cache-Control: public, max-age=31536000, immutable` from origin)
|
||||
- Cache Everything: Yes
|
||||
|
||||
2. **.m3u8 manifests (live)**
|
||||
- Expression: (http.host eq "media.example.com" and http.request.uri.path contains ".m3u8")
|
||||
- Expression: (http.host eq "media.linearvoid.tv" and http.request.uri.path contains ".m3u8")
|
||||
- Cache status: Eligible for cache
|
||||
- Edge Cache TTL: 10 seconds
|
||||
- Cache-Control: public, max-age=10, must-revalidate
|
||||
- Edge Cache TTL: 2 seconds
|
||||
- Cache-Control: public, max-age=2, must-revalidate
|
||||
|
||||
3. **Static assets**
|
||||
- Expression: (http.host eq "example.com" and http.request.uri.path contains ".js" or http.request.uri.path contains ".css")
|
||||
- Expression: (http.host eq "linearvoid.tv" and http.request.uri.path contains ".js" or http.request.uri.path contains ".css")
|
||||
- Edge Cache TTL: 1 year (with hash in filename for immutable caching)
|
||||
|
||||
**Tiered Cache**: Enable Smart Tiered Cache for R2 custom domain to reduce origin requests.
|
||||
|
||||
**WAF**: Enable basic rate limiting and bot management for /ws and admin routes.
|
||||
|
||||
These rules ensure high cache hit ratio for .ts segments while keeping live manifests fresh. Monitor cache hit ratio in Cloudflare analytics.
|
||||
These rules ensure high cache hit ratio for .m4s and .ts segments while keeping live manifests fresh. Monitor cache hit ratio in Cloudflare analytics.
|
||||
@@ -6,8 +6,8 @@ addEventListener("fetch", (event) => {
|
||||
async function handleRequest(request) {
|
||||
const url = new URL(request.url);
|
||||
|
||||
// Add immutable cache headers for .ts segments
|
||||
if (url.pathname.endsWith(".ts")) {
|
||||
// Add immutable cache headers for .m4s segments
|
||||
if (url.pathname.endsWith(".m4s")) {
|
||||
const response = await fetch(request);
|
||||
const headers = new Headers(response.headers);
|
||||
headers.set("Cache-Control", "public, max-age=31536000, immutable");
|
||||
|
||||
@@ -24,6 +24,9 @@ http {
|
||||
listen 80 default_server;
|
||||
server_name linearvoid.tv localhost 127.0.0.1;
|
||||
|
||||
# Resumable admin upload chunks are 8MB by default; allow safe headroom.
|
||||
client_max_body_size 64m;
|
||||
|
||||
root /srv/frontend;
|
||||
index index.html;
|
||||
|
||||
@@ -157,18 +160,36 @@ http {
|
||||
}
|
||||
|
||||
location = /hls/playlist.m3u8 {
|
||||
if ($request_method = OPTIONS) {
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
add_header Access-Control-Allow-Methods "GET, OPTIONS";
|
||||
add_header Access-Control-Max-Age 86400;
|
||||
return 204;
|
||||
}
|
||||
alias /srv/hls/playlist.m3u8;
|
||||
add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
}
|
||||
|
||||
location = /hls/init.mp4 {
|
||||
if ($request_method = OPTIONS) {
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
add_header Access-Control-Allow-Methods "GET, OPTIONS";
|
||||
add_header Access-Control-Max-Age 86400;
|
||||
return 204;
|
||||
}
|
||||
alias /srv/hls/init.mp4;
|
||||
add_header Cache-Control "public, max-age=31536000, immutable";
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
}
|
||||
|
||||
location ~* ^/hls/(.+\.(ts|m4s))$ {
|
||||
if ($request_method = OPTIONS) {
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
add_header Access-Control-Allow-Methods "GET, OPTIONS";
|
||||
add_header Access-Control-Max-Age 86400;
|
||||
return 204;
|
||||
}
|
||||
alias /srv/hls/$1;
|
||||
add_header Cache-Control "public, max-age=31536000, immutable";
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
@@ -176,15 +197,29 @@ http {
|
||||
|
||||
# Local/dev fallback HLS origin. Production media should use MEDIA_DOMAIN.
|
||||
location /hls/ {
|
||||
if ($request_method = OPTIONS) {
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
add_header Access-Control-Allow-Methods "GET, OPTIONS";
|
||||
add_header Access-Control-Max-Age 86400;
|
||||
return 204;
|
||||
}
|
||||
alias /srv/hls/;
|
||||
add_header Cache-Control "public, max-age=60";
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
}
|
||||
|
||||
# Health checks
|
||||
# Health checks — aggregated from upstream services
|
||||
location /health {
|
||||
return 200 "healthy";
|
||||
add_header Content-Type text/plain;
|
||||
default_type application/json;
|
||||
return 200 '{"status":"ok","service":"nginx","timestamp":"$time_iso8601"}';
|
||||
}
|
||||
|
||||
location /nginx-status {
|
||||
stub_status on;
|
||||
allow 127.0.0.1;
|
||||
allow 172.16.0.0/12;
|
||||
allow 192.168.0.0/16;
|
||||
deny all;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,12 @@
|
||||
apiVersion: 1
|
||||
|
||||
providers:
|
||||
- name: "default"
|
||||
orgId: 1
|
||||
folder: ""
|
||||
type: file
|
||||
disableDeletion: false
|
||||
updateIntervalSeconds: 10
|
||||
allowUiUpdates: true
|
||||
options:
|
||||
path: /etc/grafana/provisioning/dashboards
|
||||
@@ -0,0 +1,9 @@
|
||||
apiVersion: 1
|
||||
|
||||
datasources:
|
||||
- name: Prometheus
|
||||
type: prometheus
|
||||
access: proxy
|
||||
url: http://prometheus:9090
|
||||
isDefault: true
|
||||
editable: true
|
||||
@@ -0,0 +1,74 @@
|
||||
groups:
|
||||
- name: linearvoid
|
||||
rules:
|
||||
- alert: ChatDown
|
||||
expr: up{job="chat"} == 0
|
||||
for: 2m
|
||||
labels:
|
||||
severity: critical
|
||||
annotations:
|
||||
summary: "Chat service is down"
|
||||
description: "Chat service has been unreachable for 2 minutes."
|
||||
|
||||
- alert: PlayoutDown
|
||||
expr: up{job="playout"} == 0
|
||||
for: 2m
|
||||
labels:
|
||||
severity: critical
|
||||
annotations:
|
||||
summary: "Playout service is down"
|
||||
description: "Playout service has been unreachable for 2 minutes."
|
||||
|
||||
- alert: RedisDown
|
||||
expr: linearvoid_chat_redis_up == 0
|
||||
for: 2m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Redis is unreachable"
|
||||
description: "Chat service cannot reach Redis. Rate limiting and presence tracking degraded."
|
||||
|
||||
- alert: PlayoutSlateActive
|
||||
expr: linearvoid_playout_mode == 1
|
||||
for: 1m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Playout in slate fallback mode"
|
||||
description: "Playout has switched to emergency slate. Check playout logs."
|
||||
|
||||
- alert: HighChatLatency
|
||||
expr: rate(linearvoid_chat_messages_total[5m]) < 0.01
|
||||
for: 5m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Unusually low chat message rate"
|
||||
description: "Message rate below 0.01/s for 5 minutes. Check chat service health."
|
||||
|
||||
- alert: PrometheusWALFailing
|
||||
expr: increase(prometheus_tsdb_wal_writes_failed_total[5m]) > 0
|
||||
for: 5m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Prometheus WAL writes failing"
|
||||
description: "Prometheus TSDB WAL write failures detected."
|
||||
|
||||
- alert: HighCPU
|
||||
expr: 100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
|
||||
for: 10m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "High CPU usage"
|
||||
description: "CPU usage above 80% for 10 minutes on {{ $labels.instance }}."
|
||||
|
||||
- alert: LowDiskSpace
|
||||
expr: (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100 < 10
|
||||
for: 5m
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: "Low disk space"
|
||||
description: "Less than 10% disk space available on {{ $labels.instance }}."
|
||||
@@ -1,10 +1,28 @@
|
||||
global:
|
||||
scrape_interval: 15s
|
||||
|
||||
rule_files:
|
||||
- "/etc/prometheus/alerts.yml"
|
||||
|
||||
scrape_configs:
|
||||
- job_name: "chat"
|
||||
static_configs:
|
||||
- targets: ["chat:3000"]
|
||||
- job_name: "playout"
|
||||
static_configs:
|
||||
- targets: ["playout:8081"]
|
||||
- job_name: "nginx"
|
||||
static_configs:
|
||||
- targets: ["nginx-exporter:9113"]
|
||||
- job_name: "redis"
|
||||
static_configs:
|
||||
- targets: ["redis-exporter:9121"]
|
||||
- job_name: "postgres"
|
||||
static_configs:
|
||||
- targets: ["postgres-exporter:9187"]
|
||||
- job_name: "node"
|
||||
static_configs:
|
||||
- targets: ["node-exporter:9100"]
|
||||
- job_name: "prometheus"
|
||||
static_configs:
|
||||
- targets: ["prometheus:9090"]
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/bin/bash
|
||||
# Daily PostgreSQL backup to R2 (14-day retention)
|
||||
# Schedule: cron daily. Requires: pg_dump, aws CLI or rclone for R2.
|
||||
set -e
|
||||
|
||||
BACKUP_DIR="${BACKUP_DIR:-/tmp/linearvoid-backups}"
|
||||
RETENTION_DAYS="${RETENTION_DAYS:-14}"
|
||||
R2_BUCKET="${R2_BUCKET:-linearvoid-backups}"
|
||||
DB_URL="${DATABASE_URL:-postgres://linearvoid:linearvoid@db:5432/linearvoid?sslmode=disable}"
|
||||
|
||||
TIMESTAMP=$(date +%Y%m%d)
|
||||
BACKUP_FILE="$BACKUP_DIR/daily/db_${TIMESTAMP}.dump"
|
||||
|
||||
mkdir -p "$(dirname "$BACKUP_FILE")"
|
||||
|
||||
echo "[$(date -Iseconds)] Starting daily backup..."
|
||||
|
||||
# Extract connection details from DATABASE_URL
|
||||
DB_HOST=$(echo "$DB_URL" | sed -n 's/.*@\([^:]*\).*/\1/p')
|
||||
DB_PORT=$(echo "$DB_URL" | sed -n 's/.*:\([0-9]*\)\/.*/\1/p')
|
||||
DB_USER=$(echo "$DB_URL" | sed -n 's/.*:\/\/\([^:]*\).*/\1/p')
|
||||
DB_NAME=$(echo "$DB_URL" | sed -n 's/.*\/\([^?]*\).*/\1/p')
|
||||
DB_PASS=$(echo "$DB_URL" | sed -n 's/.*:\/\/[^:]*:\([^@]*\).*/\1/p')
|
||||
|
||||
export PGPASSWORD="${DB_PASS}"
|
||||
|
||||
# Dump
|
||||
pg_dump -h "${DB_HOST:-db}" -p "${DB_PORT:-5432}" -U "${DB_USER:-linearvoid}" \
|
||||
-Fc -f "$BACKUP_FILE" "${DB_NAME:-linearvoid}"
|
||||
|
||||
echo " Backup: $BACKUP_FILE ($(du -h "$BACKUP_FILE" | cut -f1))"
|
||||
|
||||
# Upload to R2
|
||||
if [ -n "$R2_ENDPOINT" ] && [ -n "$R2_ACCESS_KEY_ID" ]; then
|
||||
aws s3 cp "$BACKUP_FILE" "s3://${R2_BUCKET}/daily/db_${TIMESTAMP}.dump" \
|
||||
--endpoint-url "$R2_ENDPOINT" --no-verify-ssl 2>/dev/null && \
|
||||
echo " Uploaded to R2: daily/db_${TIMESTAMP}.dump" || \
|
||||
echo " WARNING: R2 upload failed"
|
||||
fi
|
||||
|
||||
# Cleanup old backups
|
||||
find "$BACKUP_DIR/daily" -name "db_*.dump" -mtime "+${RETENTION_DAYS}" -delete 2>/dev/null || true
|
||||
echo " Cleaned up backups older than ${RETENTION_DAYS} days"
|
||||
|
||||
echo "[$(date -Iseconds)] Daily backup complete."
|
||||
@@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
# Weekly full PostgreSQL backup to R2 (8-week retention)
|
||||
# Schedule: cron weekly (runs backup-db.sh with weekly prefix)
|
||||
set -e
|
||||
|
||||
BACKUP_DIR="${BACKUP_DIR:-/tmp/linearvoid-backups}"
|
||||
RETENTION_WEEKS="${RETENTION_WEEKS:-8}"
|
||||
R2_BUCKET="${R2_BUCKET:-linearvoid-backups}"
|
||||
DB_URL="${DATABASE_URL:-postgres://linearvoid:linearvoid@db:5432/linearvoid?sslmode=disable}"
|
||||
|
||||
TIMESTAMP=$(date +%Y%m%d)
|
||||
BACKUP_FILE="$BACKUP_DIR/weekly/db_${TIMESTAMP}.dump"
|
||||
|
||||
mkdir -p "$(dirname "$BACKUP_FILE")"
|
||||
|
||||
echo "[$(date -Iseconds)] Starting weekly full backup..."
|
||||
|
||||
DB_HOST=$(echo "$DB_URL" | sed -n 's/.*@\([^:]*\).*/\1/p')
|
||||
DB_PORT=$(echo "$DB_URL" | sed -n 's/.*:\([0-9]*\)\/.*/\1/p')
|
||||
DB_USER=$(echo "$DB_URL" | sed -n 's/.*:\/\/\([^:]*\).*/\1/p')
|
||||
DB_NAME=$(echo "$DB_URL" | sed -n 's/.*\/\([^?]*\).*/\1/p')
|
||||
DB_PASS=$(echo "$DB_URL" | sed -n 's/.*:\/\/[^:]*:\([^@]*\).*/\1/p')
|
||||
|
||||
export PGPASSWORD="${DB_PASS}"
|
||||
|
||||
pg_dump -h "${DB_HOST:-db}" -p "${DB_PORT:-5432}" -U "${DB_USER:-linearvoid}" \
|
||||
-Fc -f "$BACKUP_FILE" "${DB_NAME:-linearvoid}"
|
||||
|
||||
echo " Backup: $BACKUP_FILE ($(du -h "$BACKUP_FILE" | cut -f1))"
|
||||
|
||||
# Verify integrity
|
||||
pg_restore --list "$BACKUP_FILE" > /dev/null 2>&1 && \
|
||||
echo " Integrity check: PASSED" || \
|
||||
echo " WARNING: Integrity check FAILED"
|
||||
|
||||
# Upload
|
||||
if [ -n "$R2_ENDPOINT" ] && [ -n "$R2_ACCESS_KEY_ID" ]; then
|
||||
aws s3 cp "$BACKUP_FILE" "s3://${R2_BUCKET}/weekly/db_${TIMESTAMP}.dump" \
|
||||
--endpoint-url "$R2_ENDPOINT" --no-verify-ssl 2>/dev/null && \
|
||||
echo " Uploaded to R2: weekly/db_${TIMESTAMP}.dump" || \
|
||||
echo " WARNING: R2 upload failed"
|
||||
fi
|
||||
|
||||
# Cleanup
|
||||
find "$BACKUP_DIR/weekly" -name "db_*.dump" -mtime "+$((RETENTION_WEEKS * 7))" -delete 2>/dev/null || true
|
||||
echo " Cleaned up backups older than ${RETENTION_WEEKS} weeks"
|
||||
|
||||
echo "[$(date -Iseconds)] Weekly backup complete."
|
||||
@@ -0,0 +1,57 @@
|
||||
#!/bin/bash
|
||||
# Generate emergency slate video + pre-rendered HLS segments for failover.
|
||||
# Output: data/slate/ (slate MP4 + HLS segments + manifest)
|
||||
# Requires: ffmpeg
|
||||
set -e
|
||||
|
||||
SLATE_DIR="${1:-data/slate}"
|
||||
mkdir -p "$SLATE_DIR"
|
||||
|
||||
SLATE_MP4="$SLATE_DIR/slate.mp4"
|
||||
SLATE_HLS="$SLATE_DIR/hls"
|
||||
mkdir -p "$SLATE_HLS"
|
||||
|
||||
echo "=== Generating Emergency Slate ==="
|
||||
|
||||
# 1. Generate 10s slate video: color bars + "We'll Be Right Back" text
|
||||
echo "[1/3] Generating slate video (10s, H.264/AAC, 30fps, 2s keyframes)..."
|
||||
ffmpeg -y -f lavfi -i "color=c=#1a1a2e:s=1280x720:d=10:r=30" \
|
||||
-f lavfi -i "sine=frequency=440:duration=10" \
|
||||
-filter_complex "[0]drawtext=text='Linear Void':fontcolor=#587792:fontsize=60:x=(w-text_w)/2:y=(h-text_h)/2-40,drawtext=text='We'\''ll Be Right Back':fontcolor=#ede8e6:fontsize=36:x=(w-text_w)/2:y=(h-text_h)/2+30[bg];[1]volume=0.15[a]" \
|
||||
-map "[bg]" -map "[a]" \
|
||||
-c:v libx264 -preset medium -crf 21 \
|
||||
-r 30 -g 60 -keyint_min 60 -sc_threshold 0 \
|
||||
-pix_fmt yuv420p \
|
||||
-c:a aac -b:a 128k -ar 48000 -ac 2 \
|
||||
-movflags +faststart \
|
||||
-t 10 \
|
||||
"$SLATE_MP4"
|
||||
|
||||
echo " Slate MP4: $(du -h "$SLATE_MP4" | cut -f1)"
|
||||
|
||||
# 2. Verify slate meets normalization profile
|
||||
echo "[2/3] Verifying slate normalization..."
|
||||
ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate,pix_fmt,sample_rate \
|
||||
-of default=noprint_wrappers=1 "$SLATE_MP4" 2>&1 | head -10
|
||||
|
||||
# 3. Package as pre-rendered HLS (fMP4, 6s segments)
|
||||
echo "[3/3] Packaging as HLS..."
|
||||
ffmpeg -y -i "$SLATE_MP4" \
|
||||
-c copy \
|
||||
-f hls \
|
||||
-hls_time 6 \
|
||||
-hls_list_size 5 \
|
||||
-hls_segment_type fmp4 \
|
||||
-hls_fmp4_init_filename "init.mp4" \
|
||||
-hls_flags "independent_segments" \
|
||||
-hls_segment_filename "$SLATE_HLS/slate_segment_%03d.m4s" \
|
||||
"$SLATE_HLS/slate.m3u8"
|
||||
|
||||
echo ""
|
||||
echo "=== Slate Generated ==="
|
||||
echo " MP4: $SLATE_MP4"
|
||||
echo " Manifest: $SLATE_HLS/slate.m3u8"
|
||||
echo " Segments: $(find "$SLATE_HLS" -name '*.m4s' | wc -l) files"
|
||||
echo " Init: $SLATE_HLS/init.mp4"
|
||||
echo ""
|
||||
echo "To use: mount $SLATE_HLS as /app/slate in playout container"
|
||||
@@ -0,0 +1,57 @@
|
||||
#!/bin/bash
|
||||
# Normalize video to Linear Void required profile.
|
||||
# Output: H.264/AAC, 30fps constant, 2s keyframes, yuv420p, 48kHz audio.
|
||||
# Usage: ./normalize.sh <input> [output]
|
||||
set -e
|
||||
|
||||
INPUT="$1"
|
||||
if [ -z "$INPUT" ]; then
|
||||
echo "Usage: $0 <input-file> [output-file]"
|
||||
echo " Normalizes video to Linear Void profile: H.264/AAC, 30fps, 2s keyframes"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f "$INPUT" ]; then
|
||||
echo "Error: input file not found: $INPUT"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
OUTPUT="${2:-${INPUT%.*}-normalized.mp4}"
|
||||
CRF="${NORMALIZE_CRF:-21}"
|
||||
PRESET="${NORMALIZE_PRESET:-medium}"
|
||||
AUDIO_BITRATE="${NORMALIZE_AUDIO_BITRATE:-160k}"
|
||||
|
||||
echo "=== Linear Void Video Normalization ==="
|
||||
echo " Input: $INPUT ($(du -h "$INPUT" | cut -f1))"
|
||||
echo " Output: $OUTPUT"
|
||||
echo " CRF: $CRF"
|
||||
echo " Preset: $PRESET"
|
||||
echo " Audio rate: $AUDIO_BITRATE"
|
||||
echo ""
|
||||
|
||||
# Probe input
|
||||
echo "--- Input Properties ---"
|
||||
ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate,pix_fmt,sample_rate,channels \
|
||||
-of default=noprint_wrappers=1 "$INPUT" 2>&1
|
||||
echo ""
|
||||
|
||||
# Normalize
|
||||
echo "--- Normalizing ---"
|
||||
ffmpeg -y -i "$INPUT" \
|
||||
-c:v libx264 -preset "$PRESET" -crf "$CRF" \
|
||||
-r 30 -g 60 -keyint_min 60 -sc_threshold 0 \
|
||||
-pix_fmt yuv420p \
|
||||
-c:a aac -b:a "$AUDIO_BITRATE" -ar 48000 -ac 2 \
|
||||
-movflags +faststart \
|
||||
"$OUTPUT"
|
||||
|
||||
echo ""
|
||||
echo "--- Output Properties ---"
|
||||
ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate,pix_fmt,sample_rate,channels \
|
||||
-of default=noprint_wrappers=1 "$OUTPUT" 2>&1
|
||||
echo ""
|
||||
|
||||
echo "=== Normalization Complete ==="
|
||||
echo " Output: $OUTPUT ($(du -h "$OUTPUT" | cut -f1))"
|
||||
echo ""
|
||||
echo "Verify with: ffprobe -v error -show_entries stream=codec_name,r_frame_rate,pix_fmt -of default=noprint_wrappers=1 $OUTPUT"
|
||||
@@ -0,0 +1,85 @@
|
||||
#!/bin/bash
|
||||
# End-to-end playout test: single file → 3-file playlist → verify seamless transition
|
||||
# Requires: Docker, ffprobe, curl. Run from repo root.
|
||||
set -e
|
||||
|
||||
echo "=== Playout End-to-End Test ==="
|
||||
|
||||
# 1. Build playout image
|
||||
echo "[1/7] Building playout image..."
|
||||
docker compose build playout
|
||||
|
||||
# 2. Create test playlist with 3 demo files (reuse demodata if exists)
|
||||
TEST_PLAYLIST="data/playlist/playlist.txt"
|
||||
mkdir -p data/playlist data/hls
|
||||
|
||||
echo "[2/7] Creating 3-file test playlist..."
|
||||
cat > "$TEST_PLAYLIST" << 'EOF'
|
||||
file '/app/media/demo1.mp4'
|
||||
file '/app/media/demo2.mp4'
|
||||
file '/app/media/demo1.mp4'
|
||||
EOF
|
||||
echo "Playlist created with 3 entries:"
|
||||
cat "$TEST_PLAYLIST"
|
||||
|
||||
# 3. Validate playlist parses correctly
|
||||
echo "[3/7] Validating playlist parse..."
|
||||
LINE_COUNT=$(grep -c "^file " "$TEST_PLAYLIST" || true)
|
||||
if [ "$LINE_COUNT" -ne 3 ]; then
|
||||
echo "FAIL: Expected 3 entries, got $LINE_COUNT"
|
||||
exit 1
|
||||
fi
|
||||
echo "OK: $LINE_COUNT entries parsed"
|
||||
|
||||
# 4. Start playout container
|
||||
echo "[4/7] Starting playout container..."
|
||||
docker compose up -d playout
|
||||
sleep 8
|
||||
|
||||
# 5. Verify health endpoint
|
||||
echo "[5/7] Checking playout health..."
|
||||
HEALTH=$(curl -sf http://localhost:8081/health || echo "FAIL")
|
||||
if echo "$HEALTH" | grep -q "healthy"; then
|
||||
echo "OK: Health endpoint: $HEALTH"
|
||||
else
|
||||
echo "FAIL: Health check returned: $HEALTH"
|
||||
docker compose logs playout --tail 30
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 6. Verify HLS output
|
||||
echo "[6/7] Verifying HLS output..."
|
||||
sleep 10 # Let FFmpeg produce some segments
|
||||
|
||||
if [ -f "data/hls/playlist.m3u8" ]; then
|
||||
echo "OK: playlist.m3u8 exists"
|
||||
MANIFEST_LINES=$(wc -l < data/hls/playlist.m3u8)
|
||||
echo " Manifest lines: $MANIFEST_LINES"
|
||||
else
|
||||
echo "FAIL: No playlist.m3u8 generated"
|
||||
docker compose logs playout --tail 30
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SEGMENT_COUNT=$(find data/hls -name "segment_*.m4s" | wc -l)
|
||||
if [ "$SEGMENT_COUNT" -ge 1 ]; then
|
||||
echo "OK: $SEGMENT_COUNT segment(s) generated"
|
||||
else
|
||||
echo "FAIL: No segments generated"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 7. Check for seamless transition markers (no FFmpeg errors in logs)
|
||||
echo "[7/7] Checking for seamless transitions..."
|
||||
ERRORS=$(docker compose logs playout 2>&1 | grep -ci "error" || true)
|
||||
if [ "$ERRORS" -eq 0 ]; then
|
||||
echo "OK: No FFmpeg errors in logs"
|
||||
else
|
||||
echo "WARN: $ERRORS error(s) in playout logs (may be R2 connection — check manually)"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=== Playout E2E Test PASSED ==="
|
||||
echo "Verified: playlist parse → HLS generation → health endpoint → segment output"
|
||||
echo ""
|
||||
echo "Cleanup: docker compose down playout"
|
||||
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
# Weekly backup integrity verification.
|
||||
# Downloads latest backup from R2, restores into temp container, runs sanity checks.
|
||||
# Schedule: cron weekly (after weekly backup).
|
||||
set -e
|
||||
|
||||
R2_BUCKET="${R2_BUCKET:-linearvoid-backups}"
|
||||
TEMP_DIR=$(mktemp -d)
|
||||
trap "rm -rf $TEMP_DIR" EXIT
|
||||
|
||||
echo "[$(date -Iseconds)] Starting backup integrity verification..."
|
||||
|
||||
# Download latest weekly backup from R2
|
||||
if [ -z "$R2_ENDPOINT" ] || [ -z "$R2_ACCESS_KEY_ID" ]; then
|
||||
echo " SKIP: R2 credentials not configured (R2_ENDPOINT, R2_ACCESS_KEY_ID)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
LATEST=$(aws s3 ls "s3://${R2_BUCKET}/weekly/" --endpoint-url "$R2_ENDPOINT" --no-verify-ssl 2>/dev/null | sort | tail -1 | awk '{print $4}')
|
||||
if [ -z "$LATEST" ]; then
|
||||
echo " SKIP: No weekly backups found in R2"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo " Downloading: $LATEST"
|
||||
aws s3 cp "s3://${R2_BUCKET}/weekly/${LATEST}" "$TEMP_DIR/backup.dump" \
|
||||
--endpoint-url "$R2_ENDPOINT" --no-verify-ssl 2>/dev/null
|
||||
|
||||
echo " File size: $(du -h "$TEMP_DIR/backup.dump" | cut -f1)"
|
||||
|
||||
# Restore into temp PostgreSQL container
|
||||
echo " Starting temp PostgreSQL..."
|
||||
docker run -d --name lv-backup-test-$$ \
|
||||
-e POSTGRES_PASSWORD=test \
|
||||
-e POSTGRES_DB=linearvoid \
|
||||
-p 15432:5432 \
|
||||
postgres:15-alpine 2>/dev/null
|
||||
|
||||
sleep 5
|
||||
|
||||
# Wait for PG to be ready
|
||||
for i in $(seq 1 10); do
|
||||
if docker exec lv-backup-test-$$ pg_isready -U postgres 2>/dev/null; then
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
# Restore
|
||||
echo " Restoring backup..."
|
||||
docker cp "$TEMP_DIR/backup.dump" "lv-backup-test-$$:/tmp/backup.dump"
|
||||
docker exec lv-backup-test-$$ pg_restore -U postgres -d linearvoid /tmp/backup.dump 2>/dev/null && \
|
||||
echo " Restore: OK" || { echo " Restore: FAILED"; docker rm -f lv-backup-test-$$ 2>/dev/null; exit 1; }
|
||||
|
||||
# Sanity checks
|
||||
MSG_COUNT=$(docker exec lv-backup-test-$$ psql -U postgres -d linearvoid -tAc "SELECT COUNT(*) FROM messages;" 2>/dev/null || echo "0")
|
||||
MOD_COUNT=$(docker exec lv-backup-test-$$ psql -U postgres -d linearvoid -tAc "SELECT COUNT(*) FROM moderation_logs;" 2>/dev/null || echo "0")
|
||||
echo " Messages: $MSG_COUNT, Moderation: $MOD_COUNT"
|
||||
|
||||
# Cleanup
|
||||
docker rm -f lv-backup-test-$$ 2>/dev/null
|
||||
|
||||
echo "[$(date -Iseconds)] Backup verification complete. OK."
|
||||
Reference in new issue
Block a user