initial commit

This commit is contained in:
DeaDvey 2025-06-07 14:55:04 +01:00
commit d55f104aa5
2 changed files with 176 additions and 0 deletions

117
app.js Normal file
View File

@ -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(`<html><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body></html>`);
});
app.get("/post", (req,res) => {
res.send(`</html><form action="/submit_post" method="POST" onsubmit="sha512password()">
<label>Username: </label><input required name="username"><br/>
<label>Password: </label><input required id="password" name="password"><br/>
<label>Title: </label><input required name="title"><br/>
<label>Content: </label><textarea required name="content"></textarea><br/>
<label>Tags (comma seperated): </label><input name="tags"><br/>
<input type="submit" value="Submit">
</form></html>`);
});
app.get("/edit", (req,res) => {
res.send(`Edit page under construction`);
});
app.get("/user/:username", (req, res) => {
header_div = `<h1>${req.params.username}</h1>`
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(`<html><body><div id="header">${header_div}</div><div id="posts">${posts_div}</div></body></html>`);
});
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(`<html><body><div id="posts">${post_div}</div></body></html>`);
});
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}`);
});

59
config.js Normal file
View File

@ -0,0 +1,59 @@
export const seperator = "<hr/>"
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 = `<h1>%Y</h1>
<h2>%W</h2>
<a href="%P">Create Post</a><br/>
<a href="%O">Edit Post</a><br/>
<a href="%R">RSS Feed</a>
%S`
export const user_post_format = `<h2>%T</h2>
<p>%C</p>
<i>%A</i><br/>
<a href="%L">Permalink</a><br/>
%S`
export const post_page_format = `<h1>%T</h1>
<p>%C</p>
<i>%A</i><br/>
<i>By <a href="%U">%N</a></i><br/<
<i>Posted: %D</i><br/>
<i>Edited: %E</i>`
export const timeline_post_format = `<h3>%T</h3>
<p>%C</p>
<a href="%L">Permalink</a><br/>
<i>By <a href="%U">%N</a></i>
%S`