-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (49 loc) · 1.45 KB
/
index.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
"use strict";
// Dependencies
const compression = require("compression")
const serveIndex = require("serve-index")
const express = require("express")
const path = require("path")
const fs = require("fs")
// Variables
const web = express()
const port = process.env.PORT || 8080
const database = require("./database.json")
// Functions
function getLuaFiles(dirPath){
const files = []
function readDirR(dirPath){
const entries = fs.readdirSync(dirPath, { withFileTypes: true })
entries.map((entry)=>{
const fullPath = path.join(dirPath, entry.name)
if(entry.isDirectory()){
readDirR(fullPath)
}else if(path.extname(fullPath) === ".lua"){
files.push(fullPath)
}
})
}
readDirR(dirPath)
return files
}
/// Configurations
// Express
web.use(compression({ chunkSize: 65536 }))
web.use(express.static(path.join(__dirname, "public")))
web.use("/scripts", express.static(path.join(__dirname, "scripts")), serveIndex(path.join(__dirname, "scripts"), { stylesheet: path.join(__dirname, "custom.css"), icons: true }))
// Main
web.use("", (req, res, next)=>{
if(req.path.match(".html")) return res.redirect("/")
next()
})
web.use("/status", async(req, res)=>{
return res.json({
status: "success",
data: {
scriptsCount: getLuaFiles(path.join(__dirname, "scripts")).length,
scripts: database
}
})
})
web.use("*", (req, res)=>res.redirect("/"))
web.listen(port, ()=>console.log(`Server is running. Port: ${port}`))