From 5c3a013cd1bd07460394a74cee4d2fa140fa7498 Mon Sep 17 00:00:00 2001 From: "vuonglv(Andy)" <46917325+vuonglv1612@users.noreply.github.com> Date: Mon, 8 Dec 2025 22:16:39 +0700 Subject: [PATCH] feat(config): add configurable host binding for server (#454) * feat(config): add configurable host binding for server --- config.example.yaml | 4 ++++ internal/api/server.go | 2 +- internal/config/config.go | 4 ++++ sdk/cliproxy/service.go | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 0f8679aa..dfd7454b 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,3 +1,7 @@ +# Server host/interface to bind to. Default is empty ("") to bind all interfaces (IPv4 + IPv6). +# Use "127.0.0.1" or "localhost" to restrict access to local machine only. +host: "" + # Server port port: 8317 diff --git a/internal/api/server.go b/internal/api/server.go index b65185a7..79dcf12a 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -300,7 +300,7 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk // Create HTTP server s.server = &http.Server{ - Addr: fmt.Sprintf(":%d", cfg.Port), + Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), Handler: engine, } diff --git a/internal/config/config.go b/internal/config/config.go index f6d1eb73..5af74b1b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,6 +20,9 @@ import ( // Config represents the application's configuration, loaded from a YAML file. type Config struct { config.SDKConfig `yaml:",inline"` + // Host is the network host/interface on which the API server will bind. + // Default is empty ("") to bind all interfaces (IPv4 + IPv6). Use "127.0.0.1" or "localhost" for local-only access. + Host string `yaml:"host" json:"-"` // Port is the network port on which the API server will listen. Port int `yaml:"port" json:"-"` @@ -320,6 +323,7 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) { // Unmarshal the YAML data into the Config struct. var cfg Config // Set defaults before unmarshal so that absent keys keep defaults. + cfg.Host = "" // Default empty: binds to all interfaces (IPv4 + IPv6) cfg.LoggingToFile = false cfg.UsageStatisticsEnabled = false cfg.DisableCooling = false diff --git a/sdk/cliproxy/service.go b/sdk/cliproxy/service.go index 8b9a6639..13d647dd 100644 --- a/sdk/cliproxy/service.go +++ b/sdk/cliproxy/service.go @@ -498,7 +498,7 @@ func (s *Service) Run(ctx context.Context) error { }() time.Sleep(100 * time.Millisecond) - fmt.Printf("API server started successfully on: %d\n", s.cfg.Port) + fmt.Printf("API server started successfully on: %s:%d\n", s.cfg.Host, s.cfg.Port) if s.hooks.OnAfterStart != nil { s.hooks.OnAfterStart(s)