feat: update environment configuration and seed admin user in database

This commit is contained in:
Thibault Pouch
2026-02-28 14:35:54 +01:00
parent f700d7fd86
commit 073dc38fd7
5 changed files with 55 additions and 424 deletions

View File

@@ -1,8 +1,36 @@
import 'dotenv/config';
import bcrypt from 'bcryptjs';
import prisma from './lib/prisma.js';
import app from './app.js';
const PORT = parseInt(process.env.PORT ?? '3000', 10);
app.listen(PORT, () => {
console.log(`[server] Running on http://localhost:${PORT}`);
});
async function ensureAdmin(): Promise<void> {
const { ADMIN_USERNAME, ADMIN_EMAIL, ADMIN_PASSWORD } = process.env;
if (!ADMIN_USERNAME || !ADMIN_EMAIL || !ADMIN_PASSWORD) return;
await prisma.user.upsert({
where: { email: ADMIN_EMAIL },
update: { username: ADMIN_USERNAME, role: 'dev', isAdmin: true },
create: {
username: ADMIN_USERNAME,
email: ADMIN_EMAIL,
password: await bcrypt.hash(ADMIN_PASSWORD, 10),
role: 'dev',
isAdmin: true,
},
});
console.log(`[server] Admin user ready: ${ADMIN_USERNAME}`);
}
ensureAdmin()
.then(() => {
app.listen(PORT, () => {
console.log(`[server] Running on http://localhost:${PORT}`);
});
})
.catch((err) => {
console.error('[server] Startup error:', err);
process.exit(1);
});