-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
157 lines (128 loc) · 3.86 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
// server.js
// where your node app starts
// we've started you off with Express (https://expressjs.com/)
// but feel free to use whatever libraries or frameworks you'd like through `package.json`.
const startTime = Date.now();
require("dotenv").config({
path: __dirname + "/.env" + (process.env.NODE_ENV ? "." + process.env.NODE_ENV : "")
});
process.env.TEST_MODE = (process.env.NODE_ENV === "test") + "";
console.info("Starting server in " + process.env.NODE_ENV + " mode");
const express = require("express");
const swaggerUi = require('swagger-ui-express');
const cors = require("cors");
const compression = require("compression");
const cookieParser = require("cookie-parser");
const morgan = require("morgan");
const helmet = require("helmet")
const path = require("path");
const fs = require('fs');
const app = express();
const cache = require('./config/cache');
const nsfwModel = require("./models/v3_NSFWModel");
nsfwModel.init().then(() => {
//Cannot log after tests are done
if (process.env.NODE_ENV !== "test") console.log("Model loaded in", Date.now() - startTime, "ms");
});
//what is this
app.head("/", (request, response) => {
response.status(200);
});
app.use(helmet());
app.use(compression());
app.disable("x-powered-by");
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(express.raw());
/* Start Logging */
const log_path = process.env.LOG_PATH || path.join(__dirname, "logs");
// if log path not exist, log_path folder will be created
if (!fs.existsSync(log_path)) {
fs.mkdirSync(log_path, {recursive: true});
}
// Log all error requests status
app.use(
morgan("combined", {
skip: (req, res) => {
return res.statusCode < 400;
},
stream: fs.createWriteStream(path.join(log_path, "error.log"), {
flags: "a",
}),
})
);
// Log all success request status
app.use(
morgan("combined", {
skip: (req, res) => {
return res.statusCode > 400;
},
stream: fs.createWriteStream(path.join(log_path, "access.log"), {
flags: "a",
}),
})
);
/* End Logging */
/* Dynamic CORS */
app.use(
cors({
origin: "*",
})
);
/* End Dynamic CORS */
/* Start Cookie Settings */
app.use(cookieParser());
/* End Cookie Settings */
const apiVersion = process.env.API_VERSION || "v3";
const testURL = "/api/" + apiVersion + "/health";
const indexHtml = fs.readFileSync(process.cwd() + "/views/index.html").toString().replace("API_VERSIONING", apiVersion).replace("TEST_URL", testURL);
app.get("/", (request, response) => {
response.send(indexHtml);
});
const docsFolder = process.cwd() + "/docs/";
let docs = require(docsFolder + 'docs.json');
app.use("/docs", swaggerUi.serve, swaggerUi.setup(docs, {
explorer: true,
}));
/* Begin Authorization */
app.use(function (req, res, next) {
if (!process.env.NODE_NSFW_KEY) {
next();
} else if (!req.headers.authorization && !!process.env.NODE_NSFW_KEY) {
//console.log("No auth");
return res.status(401).json({
error: "No Authorization Provided"
});
} else if (req.headers.authorization !== process.env.NODE_NSFW_KEY) {
//console.log("invalid auth");
return res.status(401).json({
error: "Invalid Authorization"
});
} else {
next();
}
});
/* End Authorization */
/* Start of Routing Modules */
const v3_router = require("./routes/v3_route");
v3_router(app);
/* End of Routing Modules */
app.get("*", function (req, res) {
res.status(404);
// respond with json
if (req.accepts("json")) {
res.json({
status: "ERROR",
error_code: "404",
message: "Not found",
});
return;
}
// default to plain-text. send()
res.type("txt").send("Not found");
});
module.exports = {
app,
apiVersion,
testURL,
}