-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
91 lines (70 loc) · 2.53 KB
/
app.js
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
84
85
86
87
88
89
90
91
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const passport = require('passport');
require('./config/passport');
require('./config/dotenv');
const cookieParser = require('cookie-parser');
const { impersonateHelper } = require('./middlewares/impersonateHelper');
const app = express();
const port = process.env.PORT || 3000;
app.use(cookieParser());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI }),
cookie: { maxAge: 365 * 24 * 60 * 60 * 1000 }
}));
app.use(impersonateHelper);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res, next) => {
if (req.originalUrl === '/api/billing/stripe-webhook') {
next();
} else {
bodyParser.json()(req, res, next);
}
});
const database = require('./config/database');
database.connect(() => {
const server = app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
if (process.env.NODE_ENV === 'test') {
server.close(() => {
console.log('Server closed for tests');
});
}
});
app.use(passport.initialize());
app.use(passport.session());
const { attachUserCredits } = require('./middlewares/userCreditsMiddleware');
app.use(attachUserCredits);
app.use(express.static('public'));
const viewRoutes = require('./routes/viewRoutes');
app.use('/', viewRoutes);
app.set('view engine', 'ejs');
app.get('/ping', (req, res) => {
res.send('pong');
});
const authRoutes= require('./routes/authRoutes');
app.use('/api/auth', authRoutes);
const { router: billingRouter } = require('./routes/billingRoutes');
app.use('/api/billing', billingRouter);
const creditRoutes= require('./routes/creditRoutes');
app.use('/api/credits', creditRoutes);
const adminRoutes= require('./routes/adminRoutes');
app.use('/api/admin', adminRoutes);
const { setupScheduledTasks } = require('./services/scheduledTasks');
setupScheduledTasks();
app.post('/api/billing/stripe-webhook', express.raw({type: 'application/json'}), (req, res, next) => {
const billingRoutes = require('./routes/billingRoutes');
billingRoutes.stripeWebhook(req, res, next);
});
const adminPricingRoutes = require('./routes/adminPricingRoutes');
app.use('/api/admin', adminPricingRoutes);
const adminImpersonationRoutes = require('./routes/adminImpersonationRoutes');
app.use('/api/admin', adminImpersonationRoutes);
module.exports = app;