Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ADMIN_SECRET>" https://votre-domaine.com/api/admin/shares

# Supprimer
curl -X DELETE \
-H "Authorization: Bearer <ADMIN_SECRET>" \
-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
Expand All @@ -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
```
24 changes: 24 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env just -f

set export
set dotenv-filename := ".env.local"

# This help
@help:
Expand Down Expand Up @@ -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
###############################################################################
Expand Down
36 changes: 36 additions & 0 deletions lib/share-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,39 @@ export async function loadShare(id: string): Promise<Record<string, string> | nu
return await redis.get<Record<string, string>>(`share:${id}`);
}
}

export async function listShares(): Promise<string[]> {
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<void> {
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}`);
}
}
Loading