config: resolve MCP HTTP address

Amolith created

Change summary

internal/appconfig/config.go      | 13 ++++++
internal/appconfig/config_test.go | 61 +++++++++++++++++++++++++++++++++
2 files changed, 73 insertions(+), 1 deletion(-)

Detailed changes

internal/appconfig/config.go 🔗

@@ -19,7 +19,10 @@ import (
 	"git.secluded.site/cooked-mcp/internal/configvalue"
 )
 
-const defaultBaseURL = "https://cooked.wiki"
+const (
+	defaultBaseURL  = "https://cooked.wiki"
+	defaultHTTPAddr = "127.0.0.1:8123"
+)
 
 var userHomeDir = os.UserHomeDir
 
@@ -89,11 +92,19 @@ func Load(ctx context.Context, path string, resolver *configvalue.Resolver) (Con
 	if err != nil {
 		return Config{}, err
 	}
+	httpAddr := defaultHTTPAddr
+	if fileConfig.MCP.HTTPAddr != "" {
+		httpAddr = fileConfig.MCP.HTTPAddr
+	}
+	if envHTTPAddr := os.Getenv("COOKED_MCP_HTTP_ADDR"); envHTTPAddr != "" {
+		httpAddr = envHTTPAddr
+	}
 
 	return Config{
 		Username: username,
 		Password: password,
 		BaseURL:  baseURL,
+		HTTPAddr: httpAddr,
 	}, nil
 }
 

internal/appconfig/config_test.go 🔗

@@ -59,6 +59,67 @@ password = "toml-password"
 	}
 }
 
+func TestLoadDefaultsHTTPAddr(t *testing.T) {
+	configPath := filepath.Join(t.TempDir(), "config.toml")
+	writeConfig(t, configPath, `[user]
+name = "toml-user"
+password = "toml-password"
+`)
+
+	got, err := Load(context.Background(), configPath, configvalue.NewResolver())
+	if err != nil {
+		t.Fatalf("Load() error = %v", err)
+	}
+
+	if got.HTTPAddr != "127.0.0.1:8123" {
+		t.Fatalf("HTTPAddr = %q, want default loopback address", got.HTTPAddr)
+	}
+}
+
+// server.CONFIG.8-2
+func TestLoadACIDServerConfig8_2LoadsTOMLHTTPAddr(t *testing.T) {
+	configPath := filepath.Join(t.TempDir(), "config.toml")
+	writeConfig(t, configPath, `[user]
+name = "toml-user"
+password = "toml-password"
+
+[mcp]
+http_addr = "127.0.0.1:9000"
+`)
+
+	got, err := Load(context.Background(), configPath, configvalue.NewResolver())
+	if err != nil {
+		t.Fatalf("Load() error = %v", err)
+	}
+
+	if got.HTTPAddr != "127.0.0.1:9000" {
+		t.Fatalf("HTTPAddr = %q, want TOML address", got.HTTPAddr)
+	}
+}
+
+// server.CONFIG.3 server.CONFIG.8-1
+func TestLoadACIDServerConfig3And8_1EnvironmentOverridesTOMLHTTPAddr(t *testing.T) {
+	t.Setenv("COOKED_MCP_HTTP_ADDR", "127.0.0.1:9001")
+
+	configPath := filepath.Join(t.TempDir(), "config.toml")
+	writeConfig(t, configPath, `[user]
+name = "toml-user"
+password = "toml-password"
+
+[mcp]
+http_addr = "127.0.0.1:9000"
+`)
+
+	got, err := Load(context.Background(), configPath, configvalue.NewResolver())
+	if err != nil {
+		t.Fatalf("Load() error = %v", err)
+	}
+
+	if got.HTTPAddr != "127.0.0.1:9001" {
+		t.Fatalf("HTTPAddr = %q, want environment address", got.HTTPAddr)
+	}
+}
+
 func TestLoadRejectsMissingExplicitConfigFile(t *testing.T) {
 	_, err := Load(context.Background(), filepath.Join(t.TempDir(), "missing.toml"), configvalue.NewResolver())
 	if err == nil {