-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (71 loc) · 2.11 KB
/
Copy pathapp.js
File metadata and controls
83 lines (71 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require('express')
const cors = require('cors')
const winston = require('winston')
const { errorHandler } = require('./utils/errorHandler')
const { serve, setup } = require('./config/swagger')
// Import routes
const userRoutes = require('./routes/users')
const campaignRoutes = require('./routes/campaigns')
const rewardRoutes = require('./routes/rewards')
// Configure Winston logger
winston.configure({
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
defaultMeta: { service: 'jerota-backend' },
transports: [
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
new winston.transports.File({ filename: 'logs/combined.log' }),
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
}),
],
})
// Create Express app
const app = express()
// Middleware
app.use(
cors({
origin: process.env.FRONTEND_URL || '*',
credentials: true,
})
)
app.use(express.json({ limit: '10mb' }))
app.use(express.urlencoded({ extended: true }))
// Request logging middleware
app.use((req, res, next) => {
winston.info(`${req.method} ${req.path}`, {
ip: req.ip,
userAgent: req.get('User-Agent'),
walletAddress: req.headers['x-wallet-address'],
})
next()
})
// Swagger documentation
app.use('/api-docs', serve, setup)
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
success: true,
message: 'Jerota Backend API is running',
timestamp: new Date().toISOString(),
version: '1.0.0',
})
})
// API routes
app.use('/api/users', userRoutes)
app.use('/api/campaigns', campaignRoutes)
app.use('/api/rewards', rewardRoutes)
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({
success: false,
error: 'Route not found',
})
})
// Global error handler
app.use(errorHandler)
module.exports = app