-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.js
97 lines (84 loc) · 3.61 KB
/
WebServer.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
const cors = require("cors");
const {Utils, logLevel, logModule} = require("./Utils/Utils");
const config = require("./config.json");
const { ImageClassificationHandler } = require("./Handler/ImageClassificationHandler");
const {json} = require("express");
class WebServer {
start() {
Utils.log(logLevel.INFO, logModule.WEBSERVER, "Starting Webserver");
const express = require('express');
const cors = require('cors');
const app = express()
const port = config.webServer.port
const imageClassificationHandler = new ImageClassificationHandler();
//
// Init Webserver for own requests
//
app.use(express.json())
app.use(cors(), (req, res, next) => {
res.setHeader("X-Powered-By", "GalaxyBot")
res.setHeader("Access-Control-Allow-Origin", "127.0.0.1");
res.setHeader("Content-Type", "application/json");
let reqUri = req.originalUrl.toString().length > 61 ? req.originalUrl.toString().substring(0, 61) + "..." : req.originalUrl
if(config.debug) Utils.log(logLevel.DEBUG, logModule.WEBSERVER, "RequestURI " + reqUri)
next();
});
//
//Routes
//
// Body = {key: "key", metaData: {userID: "userID", guildID: "guildID"}, deleteByClassification: true}
// Key must be an object key from the S3 Bucket
//
// ID's in metaData are Discord snowflakes for de Logging Channel
app.post("/api/v1/classifyImage", checkHost, (req, res) => {
// Check if all required parameters are given
if (req.body.key && req.body.metaData.userID && req.body.metaData.guildID && req.body.deleteByClassification != null){
// Call the ImageClassificationHandler
imageClassificationHandler.classifyImage(req.body.key, req.body.metaData, req.body.deleteByClassification).then((results) => {
res.status(200)
res.write(JSON.stringify(results))
res.end()
}).catch((error) => {
Utils.log(logLevel.ERROR, logModule.WEBSERVER, error?.message)
res.status(500)
res.write(JSON.stringify({
"error": "Internal Server Error",
"message": error?.message
}))
res.end()
})
} else {
res.status(400)
res.write('{"error": "Missing Parameters"}')
res.end()
}
});
//Start the WebServer
app.listen(port, () => {
Utils.log(logLevel.SUCCESS, logModule.WEBSERVER, "WebServer started on port " + port)
});
//
// Middleware
//
/**
* Check the Host Header
* @param req - Request Object from Express
* @param res - Response Object from Express
* @param next - Next Function from Express
*
* We use this function to check if the request is coming from the right host for security reasons you can remove this if you want
*/
function checkHost(req, res, next) {
// Localhost == Testing with Insomnia
// 172.18.0.1 == Bot
if(req.headers.host === `localhost:${config.webServer.port}` || req.headers.host === `172.18.0.1:${config.webServer.port}`) {
next();
} else {
res.status(403)
res.write('{"error": "Invalid Host"}')
res.end()
}
}
}
}
module.exports = WebServer