-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
102 lines (88 loc) · 2.65 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
92
93
94
95
96
97
98
99
100
101
102
const express = require('express');
const fs = require('fs');
const path = require('path');
const session = require('express-session');
const morgan = require('morgan');
const MongoStore = require('connect-mongo');
const Sentry = require('@sentry/node');
const Tracing = require('@sentry/tracing');
const connectDB = require('./config/database');
require('dotenv').config();
const { authCheck } = require('./middlewares/AuthMiddleware');
const AuthRoute = require('./routes/AuthRouter');
const {
clientErrorHandler,
ErrorHandler,
} = require('./middlewares/errors/HandleErrors');
const LogStream = fs.createWriteStream(path.join(__dirname, 'syncwallet.log'), {
flags: 'a',
});
// Create an instance of express
const app = express();
// Connect to database
connectDB();
const port = process.env.PORT || 4000;
Sentry.init({
dsn: 'https://4f608a2114b440e9957713559edc96e5@o439434.ingest.sentry.io/5406225',
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
],
tracesSampleRate: 0.7,
});
// Register middlewares
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
app.use(express.static(path.join(__dirname, 'public')));
app.use(morgan('common', { stream: LogStream }));
app.use(express.json());
app.use(
session({
secret: process.env.APP_KEY,
saveUninitialized: true,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
resave: false,
store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI }),
}),
);
if (app.get('env') === 'production') {
app.set('trust proxy', 1);
// session.cookie.secure = true;
}
app.use((req, res, next) => {
res.locals.authenticated = !!req.session.user;
res.locals.user = req.session.user ? req.session.user : null;
next();
});
app.use(
express.urlencoded({
extended: true,
}),
);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
if (process.env.NODE_ENV === 'development') {
app.enable('verbose errors');
} else {
app.disable('verbose errors');
}
// application endpoints
app.use('/auth', AuthRoute);
app.get('/', (request, response) => {
response.render('index', {
message: 'Sync Wallet!',
});
});
app.get('/dashboard', authCheck, (request, response) => {
response.render('dashboard');
});
app.get('/error', () => {
throw new Error('Testing Error');
});
app.use(Sentry.Handlers.errorHandler());
// Error handling middlewares
app.use(clientErrorHandler);
app.use(ErrorHandler);
module.exports = { app, port };