started implementing data functions for easy requests to data

This commit is contained in:
max
2025-09-23 15:01:28 +01:00
parent 09967a0be9
commit 5e4eb38763
3 changed files with 82 additions and 19 deletions

View File

@@ -8,6 +8,8 @@
"date-fns": "^4.1.0",
"ejs": "^3.1.10",
"express": "^5.1.0",
"markdown-it": "^14.1.0"
"markdown-it": "^14.1.0",
"mysql": "^2.18.1",
"package.json": "^2.0.1"
}
}

57
src/data.js Normal file
View File

@@ -0,0 +1,57 @@
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const config = require("../config.json")
const fs = require("fs")
export function getdata(data) {
if (config["data_storage"] == "json") {
if (data == "posts" || data == "users") {
let result = require(`../data/${data}.json`)
return result
}
else if (data == "comments") {
let result = require("../data/comments.json")
return result.comments
}
else if (data == "hitcount") {
let result = fs.readFileSync("../data/hitcount.txt")
return result
}
else {
console.log("Error, invalid requested")
return 1
}
}
if (config["data_storage"] == "mysql") {
const mysql = require('mysql');
let con = mysql.createConnection({
host: config.database.host,
user: config.database.user,
password: config.database.password,
database: config.database.database,
});
con.connect(function(err) {
if (err) throw err;
if (data == "posts" || data == 'users' || data == 'comments') {
con.query(`SELECT * FROM ${data}`, function (err, result, fields) {
if (err) throw err;
console.log(result)
return result;
});
}
else if (data == 'hitcount') {
con.query(`SELECT paramValue FROM params WHERE paramName = '${data}'`, function (err, result, fields) {
if (err) throw err;
console.log(result)
return result;
});
}
});
}
}

View File

@@ -9,8 +9,10 @@ const crypto = require('crypto'); // For encrypting passwords, I use sha512
const { fromUnixTime, format, getUnixTime } = require("date-fns") // A date utility library
const ejs = require("ejs")
const func = require("./functions.js")
const data = require("./data.js")
const init = require("./initialise.js")
// There's only one possible argument, so we can just check if the user passed that one
// TODO I plan on adding more such as --help and --post so I should make this more robust at some point
if (process.argv[2] == "--first-time") {
@@ -23,12 +25,13 @@ let posts // contains a list of posts,
let comments // contains a list of comments
let config // contains a set of configuration for the site, see example-config.js for an example
config = require('../config.json');
if (config["data_storage"] == "json") {
try {
// We're going to try and import the modules,
users = require('../data/users.json');
posts = require('../data/posts.json');
comments = require('../data/comments.json');
config = require('../config.json');
}
catch (error) {
// if they don't all import then
@@ -38,6 +41,7 @@ catch (error) {
console.log(error)
process.exit(1)
}
}
// Import the locale
try {
@@ -178,10 +182,10 @@ app.get("/", (req,res) => {
{
config,
locale,
posts,
users,
comments: comments.comments,
hitcount: fs.readFileSync("../data/hitcount.txt"),
posts: data.getdata("posts"),
users: data.getdata("users"),
comments: data.getdata("comments"),
hitcount: data.getdata("hitcount"),
fromUnixTime,
format,
getUnixTime,