This commit is contained in:
Benjamin Toby
2025-02-16 17:12:40 +01:00
parent e9761cc971
commit e95f4d1087
628 changed files with 3091 additions and 1073 deletions
@@ -0,0 +1,88 @@
import { _n } from "@/client-exports";
import getQueue from "@/package-shared/functions/backend/queues/get-queue";
import updateQueue from "@/package-shared/functions/backend/queues/update-queue";
import { DSQL_DATASQUIREL_PROCESS_QUEUE } from "@/package-shared/types/dsql";
import debugLog from "@/package-shared/utils/logging/debug-log";
function debugLogFn(log: any, label?: string) {
debugLog({ log, addTime: true, label, title: __filename.split("/").pop() });
}
export default async function cronHandleQueue() {
const INTERVAL = 5000;
while (true) {
await (async () => {
const lastQueueItemRes = (await getQueue({
query: {
query: {
error: {
value: "0",
},
running: {
value: "0",
},
success: {
value: "0",
},
},
order: {
field: "id",
strategy: "ASC",
},
limit: 1,
},
})) as DSQL_DATASQUIREL_PROCESS_QUEUE[] | undefined;
const lastQueueItem = lastQueueItemRes?.[0];
if (!lastQueueItem) return;
debugLogFn(lastQueueItem.title, "Running Queue");
await updateQueue({
queueId: _n(lastQueueItem.id),
queue: {
running: 1,
},
});
try {
switch (lastQueueItem.job_type) {
/**
* # Dummy Queue
*/
case "dummy":
await Bun.sleep(20000);
break;
/**
* # Unhandled
*/
default:
return;
}
} catch (error: any) {
debugLogFn(error.message, "ERROR");
await updateQueue({
queueId: _n(lastQueueItem.id),
queue: {
running: 0,
error: 1,
error_message: String(error.message),
},
});
return;
}
await updateQueue({
queueId: _n(lastQueueItem.id),
queue: {
success: 1,
},
});
})();
await Bun.sleep(INTERVAL);
}
}
@@ -0,0 +1,55 @@
import createDbFromSchema from "@/package-shared/shell/createDbFromSchema";
import grabDirNames from "@/package-shared/utils/backend/names/grab-dir-names";
import debugLog from "@/package-shared/utils/logging/debug-log";
import dbSchemaToType from "@/utils/backend/db-schema-to-type";
import fs from "fs";
function debugLogFn(log: any, label?: string) {
debugLog({ log, addTime: true, label, title: "watchMainDbSchemaJSONFile" });
}
let syncing = 0;
let timeout: any;
const DEBOUNCE = 500;
export default function watchMainDbSchemaJSONFile() {
if (process.env.NODE_ENV?.match(/prod/)) return;
if (process.env.DSQL_HOST_ENV?.match(/prod/)) return;
const { mainShemaJSONFilePath, mainDbTypeDefFile } = grabDirNames();
fs.watch(mainShemaJSONFilePath, (curr) => {
if (syncing == 1) return;
if (curr !== "change") return;
clearTimeout(timeout);
debugLogFn("Main Schema JSON File Changed!");
syncing = 1;
timeout = setTimeout(() => {
const definitions = dbSchemaToType();
fs.writeFileSync(
mainDbTypeDefFile,
definitions?.join("\n\n") || "",
"utf-8"
);
createDbFromSchema({})
.then((res) => {
debugLogFn(res, "res");
})
.catch((err) => {
debugLogFn(err, "res");
})
.finally(() => {
setTimeout(() => {
debugLogFn("Main Rebuilt Successfully!");
syncing = 0;
}, 1000);
});
}, DEBOUNCE);
});
}
+1
View File
@@ -0,0 +1 @@
@moduletrace:registry=https://git.tben.me/api/packages/moduletrace/npm/
+15
View File
@@ -0,0 +1,15 @@
FROM node:20-bookworm
RUN apt-get update
RUN apt-get install -y apt-transport-https git ca-certificates curl gnupg python3 python3-pip make build-essential mariadb-client wget zip unzip xz-utils
RUN curl -fsSL https://bun.sh/install | bash
ENV PATH="/root/.bun/bin:${PATH}"
COPY .npmrc /root/.bun/install/global/.npmrc
RUN bun add -g @moduletrace/turbosync
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
+9
View File
@@ -0,0 +1,9 @@
#!/bin/bash
cd /app
if [ "$NODE_ENV" == "production" ]; then
bun docker/cron/index.ts
else
bun --watch docker/cron/index.ts
fi
+28
View File
@@ -0,0 +1,28 @@
import mysql from "serverless-mysql";
global.DSQL_DB_CONN = mysql({
config: {
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_USERNAME,
password: process.env.DSQL_DB_PASSWORD,
database: process.env.DSQL_DB_NAME,
charset: "utf8mb4",
},
});
global.DSQL_USE_LOCAL = true;
import watchMainDbSchemaJSONFile from "./(functions)/watch-main-db-schema-json-file";
import cronHandleQueue from "./(functions)/queue/handle-queue";
console.log("Running Cron Services ....");
/**
* # Watch Main JSON DB Schema File
*/
watchMainDbSchemaJSONFile();
/**
* # Handle Queue
*/
cronHandleQueue();
+1
View File
@@ -0,0 +1 @@
mariadb -u root -p$MARIADB_ROOT_PASSWORD
+2
View File
@@ -0,0 +1,2 @@
SHOW GLOBAL STATUS LIKE 'wsrep_%';
SHOW GLOBAL STATUS LIKE 'wsrep_cluster_%';
+11
View File
@@ -0,0 +1,11 @@
FROM mariadb:11-jammy
RUN apt update
RUN apt install -y curl wget zip unzip xz-utils galera-4
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
COPY .bash_history /root/.bash_history
CMD [ "mariadbd","--wsrep-new-cluster" ]
@@ -0,0 +1,16 @@
[mysqld]
# Mandatory settings
wsrep_on = ON
wsrep_cluster_name = "MariaDB Galera Cluster"
wsrep_cluster_address = gcomm://mariadb-node1,mariadb-node2,mariadb-node3
binlog_format = row
default_storage_engine = InnoDB
innodb_autoinc_lock_mode = 2
# Allow server to accept connections on all interfaces.
bind-address = 0.0.0.0
# Optional settings
#wsrep_slave_threads = 1
#innodb_flush_log_at_trx_commit = 0
@@ -0,0 +1,8 @@
[mariadb]
wsrep_on = ON
wsrep_cluster_address = gcomm://
wsrep_provider = /usr/lib/libgalera_smm.so
binlog_format = ROW
default_storage_engine = InnoDB
@@ -0,0 +1,8 @@
[mariadb]
wsrep_on = ON
wsrep_cluster_address = gcomm://mariadb-node1
wsrep_provider = /usr/lib/libgalera_smm.so
binlog_format = ROW
default_storage_engine = InnoDB
@@ -0,0 +1,8 @@
[mariadb]
wsrep_on = ON
wsrep_cluster_address = gcomm://mariadb-node1
wsrep_provider = /usr/lib/libgalera_smm.so
binlog_format = ROW
default_storage_engine = InnoDB
+12
View File
@@ -0,0 +1,12 @@
[mysqld]
max_connections = 200
skip-networking=0
skip-bind-address
ssl-ca = /ssl/ca-cert.pem
ssl-cert = /ssl/server-cert.pem
ssl-key = /ssl/server-key.pem
+9
View File
@@ -0,0 +1,9 @@
[mariadb]
wsrep_on = ON
wsrep_cluster_address = gcomm://mariadb-node1,mariadb-node2,mariadb-node3
wsrep_provider = /usr/lib/libgalera_smm.so
wsrep_provider_options ="socket.ssl_key=/ssl/server-key.pem;socket.ssl_cert=/ssl/server-cert.pem;socket.ssl_ca=/ssl/ca-cert.pem"
binlog_format = ROW
default_storage_engine = InnoDB
@@ -0,0 +1 @@
11.6.2-MariaDB
@@ -0,0 +1 @@
11.3.2-MariaDB
@@ -0,0 +1 @@
11.3.2-MariaDB
@@ -0,0 +1,54 @@
name: galera
services:
mariadb-node1:
build:
context: .
dockerfile: Dockerfile
networks:
galera:
container_name: mariadb-node1
environment:
- MARIADB_ROOT_PASSWORD=password
volumes:
- ./conf.d:/etc/mysql/conf.d
- ./ssl:/ssl
- ./db:/var/lib/mysql
mariadb-node2:
build:
context: .
dockerfile: Dockerfile
container_name: mariadb-node2
networks:
galera:
environment:
- MARIADB_ROOT_PASSWORD=password
- MARIADB_SLAVE=1
volumes:
- ./conf.d:/etc/mysql/conf.d
- ./ssl:/ssl
- ./db2:/var/lib/mysql
command: ["mariadbd"]
entrypoint: null
mariadb-node3:
build:
context: .
dockerfile: Dockerfile
container_name: mariadb-node3
networks:
galera:
environment:
- MARIADB_ROOT_PASSWORD=password
- MARIADB_SLAVE=1
volumes:
- ./conf.d:/etc/mysql/conf.d
- ./ssl:/ssl
- ./db3:/var/lib/mysql
command: ["mariadbd"]
entrypoint: null
networks:
galera:
driver: bridge
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
mariadbd --wsrep-new-cluster
@@ -0,0 +1,97 @@
version: '2'
services:
node1-mariadb:
image: hauptmedia/mariadb:10.1
hostname: node1-mariadb
container_name: node1-mariadb
environment:
MYSQL_ROOT_PASSWORD: 'iamgroot'
REPLICATION_PASSWORD: 'iamgroot'
GALERA: 'On'
NODE_NAME: node1-mariadb
CLUSTER_NAME: maria_cluster
CLUSTER_ADDRESS: gcomm://
TZ : 'Asia/Seoul'
ports:
- 13306:3306/tcp
volumes:
- ./galeranode1/mariadb:/var/lib/mysql
- ./sqldir:/docker-entrypoint-initdb.d
command:
--wait_timeout=28800
--character-set-server=utf8
--collation-server=utf8_general_ci
--max-allowed-packet=512M
--net-buffer-length=5048576
--wsrep-new-cluster
stdin_open: true
tty: true
privileged: true
ulimits:
nofile:
soft: 600000
hard: 640000
node2-mariadb:
image: hauptmedia/mariadb:10.1
hostname: node2-mariadb
container_name: node2-mariadb
links:
- node1-mariadb
environment:
REPLICATION_PASSWORD: 'iamgroot'
GALERA: 'On'
NODE_NAME: node2-mariadb
CLUSTER_NAME: maria_cluster
CLUSTER_ADDRESS: gcomm://node1-mariadb
TZ : 'Asia/Seoul'
ports:
- 23306:3306/tcp
volumes:
- ./galeranode2/mariadb:/var/lib/mysql
command:
--wait_timeout=28800
--character-set-server=utf8
--collation-server=utf8_general_ci
--max-allowed-packet=512M
--net-buffer-length=5048576
stdin_open: true
tty: true
privileged: true
depends_on:
- node1-mariadb
ulimits:
nofile:
soft: 600000
hard: 640000
node3-mariadb:
image: hauptmedia/mariadb:10.1
hostname: node3-mariadb
container_name: node3-mariadb
links:
- node1-mariadb
environment:
REPLICATION_PASSWORD: 'iamgroot'
GALERA: 'On'
NODE_NAME: node3-mariadb
CLUSTER_NAME: maria_cluster
CLUSTER_ADDRESS: gcomm://node1-mariadb
TZ : 'Asia/Seoul'
ports:
- 33306:3306/tcp
volumes:
- ./galeranode3/mariadb:/var/lib/mysql
command:
--wait_timeout=28800
--character-set-server=utf8
--collation-server=utf8_general_ci
--max-allowed-packet=512M
--net-buffer-length=5048576
stdin_open: true
tty: true
privileged: true
depends_on:
- node1-mariadb
- node2-mariadb
@@ -0,0 +1,105 @@
version: '2'
services:
node1-mariadb:
image: hauptmedia/mariadb:10.1
hostname: node1-mariadb
container_name: node1-mariadb
environment:
MYSQL_ROOT_PASSWORD: 'iamgroot'
REPLICATION_PASSWORD: 'iamgroot'
GALERA: 'On'
NODE_NAME: node1-mariadb
CLUSTER_NAME: maria_cluster
CLUSTER_ADDRESS: gcomm://
TZ : 'Asia/Seoul'
ports:
- 13306:3306/tcp
volumes:
- /data/someone/mariadb/galeranode1/mariadb:/var/lib/mysql
- /data/someone/mariadb/sqldir:/docker-entrypoint-initdb.d
command:
--wait_timeout=28800
--character-set-server=utf8
--collation-server=utf8_general_ci
--max-allowed-packet=512M
--net-buffer-length=5048576
--wsrep-new-cluster
stdin_open: true
tty: true
privileged: true
ulimits:
nofile:
soft: 600000
hard: 640000
labels:
io.rancher.scheduler.affinity:host_label: labelkey1=labelkvalue1
io.rancher.container.pull_image: always
node2-mariadb:
image: hauptmedia/mariadb:10.1
hostname: node2-mariadb
container_name: node2-mariadb
links:
- node1-mariadb
environment:
REPLICATION_PASSWORD: 'iamgroot'
GALERA: 'On'
NODE_NAME: node2-mariadb
CLUSTER_NAME: maria_cluster
CLUSTER_ADDRESS: gcomm://node1-mariadb
TZ : 'Asia/Seoul'
ports:
- 23306:3306/tcp
volumes:
- /data/someone/galeranode2/mariadb:/var/lib/mysql
command:
--wait_timeout=28800
--character-set-server=utf8
--collation-server=utf8_general_ci
--max-allowed-packet=512M
--net-buffer-length=5048576
stdin_open: true
tty: true
privileged: true
depends_on:
- node1-mariadb
ulimits:
nofile:
soft: 600000
hard: 640000
labels:
io.rancher.scheduler.affinity:host_label: labelkey2=labelkvalue2
io.rancher.container.pull_image: always
node3-mariadb:
image: hauptmedia/mariadb:10.1
hostname: node3-mariadb
container_name: node3-mariadb
links:
- node1-mariadb
environment:
REPLICATION_PASSWORD: 'iamgroot'
GALERA: 'On'
NODE_NAME: node3-mariadb
CLUSTER_NAME: maria_cluster
CLUSTER_ADDRESS: gcomm://node1-mariadb
TZ : 'Asia/Seoul'
ports:
- 33306:3306/tcp
volumes:
- /data/someone/galeranode3/mariadb:/var/lib/mysql
command:
--wait_timeout=28800
--character-set-server=utf8
--collation-server=utf8_general_ci
--max-allowed-packet=512M
--net-buffer-length=5048576
stdin_open: true
tty: true
privileged: true
depends_on:
- node1-mariadb
- node2-mariadb
labels:
io.rancher.scheduler.affinity:host_label: labelkey3=labelkvalue3
io.rancher.container.pull_image: always
+18
View File
@@ -0,0 +1,18 @@
#!/bin/bash
checkvariablecnt=$#
if [ $checkvariablecnt == 4 ]; then
echo "Migrate to an existing [ $3 ]schema. "
mysql -u$1 -p$2 --database=$3 < $4
else
echo "Migrate file $3 "
mysql -u$1 -p$2 < $3
fi
+12
View File
@@ -0,0 +1,12 @@
FROM mariadb:11-jammy
RUN apt update
RUN apt install -y curl wget zip unzip xz-utils galera-4
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
COPY .bash_history /root/.bash_history
ENTRYPOINT ["entrypoint.sh"]
CMD [ "mariadbd" ]
@@ -0,0 +1,45 @@
name: galera
services:
mariadb-node1:
build:
context: .
dockerfile: Dockerfile
networks:
galera:
container_name: mariadb-node1
environment:
- MARIADB_ROOT_PASSWORD=password
volumes:
- ./conf.d/node1.cnf:/etc/mysql/conf.d/galera.cnf
- ./db:/var/lib/mysql
mariadb-node2:
build:
context: .
dockerfile: Dockerfile
container_name: mariadb-node2
networks:
galera:
environment:
- MARIADB_ROOT_PASSWORD=password
volumes:
- ./conf.d/node2.cnf:/etc/mysql/conf.d/galera.cnf
command: ["mariadbd"]
mariadb-node3:
build:
context: .
dockerfile: Dockerfile
container_name: mariadb-node3
networks:
galera:
environment:
- MARIADB_ROOT_PASSWORD=password
volumes:
- ./conf.d/node3.cnf:/etc/mysql/conf.d/galera.cnf
command: ["mariadbd"]
networks:
galera:
driver: bridge
+31
View File
@@ -0,0 +1,31 @@
#!/bin/bash
# Path to the grastate.dat file
GRSTATE_FILE="/var/lib/mysql/grastate.dat"
# Check if the MARIADB_SLAVE environment variable is set
if [ ! -z "$MARIADB_SLAVE" ]; then
# If MARIADB_SLAVE is set, this node is a slave, so just run mariadbd (no --wsrep-new-cluster)
echo "MARIADB_SLAVE environment variable is set. Starting as a slave node."
exec mariadbd "$@"
else
# If grastate.dat exists, check if the cluster has already been bootstrapped
if [ -f "$GRSTATE_FILE" ]; then
# Read the value of safe_to_bootstrap from grastate.dat
SAFE_TO_BOOTSTRAP=$(grep -E "^safe_to_bootstrap" "$GRSTATE_FILE" | cut -d ':' -f 2 | tr -d ' ')
if [ "$SAFE_TO_BOOTSTRAP" == "1" ]; then
# Cluster is already bootstrapped, so start mariadbd without --wsrep-new-cluster
echo "Cluster is already bootstrapped. Starting mariadbd without --wsrep-new-cluster."
exec mariadbd "$@"
else
# Cluster is not bootstrapped, so run mariadbd with --wsrep-new-cluster
echo "Cluster is not bootstrapped. Bootstrapping the cluster."
exec mariadbd --wsrep-new-cluster "$@"
fi
else
# If grastate.dat doesn't exist, assume it's a new cluster and bootstrap it
echo "grastate.dat not found. Bootstrapping the cluster."
exec mariadbd --wsrep-new-cluster "$@"
fi
fi
@@ -0,0 +1,20 @@
server {
listen 80;
server_name _;
client_max_body_size 200M;
location /dsql-websocket {
proxy_pass http://172.72.0.36:7073;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location / {
proxy_pass http://172.72.0.35:7070;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
+16
View File
@@ -0,0 +1,16 @@
FROM node:20-bookworm
RUN apt-get update && apt-get install -y --no-install-recommends \
curl zip unzip ca-certificates docker.io rsync openssh-client \
zlib1g wget python3 python3-pip make build-essential \
mariadb-client xz-utils
RUN update-ca-certificates
RUN curl -fsSL https://bun.sh/install | bash -s "bun-v1.2.0"
ENV PATH="/root/.bun/bin:${PATH}"
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
@@ -0,0 +1,34 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import { _n } from "@/client-exports";
import sendData from "../../../utils/send-data";
import getQueue from "@/package-shared/functions/backend/queues/get-queue";
import { DSQL_DATASQUIREL_PROCESS_QUEUE } from "@/package-shared/types/dsql";
type Param = {
ws: ServerWebSocket<WebSocketData>;
};
export default async function checkQueue({ ws }: Param) {
try {
const user = ws.data.user;
const queue = (await getQueue({
userId: user.id,
single: true,
})) as DSQL_DATASQUIREL_PROCESS_QUEUE | undefined;
sendData(ws, {
event: "server:queue",
data: {
queue,
},
});
} catch (error: any) {
sendData(ws, {
event: "server:queue",
data: {
queue: undefined,
},
});
}
}
@@ -0,0 +1,28 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import { _n } from "@/client-exports";
import sendData from "../../../utils/send-data";
import { WebSocketDataType } from "@/types";
import deleteQueue from "@/package-shared/functions/backend/queues/delete-queue";
type Param = {
ws: ServerWebSocket<WebSocketData>;
data?: WebSocketDataType;
};
export default async function webSocketDeleteQueue({ ws, data }: Param) {
try {
const user = ws.data.user;
await deleteQueue({
queueId: _n(data?.data?.queue?.id),
userId: user.id,
});
await Bun.sleep(2000);
sendData(ws, {
event: "server:queue-deleted",
});
} catch (error: any) {}
}
@@ -0,0 +1,36 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import { _n } from "@/client-exports";
import sendData from "@WS/utils/send-data";
import addQueue from "@/package-shared/functions/backend/queues/add-queue";
type Param = {
ws: ServerWebSocket<WebSocketData>;
};
export default async function webSocketSendDummyQueue({ ws }: Param) {
try {
console.log("Sending Dummy Queue ...");
const user = ws.data.user;
const dummyQueue = await addQueue({
queue: {
job_type: "dummy",
user_id: user.id,
title: "Running Dummy Queue ...",
},
userId: user.id,
dummy: true,
});
await Bun.sleep(2000);
sendData(ws, {
event: "server:dev:queue",
});
} catch (error: any) {
sendData(ws, {
event: "server:dev:queue",
});
}
}
@@ -0,0 +1,19 @@
import { type ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
type Param = {
ws: ServerWebSocket<WebSocketData>;
};
export default async function socketClose({ ws }: Param) {
const user = ws.data.user;
console.log(`Web Closed by ${user.first_name}`);
const userSessionIndex = global.ACTIVE_USERS.findIndex(
(session) => session.id == user.id
);
if (userSessionIndex >= 0) {
global.ACTIVE_USERS.splice(userSessionIndex, 1);
}
}
@@ -0,0 +1,38 @@
import datasquirel from "@moduletrace/datasquirel";
import type { DATASQUIREL_LoggedInUser } from "@moduletrace/datasquirel/dist/package-shared/types";
type Param = {
req: Request;
debug?: boolean;
};
type Return = {
user: DATASQUIREL_LoggedInUser | null;
};
export default async function socketInit({
req,
debug,
}: Param): Promise<Return> {
const cookieString = req.headers.get("Cookie") || undefined;
if (debug) {
console.log("DEBUG:::socketInit:cookieString", cookieString);
}
if (!cookieString)
return {
user: null,
};
const user = datasquirel.user.userAuth({
cookieString,
database: process.env.DB_NAME || "",
});
if (debug) {
console.log("DEBUG:::socketInit:user", user);
}
return { user: user.payload };
}
@@ -0,0 +1,55 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import type { WebSocketDataType } from "@/types";
import { EJSON } from "@/client-exports";
import checkQueue from "./events/client-requests/check-queue";
import webSocketSendDummyQueue from "./events/client-requests/send-dummy-queue";
import webSocketDeleteQueue from "./events/client-requests/delete-queue";
type Param = {
ws: ServerWebSocket<WebSocketData>;
message: string | Buffer;
};
export type WebSocketMessageParam = {
ws: ServerWebSocket<WebSocketData>;
message?: string | Buffer;
data?: WebSocketDataType;
};
export default async function socketMessage({ ws, message }: Param) {
const user = ws.data.user;
const data = EJSON.parse(message.toString()) as
| WebSocketDataType
| undefined;
const websocketMessageParams: WebSocketMessageParam = {
ws,
data,
message,
};
switch (data?.event) {
/**
* Check Queue
*/
case "client:check-queue":
checkQueue({ ws });
break;
/**
* Send Dummy Queue
*/
case "client:dev:queue":
webSocketSendDummyQueue({ ws });
break;
/**
* Delete Queue
*/
case "client:delete-queue":
webSocketDeleteQueue({ ws, data });
break;
default:
break;
}
}
@@ -0,0 +1,12 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
type Param = {
ws: ServerWebSocket<WebSocketData>;
};
export default async function socketOpen({ ws }: Param) {
const user = ws.data.user;
console.log(`Web Socket Opened by ${user.first_name}`);
global.ACTIVE_USERS.push({ ...user, ws });
}
+57
View File
@@ -0,0 +1,57 @@
import { type ServerWebSocket, type Subprocess } from "bun";
import socketInit from "@WS/functions/socket-init";
import type { DATASQUIREL_LoggedInUser } from "@moduletrace/datasquirel/dist/package-shared/types";
import socketOpen from "@WS/functions/socket-open";
import socketClose from "@WS/functions/socket-close";
import socketMessage from "@WS/functions/socket-message";
import setupDSQLDb from "@/utils/setup-db";
import debugLog from "@/package-shared/utils/logging/debug-log";
setupDSQLDb();
export type WebSocketData = {
user: DATASQUIREL_LoggedInUser;
};
declare global {
var ACTIVE_USERS: (DATASQUIREL_LoggedInUser & {
ws: ServerWebSocket<WebSocketData>;
})[];
}
global.ACTIVE_USERS = [];
const server = Bun.serve<WebSocketData>({
async fetch(req, server) {
const { user } = await socketInit({ req });
if (!user?.logged_in_status) {
return new Response("Unauthorized!");
}
const success = server.upgrade(req, {
data: { user },
});
if (success) {
return undefined;
}
return new Response("Web Socket Connection Failed!");
},
websocket: {
async message(ws, message) {
socketMessage({ ws, message });
},
async open(ws) {
socketOpen({ ws });
},
async close(ws, code, message) {
socketClose({ ws });
},
idleTimeout: 600,
},
port: process.env.DSQL_WEBSOCKET_PORT,
});
console.log(`Websocket Listening on http://${server.hostname}:${server.port}`);
@@ -0,0 +1,50 @@
import grabCoderankSSHPrefix from "./grab-ssh-prefix";
import { execSync, type ExecSyncOptions } from "child_process";
type Param = {
filePath: string;
debug?: boolean;
server?: any;
options?: ExecSyncOptions;
};
export default function bunExecSSH({
filePath,
debug,
server,
options,
}: Param): string | undefined {
try {
let cmdPrefix = grabCoderankSSHPrefix();
let finalCmd = `${cmdPrefix}`;
if (server) {
finalCmd += ` ${server.username}@${server.ip}`;
}
finalCmd += ` bun < ${filePath}`;
if (debug) {
console.log("DEBUG:::", finalCmd);
}
const str = execSync(finalCmd, {
stdio: "pipe",
...options,
encoding: "utf-8",
});
if (debug) {
console.log("DEBUG:::", str);
}
return str.trim();
} catch (error: any) {
if (debug) {
console.log(`DEBUG::: Raw SSh Error: ${error.message}`);
}
return undefined;
}
}
@@ -0,0 +1,22 @@
import { Client, type ConnectConfig } from "ssh2";
type Param = {
config: ConnectConfig;
};
export default async function connectSSH({
config,
}: Param): Promise<Client | undefined> {
return new Promise((resolve) => {
const ssh = new Client();
ssh.on("ready", () => {
resolve(ssh);
}).connect(config);
ssh.on("error", (err) => {
console.log(`SHH connect ERROR: ${err.message}`);
resolve(undefined);
});
});
}
@@ -0,0 +1,70 @@
import grabCoderankSSHPrefix from "./grab-ssh-prefix";
import { exec, execSync, type ExecSyncOptions } from "child_process";
import debugLog from "@moduletrace/datasquirel/dist/package-shared/utils/logging/debug-log";
type Param = {
cmd: string;
debug?: boolean;
server?: any;
options?: ExecSyncOptions;
detached?: boolean;
};
export default function execRawSSH({
cmd,
debug,
server,
options,
detached,
}: Param): string | undefined {
function debugFn(log: any, label?: string) {
debugLog({
log: log,
label: label,
title: "execRawSSH",
addTime: true,
});
}
try {
let cmdPrefix = grabCoderankSSHPrefix();
let finalCmd = `${cmdPrefix}`;
if (server) {
finalCmd += ` ${server.username}@${server.ip}`;
}
finalCmd += ` << 'CDREXEC' \n\
${cmd}\n\
CDREXEC`;
if (debug) {
debugFn(finalCmd, "finalCmd");
}
const str = detached
? ""
: execSync(finalCmd, {
stdio: "pipe",
...options,
encoding: "utf-8",
});
if (detached) {
exec(finalCmd);
}
if (debug) {
debugFn(str, "commandResult");
}
return str.trim();
} catch (error: any) {
if (debug) {
debugFn(error.message, "Raw SSh Error");
}
return undefined;
}
}
@@ -0,0 +1,51 @@
import { Client } from "ssh2";
type Param = {
ssh: Client;
cmd: string;
debug?: boolean;
};
export default async function execSSH({
ssh,
cmd,
debug,
}: Param): Promise<string | undefined> {
if (!ssh) {
console.log(`No SSH object passed into execSSH function`);
return undefined;
}
return new Promise((resolve) => {
let finalData = "";
let stdErrData = "";
ssh.exec(cmd, (err, stream) => {
if (err) throw err;
stream
.on("close", (code?: number, signal?: string) => {
if (code || signal) {
resolve(undefined);
} else {
resolve(finalData);
}
})
.on("data", (data: any) => {
finalData += data;
})
.stderr.on("data", (data) => {
stdErrData += data;
})
.on("close", () => {
if (stdErrData.match(/./)) {
if (debug) {
console.log("SSH Error code:", stdErrData);
}
resolve(undefined);
}
});
});
});
}
@@ -0,0 +1,3 @@
export default function grabCoderankSSHPrefix() {
return `ssh -i ${process.env.CODERANK_APP_DIR}/ssh/coderank -o StrictHostKeyChecking=no -C -c aes128-ctr`;
}
@@ -0,0 +1,12 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import type { WebSocketDataType } from "@/types";
import datasquirel from "@moduletrace/datasquirel";
const EJSON = datasquirel.client.utils.EJSON;
export default function sendData(
ws: ServerWebSocket<WebSocketData>,
data: WebSocketDataType
) {
ws.send(String(EJSON.stringify(data)));
}
@@ -0,0 +1,19 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import type { WebSocketDataType } from "@/types";
import datasquirel from "@moduletrace/datasquirel";
const EJSON = datasquirel.client.utils.EJSON;
export default function sendError(
ws: ServerWebSocket<WebSocketData>,
message?: String
) {
ws.send(
String(
EJSON.stringify({
event: "server:error",
message: message,
} as WebSocketDataType)
)
);
}
@@ -0,0 +1,19 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "..";
import type { WebSocketDataType } from "@/types";
import datasquirel from "@moduletrace/datasquirel";
const EJSON = datasquirel.client.utils.EJSON;
export default function sendMessage(
ws: ServerWebSocket<WebSocketData>,
message: String
) {
ws.send(
String(
EJSON.stringify({
event: "server:message",
message: message,
} as WebSocketDataType)
)
);
}
@@ -0,0 +1,19 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import type { WebSocketDataType } from "@/types";
import datasquirel from "@moduletrace/datasquirel";
const EJSON = datasquirel.client.utils.EJSON;
export default function sendReady(
ws: ServerWebSocket<WebSocketData>,
message?: String
) {
ws.send(
String(
EJSON.stringify({
event: "server:ready",
message: message,
} as WebSocketDataType)
)
);
}
@@ -0,0 +1,19 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import type { WebSocketDataType } from "@/types";
import datasquirel from "@moduletrace/datasquirel";
const EJSON = datasquirel.client.utils.EJSON;
export default function sendSuccess(
ws: ServerWebSocket<WebSocketData>,
message: String
) {
ws.send(
String(
EJSON.stringify({
event: "server:success",
message: message,
} as WebSocketDataType)
)
);
}
@@ -0,0 +1,19 @@
import type { ServerWebSocket } from "bun";
import type { WebSocketData } from "@WS/.";
import type { WebSocketDataType } from "@/types";
import datasquirel from "@moduletrace/datasquirel";
const EJSON = datasquirel.client.utils.EJSON;
export default function sendUpdate(
ws: ServerWebSocket<WebSocketData>,
message: String
) {
ws.send(
String(
EJSON.stringify({
event: "server:update",
message: message,
} as WebSocketDataType)
)
);
}
+14
View File
@@ -0,0 +1,14 @@
#!/bin/bash
cd /app
if [[ -z "$NODE_ENV" ]]; then
echo "NODE_ENV is not set. Defaulting to development."
NODE_ENV="development"
fi
if [[ "$NODE_ENV" == "production" ]]; then
bun websocket:start
else
bun websocket:dev
fi