Updates
This commit is contained in:
64
backend/dist/server.js
vendored
Normal file
64
backend/dist/server.js
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import recipesRouter from './routes/recipes.js';
|
||||
import authRouter from './routes/auth.js';
|
||||
import analyticsRouter from './routes/analytics.js';
|
||||
import { getUserByUsername, createUser } from './db.js';
|
||||
import { trackEvent } from './analytics-db.js';
|
||||
import { authMiddleware, adminMiddleware } from './middleware.js';
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'tiktok-rezepte-secret-key-change-in-production';
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use((req, res, next) => {
|
||||
if (req.method === 'OPTIONS' || !req.path.startsWith('/api/'))
|
||||
return next();
|
||||
if (req.path.startsWith('/api/analytics'))
|
||||
return next();
|
||||
try {
|
||||
let userId = null;
|
||||
const authHeader = req.headers.authorization;
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
try {
|
||||
const decoded = jwt.verify(authHeader.split(' ')[1], JWT_SECRET);
|
||||
const user = getUserByUsername(decoded.username);
|
||||
if (user)
|
||||
userId = user.id;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
trackEvent('page_view', userId, { path: req.path, method: req.method });
|
||||
}
|
||||
catch { }
|
||||
next();
|
||||
});
|
||||
app.use('/api/auth', authRouter);
|
||||
app.use('/api/recipes', recipesRouter);
|
||||
app.use('/api/analytics', authMiddleware, adminMiddleware, analyticsRouter);
|
||||
app.use((err, req, res, next) => {
|
||||
console.error('Server Fehler:', err.message);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({ error: 'Interner Serverfehler' });
|
||||
}
|
||||
});
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error('Uncaught Exception:', err.message);
|
||||
});
|
||||
process.on('unhandledRejection', (err) => {
|
||||
console.error('Unhandled Rejection:', err?.message || err);
|
||||
});
|
||||
const adminUser = process.env.ADMIN_USER || 'admin';
|
||||
const adminPass = process.env.ADMIN_PASS || 'admin123';
|
||||
const existing = getUserByUsername(adminUser);
|
||||
if (!existing) {
|
||||
const hashed = bcrypt.hashSync(adminPass, 10);
|
||||
createUser(adminUser, hashed, true);
|
||||
console.log(`Admin erstellt: ${adminUser} / ${adminPass}`);
|
||||
console.log('WICHTIG: Passwort nach dem ersten Login ändern!');
|
||||
}
|
||||
app.listen(Number(PORT), () => {
|
||||
console.log(`Backend läuft auf http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user