-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
143 lines (133 loc) · 4.11 KB
/
app.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import express, { Application, NextFunction, Request, Response } from "express";
require('dotenv').config();
import https from 'https';
import fs from 'fs';
import path from 'path';
import methodOverride from 'method-override';
import { db } from './db';
import routes from './routes';
import session, {SessionOptions, MemoryStore} from 'express-session';
import MongoStore from 'connect-mongo';
import passport from 'passport';
import ExpressMongoSanitize from 'express-mongo-sanitize';
import flash from 'connect-flash';
import helmet from 'helmet';
import { Server } from "http";
import { ConnectMongoOptions } from "connect-mongo/build/main/lib/MongoStore";
const app:Application = express();
app.use(helmet());
const scriptSrcUrls = [
'https://cdnjs.cloudflare.com',
'https://cdn.jsdelivr.net',
'https://unpkg.com',
'https://kit.fontawesome.com'
];
const styleSrcUrls = [
'https://unpkg.com',
'https://cdn.jsdelivr.net',
'https://cdnjs.cloudflare.com'
];
const connectSrcUrls = [
'https://api.maptiler.com',
'https://ka-f.fontawesome.com/'
];
const fontSrcUrls = ['https://ka-f.fontawesome.com/'];
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: [],
manifestSrc: ["'self'"],
connectSrc: ["'self'", ...connectSrcUrls],
scriptSrc: ["'unsafe-inline'", "'self'", ...scriptSrcUrls],
styleSrc: ["'self'", "'unsafe-inline'", ...styleSrcUrls],
workerSrc: ["'self'", 'blob:'],
childSrc: ['blob:'],
objectSrc: [],
imgSrc: [
"'self'",
'blob:',
'data:',
'https://res.cloudinary.com/dwz8ueclf/'
],
scriptSrcAttr: ["'unsafe-inline'", "'self'"],
fontSrc: ["'self'", ...fontSrcUrls]
}
})
);
app.disable('x-powered-by');
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use(ExpressMongoSanitize());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
if (process.env.NODE_ENV === 'development') {
const morgan = require('morgan');
app.use(morgan('dev'));
}
app.use(methodOverride('_method'));
const sessionStoreOpts: ConnectMongoOptions = {
mongoUrl: process.env.DB_URL || 'mongodb://127.0.0.1:27017/review-it',
touchAfter: 24 * 3600
};
const expiryDate = new Date(Date.now() + 60 * 60 * 1000 * 24);
const sessionOpts: SessionOptions = {
name: process.env.SESSION_NAME,
// types error with MongoStore
store: MongoStore.create(sessionStoreOpts) || new MemoryStore(),
cookie: {
httpOnly: true,
expires: expiryDate
},
secret: process.env.SESSION_SECRET || 'development',
resave: false,
saveUninitialized: true,
};
if (process.env.NODE_ENV === 'production') {
console.log('Secured!');
app.set('trust proxy', 1); // trust first proxy
sessionOpts.cookie = {
httpOnly: true,
expires: expiryDate,
secure: true,
sameSite: 'none',
signed: true,
};
}
app.use(session(sessionOpts));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
db.init();
app.use(routes);
//* Error Routes
app.use((err: Error, req: Request , res: Response, next: NextFunction) => {
console.log(err);
res.render('error', { err, currentUser: req.user });
next();
});
let server:https.Server | Server;
if (process.env.HTTPS !== 'unset') {
server = https
.createServer(
{
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
},
app
)
.listen(process.env.PORT || 443);
} else {
server = app.listen(process.env.PORT || 3000, () => {
console.log('Serving on', process.env.PORT || 3000);
});
}
process.on('SIGTERM', () => {
console.info('SIGTERM signal received.');
console.log('Closing http server.');
db.close();
server.close((err?: Error) => {
console.log('Http server closed.');
process.exit(err ? 1 : 0);
});
});