diff --git a/README.md b/README.md index 288995d..9b8b2de 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,50 @@ The web UI at `http://localhost:3000` provides: --- +## Modération (admin) + +L'endpoint `/api/admin/shares` permet de lister et supprimer les affiches publiées. Il est protégé par un token secret défini dans `.env.local`. + +### Configuration + +Ajoutez ces variables dans `.env.local` et dans les variables d'environnement Vercel : + +```env +ADMIN_SECRET="votre-token-secret" # token d'authentification (requis) +APP_URL="https://votre-domaine.com" # optionnel — déduit automatiquement du host de la requête +``` + +Pour générer un token sécurisé : + +```bash +just admin-gen-token +``` + +### Commandes + +```bash +just admin-list # Lister toutes les affiches avec leurs liens +just admin-delete 0cfe97acc8ecb728 # Supprimer une affiche par son ID (16 hex) +``` + +### API directe + +```bash +# Lister +curl -H "Authorization: Bearer " https://votre-domaine.com/api/admin/shares + +# Supprimer +curl -X DELETE \ + -H "Authorization: Bearer " \ + -H "Content-Type: application/json" \ + -d '{"id": "0cfe97acc8ecb728"}' \ + https://votre-domaine.com/api/admin/shares +``` + +Réponses d'erreur : `401` si token invalide, `503` si `ADMIN_SECRET` absent, `400` si ID invalide. + +--- + ## Development commands ```bash @@ -240,4 +284,9 @@ just restart # Stop + start just rebuild # Force rebuild image just logs # Follow container logs just status # Container status + +# Admin +just admin-gen-token # Générer un token ADMIN_SECRET +just admin-list # Lister toutes les affiches publiées +just admin-delete 0cfe97acc8ecb728 # Supprimer une affiche par son ID ``` diff --git a/justfile b/justfile index 025c543..2805744 100644 --- a/justfile +++ b/justfile @@ -1,6 +1,7 @@ #!/usr/bin/env just -f set export +set dotenv-filename := ".env.local" # This help @help: @@ -106,6 +107,29 @@ logs SERVICE="": @ps: docker compose ps +############################################################################### +# Admin +############################################################################### + +# Générer un token sécurisé pour ADMIN_SECRET +[group('admin')] +@admin-gen-token: + openssl rand -hex 32 + +# Lister toutes les affiches publiées (avec leurs liens de visualisation) +[group('admin')] +admin-list: + curl -s -H "Authorization: Bearer ${ADMIN_SECRET}" ${APP_URL}/api/admin/shares | jq . + +# Supprimer une affiche par son ID (ex: just admin-delete 0cfe97acc8ecb728) +[group('admin')] +admin-delete ID: + curl -s -X DELETE \ + -H "Authorization: Bearer ${ADMIN_SECRET}" \ + -H "Content-Type: application/json" \ + -d '{"id": "{{ ID }}"}' \ + ${APP_URL}/api/admin/shares | jq . + ############################################################################### # CI / Utils ############################################################################### diff --git a/lib/share-store.ts b/lib/share-store.ts index 5e4cdde..7152c0b 100644 --- a/lib/share-store.ts +++ b/lib/share-store.ts @@ -36,3 +36,39 @@ export async function loadShare(id: string): Promise | nu return await redis.get>(`share:${id}`); } } + +export async function listShares(): Promise { + if (!useUpstash()) { + const { readdirSync, existsSync } = await import('fs'); + const { join } = await import('path'); + const dir = join(process.cwd(), 'shares'); + if (!existsSync(dir)) return []; + return readdirSync(dir) + .filter((f) => f.endsWith('.json')) + .map((f) => f.slice(0, -5)); + } else { + const { Redis } = await import('@upstash/redis'); + const redis = new Redis({ url: process.env.KV_REST_API_URL!, token: process.env.KV_REST_API_TOKEN! }); + const ids: string[] = []; + let cursor = 0; + do { + const [next, keys] = await redis.scan(cursor, { match: 'share:*', count: 100 }); + cursor = Number(next); + for (const key of keys) ids.push((key as string).replace(/^share:/, '')); + } while (cursor !== 0); + return ids; + } +} + +export async function deleteShare(id: string): Promise { + if (!useUpstash()) { + const { unlinkSync, existsSync } = await import('fs'); + const { join } = await import('path'); + const file = join(process.cwd(), 'shares', `${id}.json`); + if (existsSync(file)) unlinkSync(file); + } else { + const { Redis } = await import('@upstash/redis'); + const redis = new Redis({ url: process.env.KV_REST_API_URL!, token: process.env.KV_REST_API_TOKEN! }); + await redis.del(`share:${id}`); + } +}