From 5e4eb38763a7218ca34240eeb263871c5b4b5796 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 23 Sep 2025 15:01:28 +0100 Subject: [PATCH] started implementing data functions for easy requests to data --- package.json | 4 +++- src/data.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/server.js | 40 ++++++++++++++++++++---------------- 3 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 src/data.js diff --git a/package.json b/package.json index 43ccbaa..103e11d 100755 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/data.js b/src/data.js new file mode 100644 index 0000000..42c15ce --- /dev/null +++ b/src/data.js @@ -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; + }); + + } + }); + } +} diff --git a/src/server.js b/src/server.js index dd34430..2f8382a 100644 --- a/src/server.js +++ b/src/server.js @@ -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,20 +25,22 @@ 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 -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 - // inform the user to pass --first-time and exit with an error code - console.log("A file is missing!") - console.log("Run with --first-time to initialise the program") - console.log(error) - process.exit(1) +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'); + } + catch (error) { + // if they don't all import then + // inform the user to pass --first-time and exit with an error code + console.log("A file is missing!") + console.log("Run with --first-time to initialise the program") + console.log(error) + process.exit(1) + } } // Import the locale @@ -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,