From 86224d00e3246eba415819a95a13ee8f339751ee Mon Sep 17 00:00:00 2001 From: Enstrayed <48845980+Enstrayed@users.noreply.github.com> Date: Sat, 17 Aug 2024 11:04:58 -0700 Subject: [PATCH] I forgor --- index.js | 6 +----- liberals/auth.js | 6 +----- routes/frontpage.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 routes/frontpage.js diff --git a/index.js b/index.js index 831794d..fbb7efb 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ function criticalFileLoader(file) { const globalConfig = JSON.parse(criticalFileLoader('config.json')) const globalVersion = criticalFileLoader('GITVERSION').split(" ")[0] -module.exports = { app, globalConfig, fs } // Export express app and fs objects and globalconfig +module.exports = { app, globalConfig, fs, globalVersion } // Export express app and fs objects and globalconfig app.use(express.json()) // Allows receiving JSON bodies // see important note: https://expressjs.com/en/api.html#express.json @@ -39,9 +39,5 @@ fs.readdir(globalConfig.startup.routesDir, (err, files) => { } }) -app.get("/", (rreq,rres) => { - rres.send(`Enstrayed API | Version: ${globalVersion} | Documentation: etyd.cc/apidocs`) -}) - console.log(`Enstrayed API | Version: ${globalVersion} | Port: ${globalConfig.startup.apiPort}`) app.listen(globalConfig.startup.apiPort) \ No newline at end of file diff --git a/liberals/auth.js b/liberals/auth.js index 115523c..07ff077 100644 --- a/liberals/auth.js +++ b/liberals/auth.js @@ -1,11 +1,7 @@ const { globalConfig } = require("../index.js") async function checkToken(token,scope) { - return await fetch(`http://${globalConfig.couchdb.host}/auth/sessions`, { - headers: { - "Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}` - } - }).then(fetchRes => { + return await fetch(`${globalConfig.couchdbHost}/auth/sessions`).then(fetchRes => { // CouchDB should only ever return 200/304 for success so this should work // https://docs.couchdb.org/en/stable/api/document/common.html#get--db-docid diff --git a/routes/frontpage.js b/routes/frontpage.js new file mode 100644 index 0000000..7c2297f --- /dev/null +++ b/routes/frontpage.js @@ -0,0 +1,44 @@ +const { app, globalConfig, fs, globalVersion } = require("../index.js") // Get globals from index + +var timeSinceLastQuery = Date.now()-10000 +var cachedResult = "" + +app.get("/static/*", (rreq,rres) => { + rres.sendFile(globalConfig.frontpage.frontpageDir+"/static/"+rreq.url.replace("/static/","")) +}) + +app.get("/posts/*", (rreq,rres) => { + rres.sendFile(globalConfig.frontpage.frontpageDir+"/posts/"+rreq.url.replace("/posts/","")) +}) + +app.get("/", (rreq, rres) => { + if (Date.now() < timeSinceLastQuery+10000) { + rres.send(cachedResult) + } else { + let indexFile = fs.readFileSync(globalConfig.frontpage.frontpageDir+"/index.html","utf-8") + cachedResult = indexFile.replace("",parseFiles()).replace("",`API Version ${globalVersion}`) + rres.send(cachedResult) + } +}) + +function parseFiles() { + let files = fs.readdirSync(globalConfig.frontpage.frontpageDir+"/posts/") + let result = "" + + for (x in files) { + if (files[x].endsWith(".html") === false) { break } // If file/dir is not .html then ignore + + let date = files[x].split("-")[0] + if (date < 10000000 || date > 99999999) { break } // If date does not fit ISO8601 format then ignore + + date = date.replace(/.{2}/g,"$&-").replace("-","").slice(0,-1) // Insert a dash every 2 characters, remove the first dash, remove the last character + + let name = files[x].slice(9).replace(/-/g," ").replace(".html","") // Strip Date, replace seperator with space & remove file extension + + result = `${date} ${name}`+result + } + + return result +} + +module.exports = {app} \ No newline at end of file