${post.name}
+${post.desc}
+Path: /post/${post.path}
diff --git a/.gitignore b/.gitignore index 1d5d8e5..48e99b1 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,5 @@ dist .yarn/install-state.gz .pnp.* -data \ No newline at end of file +data +static/index.html \ No newline at end of file diff --git a/api.js b/api.js deleted file mode 100644 index f773aab..0000000 --- a/api.js +++ /dev/null @@ -1,5 +0,0 @@ -const app = require("./exposer.js").app - -app.get("/*", (req, res) => { - res.send("
Like... alot later.") -}) \ No newline at end of file diff --git a/exposer.js b/exposer.js deleted file mode 100644 index 3f7ebb2..0000000 --- a/exposer.js +++ /dev/null @@ -1,52 +0,0 @@ -const express = require("express"), -paths = require("./fileManager.js") -path = require("path"), -fs = require("fs"), -WebSocket = require("ws") - -const PORT = process.env.PORT || 8080 - -var app = express() - -app.use(express.static(paths.data)) - -app.listen(PORT, () => { - console.log("Violet's Limbo is now listening on port: " + PORT) -}) - -var sockets = [] - -wsServer = WebSocket.Server; -let server = require('http').createServer() -wsServer = new wsServer({ - server: server, - perMessageDeflate: false -}) -server.on('request', app) - -wsServer.on("connection", function connection(socket) { - socket.on('message', function message(data) { - data = JSON.parse(data) - if (data.type == "ping") { - for (let index = 0; index < sockets.length; index++) { - const socketData = sockets[index]; - if (socketData.socket == socket) { - sockets[index].lastPing = Date.now() - } - } - - socket.send(`{"op": 3}`) - } else { - console.log(data) - } - }) - - sockets.push({ socket, lastPing: Date.now() }) -}) - - -module.exports = { - app: app -} - -require("./api.js") \ No newline at end of file diff --git a/fileManager.js b/fileManager.js deleted file mode 100644 index 9ee915b..0000000 --- a/fileManager.js +++ /dev/null @@ -1,30 +0,0 @@ -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) \ No newline at end of file diff --git a/index.js b/index.js index 18d42bd..d95bc9a 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,101 @@ const express = require("express"), +chokidar = require("chokidar"), fs = require("fs"), -path = require("path") +path = require("path"), +showdown = require("showdown"), +mkthtml = new showdown.Converter() -require("./exposer.js") +mkthtml.setFlavor("github") + +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 += + `
` + + 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}", "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!
") + res.send(html) + + return + +}) process.on('uncaughtException', (err, origin) => { fs.writeSync( diff --git a/package-lock.json b/package-lock.json index c981df9..b480755 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,8 +11,7 @@ "dependencies": { "chokidar": "^3.5.3", "express": "^4.18.2", - "showdown": "^2.1.0", - "ws": "^8.18.0" + "showdown": "^2.1.0" } }, "node_modules/accepts": { @@ -849,27 +848,6 @@ "engines": { "node": ">= 0.8" } - }, - "node_modules/ws": { - "version": "8.18.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", - "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", - "license": "MIT", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": ">=5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } } } } diff --git a/package.json b/package.json index 2b4bb12..4bfe6b8 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "dependencies": { "chokidar": "^3.5.3", "express": "^4.18.2", - "showdown": "^2.1.0", - "ws": "^8.18.0" + "showdown": "^2.1.0" } } diff --git a/resources/mainPage.html b/resources/mainPage.html new file mode 100644 index 0000000..b5fa166 --- /dev/null +++ b/resources/mainPage.html @@ -0,0 +1,25 @@ + + + + + +Violet's Limbo is... my blog! I talk about game design, development, and whatever else comes to mind!
+Enjoy my bad opinions!