Update environment configuration for PostgreSQL: Add new environment variables for NextAuth and PostgreSQL settings in .env file, update docker-compose.yml to utilize these variables, and enhance README documentation for environment setup. Ensure DATABASE_URL is constructed dynamically if not defined.

This commit is contained in:
Julien Froidefond
2025-12-17 13:15:40 +01:00
parent 2c7a346cde
commit cb02b494f4
7 changed files with 121 additions and 9 deletions

30
.env
View File

@@ -5,8 +5,28 @@
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="file:./data/dev.db"
AUTH_SECRET="your-secret-key-change-this-in-production"
AUTH_URL="http://localhost:3000"
PRISMA_DATA_PATH="/Users/julien.froidefond/Sites/DAIS/public/got-gaming/data"
UPLOADS_PATH="/Users/julien.froidefond/Sites/DAIS/public/got-gaming/public/uploads"
# DATABASE_URL="file:./data/dev.db"
# AUTH_SECRET="your-secret-key-change-this-in-production"
# AUTH_URL="http://localhost:3000"
# PRISMA_DATA_PATH="/Users/julien.froidefond/Sites/DAIS/public/got-gaming/data"
# UPLOADS_PATH="/Users/julien.froidefond/Sites/DAIS/public/got-gaming/public/uploads"
# NextAuth Configuration
NEXTAUTH_SECRET=change-this-secret-in-production
NEXTAUTH_URL=http://localhost:3000
# PostgreSQL Configuration
POSTGRES_USER=gotgaming
POSTGRES_PASSWORD=change-this-in-production
POSTGRES_DB=gotgaming
POSTGRES_HOST=localhost
POSTGRES_PORT=5433
# Database URL (construite automatiquement si non définie)
# Si vous définissez cette variable, elle sera utilisée telle quelle
# Sinon, elle sera construite à partir des variables POSTGRES_* ci-dessus
# DATABASE_URL=postgresql://gotgaming:change-this-in-production@got-postgres:5432/gotgaming?schema=public
# Docker Volumes (optionnel)
POSTGRES_DATA_PATH=./data/postgres
UPLOADS_PATH=./public/uploads

19
.env.example Normal file
View File

@@ -0,0 +1,19 @@
# NextAuth Configuration
NEXTAUTH_SECRET=change-this-secret-in-production
NEXTAUTH_URL=http://localhost:3000
# PostgreSQL Configuration
POSTGRES_USER=gotgaming
POSTGRES_PASSWORD=change-this-in-production
POSTGRES_DB=gotgaming
POSTGRES_HOST=got-postgres
POSTGRES_PORT=5432
# Database URL (construite automatiquement si non définie)
# Si vous définissez cette variable, elle sera utilisée telle quelle
# Sinon, elle sera construite à partir des variables POSTGRES_* ci-dessus
# DATABASE_URL=postgresql://gotgaming:change-this-in-production@got-postgres:5432/gotgaming?schema=public
# Docker Volumes (optionnel)
POSTGRES_DATA_PATH=./data/postgres
UPLOADS_PATH=./public/uploads

1
.gitignore vendored
View File

@@ -25,6 +25,7 @@ yarn-debug.log*
yarn-error.log*
# local env files
.env
.env*.local
# vercel

View File

@@ -24,17 +24,36 @@ docker-compose logs -f
## Variables d'environnement
Créez un fichier `.env` à la racine du projet avec les variables suivantes :
Créez un fichier `.env` à la racine du projet à partir du template `.env.example` :
```bash
cp .env.example .env
```
Puis modifiez les valeurs dans `.env` selon votre configuration :
```env
# NextAuth Configuration
NEXTAUTH_SECRET=your-secret-key-here
NEXTAUTH_URL=http://localhost:3000
# PostgreSQL Configuration
POSTGRES_USER=gotgaming
POSTGRES_PASSWORD=change-this-in-production
POSTGRES_DB=gotgaming
DATABASE_URL=postgresql://gotgaming:change-this-in-production@got-postgres:5432/gotgaming?schema=public
# Database URL (optionnel - construite automatiquement si non définie)
# DATABASE_URL=postgresql://gotgaming:change-this-in-production@got-postgres:5432/gotgaming?schema=public
# Docker Volumes (optionnel)
POSTGRES_DATA_PATH=./data/postgres
UPLOADS_PATH=./public/uploads
```
**Important** :
- Le fichier `.env` est ignoré par Git (ne pas commiter vos secrets)
- Si vous changez `POSTGRES_PASSWORD` après la première initialisation, vous devrez soit réinitialiser la base, soit changer le mot de passe manuellement dans PostgreSQL
## Volumes persistants
### Base de données PostgreSQL

