-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
215 lines (179 loc) · 6.25 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* This file is part of SOMHunter.
*
* Copyright (C) 2020 František Mejzlík <frankmejzlik@gmail.com>
* Mirek Kratochvil <exa.exa@gmail.com>
* Patrik Veselý <prtrikvesely@gmail.com>
*
* SOMHunter is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 2 of the License, or (at your option)
* any later version.
*
* SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* SOMHunter. If not, see <https://www.gnu.org/licenses/>.
*/
"use strict";
const createError = require("http-errors");
const express = require("express");
const session = require("express-session");
const path = require("path");
const cookieParser = require("cookie-parser");
const morgan = require("morgan");
const fs = require("fs");
const { createLogger, format, transports } = require("winston");
const { combine, timestamp, printf } = format;
/*
* Configuration
*/
// Set root project directory
global.rootDir = __dirname;
// Load config
const config = require("./config/config");
config.initConfig();
/*
* Request routing
*/
// Page routers
const somhunterRouter = require("./routes/somhunter");
const routerNotFound = require("./routes/404");
// API endpoints
const endpoints = require("./routes/endpoints");
/*
* Logging
*/
// Log format closure
const myFormat = printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
});
// Make sure that this dir exists
fs.mkdir(global.cfg.logsDir, { recursive: true }, (err) => {
if (err) throw err;
});
// Create main logger
global.logger = createLogger({
level: "debug",
format: combine(timestamp(), myFormat),
defaultMeta: { service: "user-service" },
transports: [
// Write all logs with level `error` and below to `error.log`
// Write all logs with level `info` and below to `combined.log`
new transports.Stream({
stream: fs.createWriteStream(global.cfg.logsDir + "error.log", { flags: "a+" }),
level: "error",
}),
new transports.Stream({ stream: fs.createWriteStream(global.cfg.logsDir + "combined.log", { flags: "a+" }) }),
],
});
// Log only into log files while in production
if (process.env.NODE_ENV !== "production") {
global.logger.add(
new transports.Console({
format: combine(timestamp(), myFormat),
})
);
}
/*
* Launch the app
*/
// Instantiate Express app
const app = express();
// Setup where EJS templates are stored
app.set("views", path.join(__dirname, "views"));
// Setup EJS engine for templates
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// Use Morgan for request logging
app.use(
morgan("common", {
stream: fs.createWriteStream(__dirname + "/" + global.cfg.logsDir + "/requests.log", { flags: "a+" }),
})
);
app.use(morgan("dev"));
/*
* Turn on sessions
*/
app.use(session({ secret: "matfyz", resave: false, saveUninitialized: true }));
/*
* HTTP authentication
*/
app.use((req, res, next) => {
// Get auth credentials
const creds = global.cfg.creds
// Parse login and password from headers
const b64auth = (req.headers.authorization || "").split(" ")[1] || "";
const [login, password] = new Buffer(b64auth, "base64").toString().split(":");
// Verify login and password are set and correct
if (req.session.user || (login && password && creds[login] && password === creds[login])) {
if (!req.session.user)
req.session.user = login;
// Access granted
return next();
}
// Access denied
res.set("WWW-Authenticate", "Basic realm='401'");
res.status(401).send("Authentication required.");
});
/*
* Initial communication with the admin user
*/
global.logger.log("debug", "process.env = '" + process.env["NODE_ENV"] + "'");
if (process.env["NODE_ENV"] === "development") {
global.logger.log(
"warn",
`
=======================================================================
App is running in 'development' environment!
Debug info will be printed.
This is not recommended while exposed to public users.
To change it, you can set environment variable \`NODE_ENV\` to 'production.
=======================================================================`
);
}
/*
* Load native evaluation library (ImageRanker)
*/
const dataConfigFilepath = path.join(global.rootDir, global.cfg.dataConfigFilepath);
global.logger.log("info", "Initializing SOMHunter core...");
global.logger.log("debug", "dataConfigFilepath = " + dataConfigFilepath);
// Create global instance of the ImageRanker
const core = require(path.join(__dirname, "build/Release/somhunter_core.node"));
global.core = new core.SomHunterNapi(dataConfigFilepath);
global.logger.log("info", "SOMHunter is ready...");
/*
* Push all routers into express middleware stack
*/
app.use("/", somhunterRouter);
// SOMHunter endpoints
app.get("/get_frame_detail_data", endpoints.getFrameDetailData);
app.get("/get_autocomplete_results", endpoints.getAutocompleteResults);
app.get("/get_top_screen", endpoints.getTopScreen);
app.get("/get_som_screen", endpoints.getSomScreen);
app.get("/get_target", endpoints.getTarget);
app.get("/get_previous_screen", endpoints.getPreviousScreen);
app.get("/get_level_info", endpoints.getLevelInfo);
app.post("/submit_frame", endpoints.submitFrame);
app.post("/report_issue", endpoints.reportIssue);
app.post("/reset_search_session", endpoints.resetSearchSession);
app.post("/rescore", endpoints.rescore);
app.post("/like_frame", endpoints.likeFrame);
app.post("/unlike_frame", endpoints.unlikeFrame);
// 404 fallback
app.use("/404", routerNotFound);
// Error handler
app.use(function (err, req, res, next) {
// Show error message if in development environment
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;