commit d55f104aa585d80247efd7be9e69b6f05b26b4c0 Author: DeaDvey Date: Sat Jun 7 14:55:04 2025 +0100 initial commit diff --git a/app.js b/app.js new file mode 100644 index 0000000..8c07af8 --- /dev/null +++ b/app.js @@ -0,0 +1,117 @@ +const express = require('express'); +const crypto = require('crypto'); +const fs = require('fs'); +const users = require('./users.js'); +const posts = require('./posts.js'); +const config = require('./config.js'); +const app = express(); +const port = 8005; + +app.use(express.urlencoded({ extended: true })); +app.use(express.json()); +app.use(express.static(config.root_path)); + +function unix_time_to_date_format(unix_time) { + let date_object = new Date(unix_time) + let formatter = new Intl.DateTimeFormat(config.date_format,config.date_options); + let formatted_time = formatter.format(date_object); + + return formatted_time +} + +function replace_format_indicators(input_string, post_index) { + post_object = posts.posts[post_index] + output_string = input_string + .replace("%A", (post_object["tags"])) + .replace("%C", post_object["content"]) + .replace("%D", unix_time_to_date_format(post_object["pubdate"])) + .replace("%E", unix_time_to_date_format(post_object["editdate"])) + .replace("%L", `/post/${post_index}`) + .replace("%N", post_object["poster"]) + .replace("%P", "/post") + .replace("%O", "/edit") + .replace("%R", "/rss") + .replace("%S", config.seperator) + .replace("%T", post_object["title"]) + .replace("%U", `/user/${post_object["poster"]}`) + .replace("%Y", config.site_name) + .replace("%W", config.site_description) + + return output_string +} + +app.get("/", (req,res) => { + header_div = config.timeline_header + header_div = replace_format_indicators(header_div, 0); + posts_div = ""; + counter = posts.posts.length - 1; + while ((counter >= 0) && (counter > (posts.posts.length - (config.timeline_length + 1)))) + { + let post = config.timeline_post_format; + posts_div += replace_format_indicators(post, counter); + counter -= 1; + } + res.send(`
${posts_div}
`); +}); + +app.get("/post", (req,res) => { + res.send(`
+
+
+
+
+
+ +
`); +}); +app.get("/edit", (req,res) => { + res.send(`Edit page under construction`); +}); + +app.get("/user/:username", (req, res) => { + header_div = `

${req.params.username}

` + posts_div = ""; + for (let post_index = posts.posts.length-1; post_index >= 0; post_index--) { + if (posts.posts[post_index]["poster"] == req.params.username) { + let post = config.user_post_format; + posts_div += replace_format_indicators(post, post_index); + } + } + res.send(`
${posts_div}
`); +}); +app.get("/post/:post_index", (req, res) => { + post_div = ""; + let post = config.post_page_format; + post_div += replace_format_indicators(post, req.params.post_index); + res.send(`
${post_div}
`); +}); + +app.post("/submit_post", (req,res) => { + const password = crypto.createHash('sha512').update(req.body.password).digest('hex'); + const username = req.body.username + const title = req.body.title + const content = req.body.content + const tags = req.body.tags.split(','); + const datetime = new Date().getTime() + console.log(username, "is submitting a post titled:", title); + + if (users.users[username] == password) { // Password matches + posts.posts.push({ + "poster": username, + "title": title, + "content": content, + "pubdate": datetime, + "editdate": datetime, + "tags": tags, + }) + fs.writeFileSync(`${config.root_path}/posts.js`, `export const posts = ${JSON.stringify(posts.posts)}`, 'utf-8'); + res.redirect(302, "/"); + } + else { + res.send(`Invalid Password for user`,username); + } +}); + +app.listen(port, () => { + console.log(`Server is running at http://localhost:${port}`); +}); diff --git a/config.js b/config.js new file mode 100644 index 0000000..7cc5b05 --- /dev/null +++ b/config.js @@ -0,0 +1,59 @@ +export const seperator = "
" +export const site_name = "DeaDvey's Blog" +export const site_description = "Films, tech, random shit" +export const timeline_length = 20 +export const root_path = "/home/gaming/code/web/blogger" + +// Date format using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString +export const date_format = 'en-GB'; +export const date_options = { + day: '2-digit', + month: '2-digit', + year: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: true, + timeZone: 'UTC' +} + +//// Format ///// +// The syntax for this is pretty simple +// %A - List of tags +// %C - Post content +// %D - Published date in the format specified by date_format +// %E - Edited date in the format specified by date_format +// %L - URL Permanent link to the post +// %N - the username of the user (poster) +// %P - URL to create a new post +// %O - URL to edit a post +// %R - Site wide RSS feed +// %S - post seperator as defined by post_seperator +// %T - Title +// %U - URL the the user (poster) +// %Y - Site Name as defined by site_name +// %W - Site Description as defined by site_description + +export const timeline_header = `

%Y

+

%W

+Create Post
+Edit Post
+RSS Feed +%S` + +export const user_post_format = `

%T

+

%C

+%A
+Permalink
+%S` +export const post_page_format = `

%T

+

%C

+%A
+By %N
Posted: %D
+Edited: %E` +export const timeline_post_format = `

%T

+

%C

+Permalink
+By %N +%S`