-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
270 lines (224 loc) · 8.67 KB
/
server.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @version 1.2.0 // 23/07/2023
* @author Sylicium
* @description Module de serveur qui gère un site web, une api, et supporte un bot discord
*
*/
const axios = require("axios")
const fs = require("fs")
let config = require("./config")
const express = require('express');
const { queryParser } = require('express-query-parser')
const bodyParser = require('body-parser');
const Discord = require("discord.js");
const Logger = require("./localModules/logger")();
const app = express();
app.use(express.urlencoded())
app.use(express.json())
app.use(queryParser({
parseNull: true,
parseUndefined: true,
parseBoolean: true,
parseNumber: true,
}))
app.use(bodyParser.json());
const serv = require('http').createServer(app);
const io = require('socket.io')(serv);
let ztParser = require("./localModules/ztParser")
const Modules_ = {
"Discord": Discord,
"app": app,
"config": config,
"axios": axios,
"ztParser": ztParser
}
let servEndpoints = {
api: {
fs: "/api", // chemin depuis le root du projet et sans le slash de fin
relative: "/api", // chemin relatif par rapport à ce fichier
},
site: {
fs: "/docs", // chemin depuis le root du projet et sans le slash de fin
relative: "/docs", // chemin relatif par rapport à ce fichier
}
}
let Client;
/*
param types:
string - pour du texte
object - dictionnaire { }
array - liste []
number - Nombre entier ou flotant
boolean - true/false
*/
let APIEvents = [
]
Logger.info(`[API] Loading APIEvents...`)
fs.readdirSync(`.${servEndpoints.api.fs}/`).forEach(directoryName => {
let dirPath = `.${servEndpoints.api.fs}/${directoryName}`
try {
if( fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory() ) {
Logger.info(`[API] Loading api endpoints for method ${directoryName.toUpperCase()}`)
fs.readdirSync(`.${servEndpoints.api.fs}/${directoryName}/`).forEach(file => {
let the_require = require(`.${servEndpoints.api.relative}/${directoryName}/${file}`)
the_require.method = directoryName.toUpperCase()
let fileName = file.split(".")
fileName.pop()
fileName = fileName.join(".")
the_require.endpoint = fileName
APIEvents.push(the_require)
Logger.info(`[API] ✔ Loaded API endpoint (${the_require.method}) /${the_require.endpoint}`)
})
} else {
Logger.warn(`[API] ! ${directoryName} is a file, not a directory`)
}
} catch(e) {
Logger.error(`[API][ERROR] ❌`,e)
}
})
Logger.info(`[API] ✅ Loaded ${APIEvents.length} APIEvents`,APIEvents)
module.exports.run = (instance_client) => {
Client = instance_client
app.all("/assets/*", (req, res) => {
return res.sendFile(`${__dirname}${servEndpoints.site.relative}${req.path}`)
})
app.all("*", async (req, res) => { // tout à la fin sinon le "*" catch à la place des autres app.get()
if(req.path.startsWith("/assets/")) { return res.sendFile(`${__dirname}${servEndpoints.site.relative}${req.path}`) }
if(req.path.startsWith("/api/")) {
handleAPI(req, res)
return;
}
try {
Logger.debug(`[Web] ${req.method.toUpperCase()} -> ${req.url}`)
// console.log(req.query)
if(req.path == "/favicon.ico") return res.sendFile(`${__dirname}${servEndpoints.site.relative}/favicon.ico`)
if(req.path.startsWith("/api/")) return;
if(req.path.startsWith("/assets/")) return;
if(req.path.endsWith("/") && fs.existsSync(`.${servEndpoints.site.fs}${req.path}/index.html`)) {
return res.sendFile(`${__dirname}${servEndpoints.site.relative}${req.path}/index.html`)
} else if(fs.existsSync(`.${servEndpoints.site.fs}${req.path}.html`)) {
return res.sendFile(`${__dirname}${servEndpoints.site.relative}${req.path}.html`)
} else {
return res.sendFile(`${__dirname}${servEndpoints.site.relative}/404.html`)
}
} catch(err) {
res.status(500)
return res.send(JSON.stringify({
message: `An error occured server-side. ${err}`,
stack: err.stack.split("\n"),
}))
}
})
serv.listen(config.website.port, () => {
Logger.info(`[server.js] Serveur démarré sur le port ${config.website.port}`)
})
}
// Do not remove the under message
// API Disabled for legal reasons. Make it public at your own risks and on your own machine, I decline all responsibility in the event of misuse of my code.
// Do not remove the above message
function handleAPI(req, res) {
return; // API Disabled for legal reasons. Make it public at your own risks and on your own machine, I decline all responsibility in the event of misuse of my code.
// Logger.debug("got api",req.url)
let endpoint = req.path.substr(5, req.path.length)
let apiEvent_list = APIEvents.filter((item) => {
return (endpoint == item.endpoint)
})
if(apiEvent_list.length == 0) return res.send({
status: 404,
message: `Endpoint does not exists`
})
apiEvent_list2 = apiEvent_list.filter((item) => {
return (item.method == req.method)
})
let allMethodsAllowed = apiEvent_list.map((item, index) => {
return item.method
})
if(apiEvent_list2.length == 0) return res.send({
status: 405,
message: `Method not allowed`,
methods: allMethodsAllowed
})
let apiEvent = apiEvent_list2[0]
/*
// Parse JSON in query parameters
for(let paramName in req.query) {
let paramValue = req.query[paramName]
try {
req.query[paramName] = JSON.parse(paramValue)
} catch(e) {
Logger.error(e)
return res.send({
status: 500,
message: `Internal server error while parsing to JSON query parameter '${paramName}'.`,
error: `${e}`,
stack: e.stack.split("\n")
})
}
}*/
for(let i in apiEvent.parameters) {
let param = apiEvent.parameters[i]
if(!req.query[param.name] && param.required) {
return res.send({
status: 400,
message: `Bad request. ERR#01 Missing parameter: '${param.name}'. ${param.msg || ""}`,
parameters: apiEvent.parameters
})
} else if(req.query[param.name]) {
try {
if(param.type == "array") {
if(!Array.isArray(req.query[param.name])) {
return res.send({
status: 400,
message: `Bad request. ERR#02 Invalid parameter type: '${param.name}'. ${param.msg || ""}`,
parameters: apiEvent.parameters
})
}
} else if(typeof req.query[param.name] != param.type) {
return res.send({
status: 400,
message: `Bad request. ERR#03 Invalid parameter type: '${param.name}'. ${param.msg || ""}`,
parameters: apiEvent.parameters
})
}
} catch(e) {
Logger.error(e)
return res.send({
status: 500,
message: `Internal server error while parsing query parameter '${param.name}' (type:${param.type} | required:${param.required}).`,
error: `${e}`,
stack: e.stack.split("\n")
})
}
}
}
try {
/*
TypeError: apiEvent.func(...).then is not a function ----->> /api/method/endpoint.func must be async function
*/
apiEvent.func(Client, Modules_, req, res).then(JSONResponse => {
return res.send(JSONResponse)
}).catch(err => {
Logger.error(err)
return res.send({
status: 500,
message: `Internal server error while executing request.`,
error: `${err}`,
stack: err.stack.split("\n")
})
})
} catch(err) {
Logger.error(err)
return res.send({
status: 500,
message: `Internal server error while executing request.`,
error: `${err}`,
stack: err.stack.split("\n")
})
}
}
io.on('connection', socket => {
Logger.info(`[socket][+] New connection: ${socket.id}`)
io.on('disconnect', socket => {
Logger.info(`[socket][-] Lost connection: ${socket.id}`)
})
})