From 75015f82eaa3fbc1ab1d10b2a71cbef6738e3bb6 Mon Sep 17 00:00:00 2001 From: Benson Wong Date: Mon, 16 Jun 2025 15:32:09 -0700 Subject: [PATCH] fix bug caused by macro replacement order (#166) User defined macros should be applied before checking for ${PORT} constraint in model.cmd and model.proxy. --- proxy/config.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/proxy/config.go b/proxy/config.go index 9d564f6..e4b0a50 100644 --- a/proxy/config.go +++ b/proxy/config.go @@ -205,11 +205,6 @@ func LoadConfigFromReader(r io.Reader) (Config, error) { for _, modelId := range modelIds { modelConfig := config.Models[modelId] - // enforce ${PORT} used in both cmd and proxy - if !strings.Contains(modelConfig.Cmd, "${PORT}") && strings.Contains(modelConfig.Proxy, "${PORT}") { - return Config{}, fmt.Errorf("model %s: proxy uses ${PORT} but cmd does not - ${PORT} is only available when used in cmd", modelId) - } - // go through model config fields: cmd, cmdStop, proxy, checkEndPoint and replace macros with macro values for macroName, macroValue := range config.Macros { macroSlug := fmt.Sprintf("${%s}", macroName) @@ -219,6 +214,11 @@ func LoadConfigFromReader(r io.Reader) (Config, error) { modelConfig.CheckEndpoint = strings.ReplaceAll(modelConfig.CheckEndpoint, macroSlug, macroValue) } + // enforce ${PORT} used in both cmd and proxy + if !strings.Contains(modelConfig.Cmd, "${PORT}") && strings.Contains(modelConfig.Proxy, "${PORT}") { + return Config{}, fmt.Errorf("model %s: proxy uses ${PORT} but cmd does not - ${PORT} is only available when used in cmd", modelId) + } + // only iterate over models that use ${PORT} to keep port numbers from increasing unnecessarily if strings.Contains(modelConfig.Cmd, "${PORT}") || strings.Contains(modelConfig.Proxy, "${PORT}") || strings.Contains(modelConfig.CmdStop, "${PORT}") { nextPortStr := strconv.Itoa(nextPort)