feat: update environment configuration and seed admin user in database
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user