-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
160 lines (134 loc) · 5.04 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require("@marko/compiler/register");
var debug = require('debug')('kcapp:app');
var compression = require('compression');
var express = require('express');
var isProduction = process.env.NODE_ENV === 'production';
// Configure lasso to control how JS/CSS/etc. is delivered to the browser
var lasso = require('lasso');
lasso.configure({
plugins: [
'lasso-marko', // Allow Marko templates to be compiled and transported to the browser
'lasso-less'
],
outputDir: `${__dirname}/static`, // Place all generated JS/CSS/etc. files into the "static" dir
bundlingEnabled: isProduction, // Only enable bundling in production
minify: isProduction, // Only minify JS and CSS code in production
fingerprintsEnabled: isProduction, // Only add fingerprints to URLs in production
cacheProfile: process.env.DISK_CACHE && isProduction ? 'production' : null // Cache to disk
});
var path = require('path');
var cookieParser = require('cookie-parser');
var lessMiddleware = require('less-middleware');
var app = express();
var marko = require("@marko/express").default;
app.use(marko());
app.use(compression());
app.use(require('lasso/middleware').serveStatic());
// Make sure we get correct user IP when running behind a reverse proxy
app.enable('trust proxy');
var socketIo = require("socket.io");
var io = socketIo();
app.io = io;
// Set application variables
app.locals.kcapp = {};
app.locals.kcapp.api = process.env.KCAPP_API || 'http://localhost:8001';
app.locals.kcapp.api_path = process.env.KCAPP_API_PATH || ':8001';
app.locals.kcapp.local_admin = process.env.KCAPP_LOCAL_ADMIN ? process.env.KCAPP_LOCAL_ADMIN === "true" : true;
// Create all routes
const socketHandler = require('./routes/lib/socketio_handler')(io, app);
const matches = require('./routes/matches')(socketHandler);
const index = require('./routes/index');
const legs = require('./routes/legs')(app, socketHandler);
const offices = require('./routes/offices');
const owes = require('./routes/owes');
const players = require('./routes/players');
const practice = require('./routes/practice');
const presets = require('./routes/presets');
const statistics = require('./routes/statistics');
const tournaments = require('./routes/tournaments');
const elo = require('./routes/elo');
const venues = require('./routes/venues')(app, socketHandler);
const badges = require('./routes/badges');
socketHandler.setupActiveNamespace();
app.locals.moment = require('moment');
app.locals._ = require('underscore');
// Write access log to a daily rotated file in /log
const pad = num => (num > 9 ? "" : "0") + num;
const generator = (time, index) => {
if (!time) {
return "access.log";
}
const date = `${time.getFullYear()}-${pad(time.getMonth() + 1)}-${pad(time.getDate())}-${index}`;
return `access.${date}.log.gz`;
};
var logger = require('morgan');
var rfs = require('rotating-file-stream')
const accessLogStream = rfs.createStream(generator, {
interval: "1d",
compress: true,
path: path.join(__dirname, 'log')
});
app.use(logger('combined', { stream: accessLogStream }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(lessMiddleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/matches', matches);
app.use('/legs', legs);
app.use('/offices/', offices);
app.use('/owes', owes);
app.use('/players', players);
app.use('/practice', practice);
app.use('/presets', presets);
app.use('/statistics', statistics);
app.use('/tournaments', tournaments);
app.use('/elo', elo);
app.use('/venues', venues);
app.use('/badges', badges);
// Not Found (404) Handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error Handler
const template = require('marko');
const notFoundTemplate = template.load(require.resolve('./src/pages/404/404.marko'));
const errorTemplate = template.load(require.resolve('./src/pages/404/404.marko'));
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
if (err.status == 404 || (err.response && err.response.status == 404)) {
// respond with html page
if (req.accepts('html')) {
res.marko(notFoundTemplate, {
url: req.url
});
return;
}
// respond with json
if (req.accepts('json')) {
res.send({
error: 'Not found'
});
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
} else {
if (err.response !== undefined) {
debug(`Axios error message: ${err.response.data.trim()}`);
} else if (err.stack !== undefined) {
debug(err.stack);
}
res.marko(errorTemplate, {
error: err
});
}
});
module.exports = app;