-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (101 loc) · 3.53 KB
/
Copy pathserver.js
File metadata and controls
101 lines (101 loc) · 3.53 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require('dotenv').config();
const express = require('express');
const compression = require('compression');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const path = require('path');
const fs = require('fs');
const http = require('http');
const http2 = require('http2');
const ejsLayouts = require('express-ejs-layouts');
const { applySecurityMiddleware } = require('./src/middleware/security');
const { i18nMiddleware } = require('./src/middleware/i18n');
const indexRoutes = require('./src/routes/index');
const apiRoutes = require('./src/routes/api');
const { tools } = indexRoutes;
const app = express();
if (process.env.NODE_ENV === 'production') {
app.set('trust proxy', 1);
}
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.set('layout', 'layout');
app.set('layout extractScripts', true);
app.use(ejsLayouts);
app.use(compression({
filter: (req, res) => {
if (req.path === '/sitemap.xml') return false;
return compression.filter(req, res);
}
}));
app.use(express.json({ limit: '1mb' }));
app.use(express.urlencoded({ extended: true, limit: '1mb' }));
app.use(cookieParser());
if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev'));
}
applySecurityMiddleware(app);
app.use(i18nMiddleware);
app.use((req, res, next) => {
res.locals.tools = tools;
next();
});
app.use('/fonts/v2', express.static(path.join(__dirname, 'public', 'fonts'), {
maxAge: '1y',
immutable: true,
etag: false,
setHeaders: (res) => {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
}
}));
const immutableFiles = new Set(['logo_light.webp', 'logo_dark.webp', 'logo_light.png', 'logo_dark.png', 'favicon.ico']);
app.use(express.static(path.join(__dirname, 'public'), {
etag: true,
setHeaders: (res, filePath) => {
const name = path.basename(filePath);
const ext = path.extname(filePath);
if (immutableFiles.has(name)) {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
} else if (ext === '.css' || ext === '.js') {
const age = process.env.NODE_ENV === 'production' ? 2592000 : 0;
res.setHeader('Cache-Control', age ? `public, max-age=${age}` : 'no-cache');
} else {
const age = process.env.NODE_ENV === 'production' ? 604800 : 0;
res.setHeader('Cache-Control', age ? `public, max-age=${age}` : 'no-cache');
}
}
}));
app.use('/', indexRoutes);
app.use('/api', apiRoutes);
app.use((req, res) => {
res.status(404).render('404', {
title: '404 - Page Not Found | AlwizTool',
description: 'The page you are looking for does not exist.',
canonical: process.env.SITE_URL || 'https://ubegui.my.id',
tools,
});
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Internal server error' });
});
if (require.main === module) {
const PORT = process.env.PORT || 3000;
const certPath = process.env.SSL_CERT || path.join(__dirname, 'certs', 'cert.pem');
const keyPath = process.env.SSL_KEY || path.join(__dirname, 'certs', 'key.pem');
if (fs.existsSync(certPath) && fs.existsSync(keyPath)) {
const opts = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
allowHTTP1: true,
};
http2.createSecureServer(opts, app).listen(PORT, () =>
console.log(`AlwizTool running on https://localhost:${PORT} (HTTP/2)`)
);
} else {
http.createServer(app).listen(PORT, () =>
console.log(`AlwizTool running on http://localhost:${PORT} (HTTP/1.1 — add certs/ for HTTP/2)`)
);
}
}
module.exports = app;