Skip to content

Commit

Permalink
working on mariadb compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
SorenEricMent committed Oct 28, 2022
1 parent 5da1652 commit 00b08af
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
10 changes: 7 additions & 3 deletions main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const http = require('http');
const readline = require('node:readline');

async function wrapper() {
var meta = {
"_mariadb": false
};
if(process.argv.indexOf("--iapi-shell") !== -1){
console.log("[WRAPPER] Entering IAPI shell testing environment...");
const initializeBlorumServer = (await import("./modules/init.mjs")).initializeBlorumServer;
Expand All @@ -18,7 +21,7 @@ async function wrapper() {
});

const IAPI = (await import("./modules/iapi.mjs")).IAPI;
const iapi = new IAPI(results.mysql, results.redis, results.siteConfig, results.log, results.bootConfig.security.digest_salt, results.bootConfig.database.redis.prefix, scheduleDaemon);
const iapi = new IAPI(meta, results.mysql, results.redis, results.siteConfig, results.log, results.bootConfig.security.digest_salt, results.bootConfig.database.redis.prefix, scheduleDaemon);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
Expand All @@ -40,6 +43,7 @@ async function wrapper() {
//const extensionList = (await import("./modules/extension.mjs")).getExtensionsList;
const prerequisite = initializeBlorumServer();
prerequisite.then(async (results) => {
meta._mariadb = results._mariadb;
scheduleDaemon = results.scheduleDaemon;
scheduleDaemon.on('message', (message) => {
switch (message.action) {
Expand All @@ -50,7 +54,7 @@ async function wrapper() {
});

const IAPI = (await import("./modules/iapi.mjs")).IAPI;
const iapi = new IAPI(results.mysql, results.redis, results.siteConfig, results.log, results.bootConfig.security.digest_salt, results.bootConfig.database.redis.prefix, scheduleDaemon);
const iapi = new IAPI(meta, results.mysql, results.redis, results.siteConfig, results.log, results.bootConfig.security.digest_salt, results.bootConfig.database.redis.prefix, scheduleDaemon);
results.log("log", "Main", "Blorum pre-initialization finished.");
let router = initializeRouter(iapi, results.mysql, results.redis, results.siteConfig, results.log);
if (results.bootConfig.port <= 1000 && results.bootConfig.port != 0) {
Expand All @@ -61,7 +65,7 @@ async function wrapper() {
router
).listen(results.bootConfig.port, function () {
results.log("log", "Main", "Blorum Server started on port " + finalServer.address().port);
console.log("Welcome to Blorum, made with by Winslow S.E.M.");
console.log("Welcome to Blorum, made with spaghetti by Winslow S.E.M.");
}).on('error', function (err) {
results.log("error", "Main", "Blorum Server failed to start on port " + results.bootConfig.port);
results.log("error", "Main", err);
Expand Down
5 changes: 3 additions & 2 deletions modules/iapi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { v4 as uuidv4 } from 'uuid';
import stringify from "quick-stable-stringify";

class IAPI {
constructor(mysql, redis, siteConfig, log, salt, redisPrefix, scheduleDaemon) {
constructor(meta, mysql, redis, siteConfig, log, salt, redisPrefix, scheduleDaemon) {
this.meta = meta;
this.mysql = mysql;
this.redis = redis;
this.siteConfig = siteConfig;
Expand Down Expand Up @@ -600,7 +601,7 @@ class IAPI {
let statusProto = {};

}
alterArticle(){
alterArticle(id, actionList){

}
deleteArticle(id){
Expand Down
17 changes: 16 additions & 1 deletion modules/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { promisifiedMysqlConnect, promisifiedRedisConnect } from "./utils.mjs";
import { default as child_process } from 'child_process';

import stringify from "quick-stable-stringify";
import parse from "simdjson";
JSON.parse = parse.parse;

var _mariadb = false;

function initializeBlorumServer() {
const __dirname = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -55,6 +59,12 @@ function initializeBlorumServer() {
let mysqlPromise = promisifiedMysqlConnect(mysqlConnection);
mysqlPromise.then(function () {
log("log", "INIT/db/mysql", "Successfully connected to MySQL Server.");
let mysqlServerVer = mysqlConnection._handshakePacket.serverVersion.toLowerCase();
if(mysqlServerVer.indexOf("mariadb") !== -1){
log("warn", "INIT", "MariaDB detected, using MariaDB compatibility mode.");
log("warn", "INIT", "MariaDB compatibility mode is not fully tested, please consider using MySQL instead.");
_mariadb = true;
}
}).catch(function (err) {
log("error", "INIT/db/mysql", "Failed to connect to MySQL Server.");
reject(err);
Expand Down Expand Up @@ -83,6 +93,10 @@ function initializeBlorumServer() {
try {
let keyName = redisKey + element.name;
delete element.name;
if(_mariadb){
element.permissions = JSON.parse(element.permissions);
element.rate_limits = JSON.parse(element.rate_limits);
}
redisConn.set(keyName, stringify(element));
} catch (error) {
log("error", "INIT/db/redis", "Failed to set role in redis.");
Expand All @@ -109,7 +123,8 @@ function initializeBlorumServer() {
"redis": redisConn,
"siteConfig": siteConfig,
"bootConfig": bootConfig,
"scheduleDaemon": scheduleDaemon
"scheduleDaemon": scheduleDaemon,
"_mariadb": _mariadb
});
break;
}
Expand Down
13 changes: 6 additions & 7 deletions modules/scheduled.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { default as process } from 'process';
import { clearInterval } from 'timers';

var taskList = [];
var mysql, redis;
var cacheMap = {};
var mainLoop = {
"_destroyed": true
};

function log(level, message){
process.send({
"id": null,
"action": "log",
"level": level,
"info": message
Expand All @@ -20,6 +22,7 @@ function beforeInit(message){
mysql = message.mysql;
redis = message.redis;
process.send({
"id": null,
"action": "init"
});
eventExecutor = afterInit;
Expand All @@ -38,19 +41,15 @@ function afterInit(message){
clearInterval(mainLoop);
case "loop_status":
process.send({
"id": message.id,
"status": mainLoop._destroyed
});
case "create_task":
break;
case "fetch_task_list":
break;
default:
process.send({
"action": "log",
"id": message.id,
"level": "log",
"info": JSON.stringify(message)
});
log("error", "Unknown action, message: " + message)
}
}

Expand Down

0 comments on commit 00b08af

Please sign in to comment.