feat: implement dynamic configuration loading for server and WebSocket URLs

This commit is contained in:
MayaTheShy
2026-03-22 16:07:55 -04:00
parent 459664825c
commit b72826bc46

View File

@@ -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")