From b72826bc46b890ebe58fd822e9b23005e933a0e7 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 22 Mar 2026 16:07:55 -0400 Subject: [PATCH] feat: implement dynamic configuration loading for server and WebSocket URLs --- webbridge.lua | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/webbridge.lua b/webbridge.lua index 6ec4d81..6e09ef5 100644 --- a/webbridge.lua +++ b/webbridge.lua @@ -3,13 +3,38 @@ -- Forwards turtle modem messages to server and server commands to turtles. -- Falls back to HTTP polling if WebSocket is unavailable. -local SERVER_URL = "http://beta:4200" -local WS_URL = "ws://beta:4200/ws/bridge" +local _baseDir = fs.getDir(shell.getRunningProgram()) +local function _path(rel) return fs.combine(_baseDir, rel) end + +-- Default config (overridable via .webbridge_config) +local SERVER_URL = "http://localhost:4200" +local WS_URL = "ws://localhost:4200/ws/bridge" local CHANNEL_RECEIVE = 101 local STATUS_CHANNEL = 102 local COMMAND_CHANNEL = 100 local POCKET_CHANNEL = 103 +-- Load config from file if present +local CONFIG_FILE = _path(".webbridge_config") +if fs.exists(CONFIG_FILE) then + local f = fs.open(CONFIG_FILE, "r") + local data = f.readAll() + f.close() + local ok, cfg = pcall(textutils.unserialiseJSON, data) + if ok and cfg then + if cfg.serverUrl then + SERVER_URL = cfg.serverUrl + WS_URL = cfg.serverUrl:gsub("^http", "ws") .. "/ws/bridge" + end + if cfg.wsUrl then WS_URL = cfg.wsUrl end + if cfg.channelReceive then CHANNEL_RECEIVE = cfg.channelReceive end + if cfg.statusChannel then STATUS_CHANNEL = cfg.statusChannel end + if cfg.commandChannel then COMMAND_CHANNEL = cfg.commandChannel end + if cfg.pocketChannel then POCKET_CHANNEL = cfg.pocketChannel end + print("[CONFIG] Loaded from " .. CONFIG_FILE) + end +end + -- Find peripherals local modem = peripheral.find("modem") local monitor = peripheral.find("monitor")