add Analyse
This commit is contained in:
@@ -1,18 +1,42 @@
|
||||
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();
|
||||
|
||||
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 });
|
||||
next();
|
||||
});
|
||||
|
||||
app.use('/api/auth', authRouter);
|
||||
app.use('/api/recipes', recipesRouter);
|
||||
app.use('/api/analytics', authMiddleware, adminMiddleware, analyticsRouter);
|
||||
|
||||
const adminUser = process.env.ADMIN_USER || 'admin';
|
||||
const adminPass = process.env.ADMIN_PASS || 'admin123';
|
||||
|
||||
Reference in New Issue
Block a user