Organize backend

This commit is contained in:
bingus_violet 2024-07-06 21:56:39 -05:00
parent 92150b9b94
commit 8fff995bd0
5 changed files with 122 additions and 97 deletions

1
.gitignore vendored
View file

@ -131,4 +131,3 @@ dist
.pnp.* .pnp.*
data data
static/index.html

23
expressHandler.js Normal file
View file

@ -0,0 +1,23 @@
const express = require("express"),
paths = require("./fileManager.js"),
pageUpdater = require("./pageUpdater.js"),
path = require("path"),
fs = require("fs")
const PORT = process.env.PORT || 8080
var app = express()
app.use(express.static(paths.static))
app.get("/", (req, res) => {
res.send(pageUpdater.root())
})
app.get('/post/:post*', (req, res) => {
res.send(pageUpdater.blogPost(req))
})
app.listen(PORT, () => {
console.log("Violet's Limbo is now listening on port: " + PORT)
})

30
fileManager.js Normal file
View file

@ -0,0 +1,30 @@
const chokidar = require("chokidar"),
path = require("path"),
fs = require("fs"),
EventEmitter = require("events").EventEmitter
var dataPath = path.join(__dirname, 'data')
var postsPath = path.join(dataPath, 'posts')
var staticPath = path.join(__dirname, 'static')
var reqPaths = [dataPath, postsPath]
for (var i = 0; i < reqPaths.length; i++) {
var p = reqPaths[i]
if (!fs.existsSync(p)) {
fs.mkdirSync(p)
}
}
module.exports = {
data: dataPath,
posts: postsPath,
static: staticPath,
emitter: new EventEmitter()
}
var watcher = chokidar.watch(dataPath)
watcher
.on('change', module.exports.emitter.emit)
.on('add', module.exports.emitter.emit)

View file

@ -1,101 +1,8 @@
const express = require("express"), const express = require("express"),
chokidar = require("chokidar"),
fs = require("fs"), fs = require("fs"),
path = require("path"), path = require("path")
showdown = require("showdown"),
mkthtml = new showdown.Converter()
mkthtml.setFlavor("github") require("./expressHandler.js")
const PORT = process.env.PORT || 8080
var dataPath = path.join(__dirname, 'data')
var postsPath = path.join(dataPath, 'posts')
var staticPath = path.join(__dirname, 'static')
var watcher = chokidar.watch(dataPath)
var app = express()
app.use(express.static(staticPath))
var reqPaths = [dataPath, postsPath]
for (var i = 0; i < reqPaths.length; i++) {
var p = reqPaths[i]
if (!fs.existsSync(p)) {
fs.mkdirSync(p)
}
}
app.listen(PORT, () => {
console.log("Violet's Limbo is now listening on: " + PORT)
})
function pageUpdate() {
var data = JSON.parse(fs.readFileSync(path.join(dataPath, 'data.json')))
var postsArray = data.posts
var html = ""
for (var i = 0; i < postsArray.length; i++) {
var addedHTML = ""
var post = postsArray[i]
addedHTML +=
`<div class="post">
<a style="text-decoration: none;" href="./post/${post.path}">
<h2>${post.name}</h2>
<p style="color: white; font-size: 1rem;">${post.desc}</p>
<p style="color: darkgray; font-size: 1rem;">Path: <code>/post/${post.path}</code></p>
</a>
</div>`
html += addedHTML
}
var html = fs.readFileSync(path.join(__dirname, 'resources/mainPage.html')).toString().replace('{POSTS}', html)
fs.writeFileSync(path.join(staticPath, 'index.html'), html)
}
pageUpdate()
watcher
.on('change', pageUpdate)
.on('add', pageUpdate)
app.get('/post/:post*', (req, res) => {
var postName = req.params.post
var data = JSON.parse(fs.readFileSync(path.join(dataPath, 'data.json')))
var postsArray = data.posts
for (let index = 0; index < postsArray.length; index++) {
const post = postsArray[index];
if (post.path == postName) {
postContent = mkthtml.makeHtml(fs.readFileSync(path.join(postsPath, post.path)).toString())
var html = fs.readFileSync(path.join(__dirname, 'resources/postPage.html')).toString()
html = html.replace('{POST}', postContent)
html = html.replace('{POST_TITLE}', post.name)
res.send(html)
return
}
}
var html = fs.readFileSync(path.join(__dirname, 'resources/postPage.html')).toString()
html = html.replace("{POST_TITLE}", "Not found!")
html = html.replace("{POST}", "<p>Couldn't find this post... Maybe try clearing your cache? Violet's Limbo is currently going through alot of backend changes, so expect things to break!</p>")
res.send(html)
return
})
process.on('uncaughtException', (err, origin) => { process.on('uncaughtException', (err, origin) => {
fs.writeSync( fs.writeSync(

66
pageUpdater.js Normal file
View file

@ -0,0 +1,66 @@
const fs = require("fs"),
path = require("path"),
showdown = require("showdown"),
paths = require("./fileManager.js"),
mkthtml = new showdown.Converter()
mkthtml.setFlavor("github")
module.exports = {
root: () => {
var data = JSON.parse(fs.readFileSync(path.join(paths.data, 'data.json')))
var postsArray = data.posts
var html = ""
for (var i = 0; i < postsArray.length; i++) {
var addedHTML = ""
var post = postsArray[i]
addedHTML +=
`<div class="post">
<a style="text-decoration: none;" href="./post/${post.path}">
<h2>${post.name}</h2>
<p style="color: white; font-size: 1rem;">${post.desc}</p>
<p style="color: darkgray; font-size: 1rem;">Path: <code>/post/${post.path}</code></p>
</a>
</div>`
html += addedHTML
}
html = fs.readFileSync(path.join(__dirname, 'resources/mainPage.html')).toString().replace('{POSTS}', html)
return html
},
blogPost: (req) => {
var postName = req.params.post
var data = JSON.parse(fs.readFileSync(path.join(paths.data, 'data.json')))
var postsArray = data.posts
for (let index = 0; index < postsArray.length; index++) {
const post = postsArray[index];
if (post.path == postName) {
postContent = mkthtml.makeHtml(fs.readFileSync(path.join(paths.posts, post.path)).toString())
var html = fs.readFileSync(path.join(__dirname, 'resources/postPage.html')).toString()
html = html.replace('{POST}', postContent)
html = html.replace('{POST_TITLE}', post.name)
// res.send(html)
return html
}
}
var html = fs.readFileSync(path.join(__dirname, 'resources/postPage.html')).toString()
html = html.replace("{POST_TITLE}", "Not found!")
html = html.replace("{POST}", "<p>Couldn't find this post... Maybe try clearing your cache? Violet's Limbo is currently going through alot of backend changes, so expect things to break!</p>")
// res.send(html)
return html
}
}