37 lines
980 B
TypeScript
37 lines
980 B
TypeScript
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);
|
|
|
|
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);
|
|
});
|