diff --git a/.gitignore b/.gitignore index 48e99b1..1d5d8e5 100644 --- a/.gitignore +++ b/.gitignore @@ -130,5 +130,4 @@ dist .yarn/install-state.gz .pnp.* -data -static/index.html \ No newline at end of file +data \ No newline at end of file diff --git a/expressHandler.js b/expressHandler.js new file mode 100644 index 0000000..741d4f5 --- /dev/null +++ b/expressHandler.js @@ -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) +}) \ No newline at end of file diff --git a/fileManager.js b/fileManager.js new file mode 100644 index 0000000..9ee915b --- /dev/null +++ b/fileManager.js @@ -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) \ No newline at end of file diff --git a/index.js b/index.js index d95bc9a..ec50904 100644 --- a/index.js +++ b/index.js @@ -1,101 +1,8 @@ const express = require("express"), -chokidar = require("chokidar"), fs = require("fs"), -path = require("path"), -showdown = require("showdown"), -mkthtml = new showdown.Converter() +path = require("path") -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 += - `
- -

${post.name}

-

${post.desc}

-

Path: /post/${post.path}

-
-
` - - 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 - -}) +require("./expressHandler.js") process.on('uncaughtException', (err, origin) => { fs.writeSync( diff --git a/pageUpdater.js b/pageUpdater.js new file mode 100644 index 0000000..71a7b35 --- /dev/null +++ b/pageUpdater.js @@ -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 += + `
+ +

${post.name}

+

${post.desc}

+

Path: /post/${post.path}

+
+
` + + 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}", "

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 html + } +} \ No newline at end of file