Skip to content

Commit 0fc9b9b

Browse files
committed
feat: make host configurable via env var
1 parent 28d13c2 commit 0fc9b9b

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

docs/protocol-api.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ This document provides the TCP API protocol reference for developers who want to
66

77
The BalatroBot API establishes a TCP socket connection to communicate with the Balatro game through the BalatroBot Lua mod. The protocol uses a simple JSON request-response model for synchronous communication.
88

9-
- **Host:** `127.0.0.1` (localhost)
10-
- **Port:** `12346` (default)
9+
- **Host:** `127.0.0.1` (default, configurable via `BALATROBOT_HOST`)
10+
- **Port:** `12346` (default, configurable via `BALATROBOT_PORT`)
1111
- **Message Format:** JSON
1212

13+
### Configuration
14+
15+
The API server can be configured using environment variables:
16+
17+
- `BALATROBOT_HOST`: The network interface to bind to (default: `127.0.0.1`)
18+
- `127.0.0.1`: Localhost only (secure for local development)
19+
- `*` or `0.0.0.0`: All network interfaces (required for Docker or remote access)
20+
- `BALATROBOT_PORT`: The TCP port to listen on (default: `12346`)
21+
- `BALATROBOT_HEADLESS`: Enable headless mode (`1` to enable)
22+
- `BALATROBOT_FAST`: Enable fast mode for faster gameplay (`1` to enable)
23+
1324
### Communication Sequence
1425

1526
The typical interaction follows a game loop where clients continuously query the game state, analyze it, and send appropriate actions:

src/lua/api.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,17 @@ function API.update(_)
7575
end
7676

7777
API.server_socket:settimeout(SOCKET_TIMEOUT)
78+
local host = G.BALATROBOT_HOST or "127.0.0.1"
7879
local port = G.BALATROBOT_PORT
79-
local success, err = API.server_socket:bind("127.0.0.1", tonumber(port) or 12346)
80+
local success, err = API.server_socket:bind(host, tonumber(port) or 12346)
8081
if not success then
8182
sendErrorMessage("Failed to bind to port " .. port .. ": " .. tostring(err), "API")
8283
API.server_socket = nil
8384
return
8485
end
8586

8687
API.server_socket:listen(1)
87-
sendDebugMessage("TCP server socket created on port " .. port, "API")
88+
sendDebugMessage("TCP server socket created on " .. host .. ":" .. port, "API")
8889
end
8990

9091
-- Accept client connection if we don't have one

src/lua/settings.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local headless = os.getenv("BALATROBOT_HEADLESS") == "1"
33
local fast = os.getenv("BALATROBOT_FAST") == "1"
44
local audio = os.getenv("BALATROBOT_AUDIO") == "1"
55
local port = os.getenv("BALATROBOT_PORT")
6+
local host = os.getenv("BALATROBOT_HOST")
67

78
SETTINGS = {}
89

@@ -184,6 +185,7 @@ end
184185
-- Main setup function
185186
SETTINGS.setup = function()
186187
G.BALATROBOT_PORT = port or "12346"
188+
G.BALATROBOT_HOST = host or "127.0.0.1"
187189

188190
-- Apply Love2D performance patches
189191
apply_love_patches()

0 commit comments

Comments
 (0)