View File

@@ -35,6 +35,9 @@ services:
- "3040:3000"
environment:
- NODE_ENV=production
- POSTGRES_USER=${POSTGRES_USER:-gotgaming}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-change-this-in-production}
- POSTGRES_DB=${POSTGRES_DB:-gotgaming}
- DATABASE_URL=postgresql://${POSTGRES_USER:-gotgaming}:${POSTGRES_PASSWORD:-change-this-in-production}@got-postgres:5432/${POSTGRES_DB:-gotgaming}?schema=public
- NEXTAUTH_URL=${NEXTAUTH_URL:-http://localhost:3000}
- NEXTAUTH_SECRET=${NEXTAUTH_SECRET:-change-this-secret-in-production}

View File

@@ -2,8 +2,31 @@ import { PrismaClient } from "@/prisma/generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import { Pool } from "pg";
// Construire DATABASE_URL si elle n'est pas définie, en utilisant les variables individuelles
let databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
const user = process.env.POSTGRES_USER || "gotgaming";
const password = process.env.POSTGRES_PASSWORD || "change-this-in-production";
const host = process.env.POSTGRES_HOST || "got-postgres";
const port = process.env.POSTGRES_PORT || "5432";
const db = process.env.POSTGRES_DB || "gotgaming";
// Encoder le mot de passe pour l'URL
const encodedPassword = encodeURIComponent(password);
databaseUrl = `postgresql://${user}:${encodedPassword}@${host}:${port}/${db}?schema=public`;
}
if (typeof databaseUrl !== "string") {
throw new Error("DATABASE_URL must be a string");
}
// Logger l'URL de connexion (masquer le mot de passe pour la sécurité)
const logUrl = databaseUrl.replace(/:\/\/[^:]+:[^@]+@/, "://***:***@");
console.log(`[Prisma] Connecting to PostgreSQL: ${logUrl}`);
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
connectionString: databaseUrl,
});
const adapter = new PrismaPg(pool);

View File

@@ -7,8 +7,35 @@ import { PrismaPg } from "@prisma/adapter-pg";
import { Pool } from "pg";
import bcrypt from "bcryptjs";
// Construire DATABASE_URL si elle n'est pas définie (même logique que lib/prisma.ts)
let databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
const user = process.env.POSTGRES_USER || "gotgaming";
const password = process.env.POSTGRES_PASSWORD || "change-this-in-production";
// Si on est dans Docker, utiliser le nom du service, sinon localhost avec le port externe
const host =
process.env.POSTGRES_HOST ||
(process.env.DOCKER_ENV ? "got-postgres" : "localhost");
const port =
process.env.POSTGRES_PORT || (process.env.DOCKER_ENV ? "5432" : "5433");
const db = process.env.POSTGRES_DB || "gotgaming";
// Encoder le mot de passe pour l'URL
const encodedPassword = encodeURIComponent(password);
databaseUrl = `postgresql://${user}:${encodedPassword}@${host}:${port}/${db}?schema=public`;
}
if (typeof databaseUrl !== "string") {
throw new Error("DATABASE_URL must be a string");
}
// Logger l'URL de connexion (masquer le mot de passe pour la sécurité)
const logUrl = databaseUrl.replace(/:\/\/[^:]+:[^@]+@/, "://***:***@");
console.log(`[Seed] Connecting to PostgreSQL: ${logUrl}`);
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
connectionString: databaseUrl,
});
const adapter = new PrismaPg(pool);