From 26e2154c64f6625909ff9ecbbdc05afd9de5062a Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 21 Sep 2024 17:22:17 +0800 Subject: [PATCH] fixed startup crash for file provider --- README.md | 11 +++++++---- src/common/constants.go | 6 ------ src/common/env.go | 17 +++++++++++++++-- src/main.go | 2 -- src/models/proxy_entry.go | 4 ++++ src/watcher/file_watcher.go | 7 +++---- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9b24ce9..8e8f57b 100755 --- a/README.md +++ b/README.md @@ -74,10 +74,13 @@ A lightweight, easy-to-use, and [performant](docs/benchmark_result.md) reverse p ### Environment variables -| Environment Variable | Description | Default | Values | -| ------------------------------ | ------------------------- | ------- | ------- | -| `GOPROXY_NO_SCHEMA_VALIDATION` | disable schema validation | `false` | boolean | -| `GOPROXY_DEBUG` | enable debug behaviors | `false` | boolean | +| Environment Variable | Description | Default | Values | +| ------------------------------ | ----------------------------- | ------- | ------- | +| `GOPROXY_NO_SCHEMA_VALIDATION` | disable schema validation | `false` | boolean | +| `GOPROXY_DEBUG` | enable debug behaviors | `false` | boolean | +| `GOPROXY_HTTP_PORT` | http server port | `80` | integer | +| `GOPROXY_HTTPS_PORT` | http server port (if enabled) | `443` | integer | +| `GOPROXY_API_PORT` | api server port | `8888` | integer | ### Use JSON Schema in VSCode diff --git a/src/common/constants.go b/src/common/constants.go index 3308f7d..d871988 100644 --- a/src/common/constants.go +++ b/src/common/constants.go @@ -37,12 +37,6 @@ const ( const DockerHostFromEnv = "$DOCKER_HOST" -const ( - ProxyHTTPPort = ":80" - ProxyHTTPSPort = ":443" - APIHTTPPort = ":8888" -) - var WellKnownHTTPPorts = map[uint16]bool{ 80: true, 8000: true, diff --git a/src/common/env.go b/src/common/env.go index 91cad55..9f793b2 100644 --- a/src/common/env.go +++ b/src/common/env.go @@ -6,9 +6,22 @@ import ( U "github.com/yusing/go-proxy/utils" ) -var NoSchemaValidation = getEnvBool("GOPROXY_NO_SCHEMA_VALIDATION") -var IsDebug = getEnvBool("GOPROXY_DEBUG") +var ( + NoSchemaValidation = getEnvBool("GOPROXY_NO_SCHEMA_VALIDATION") + IsDebug = getEnvBool("GOPROXY_DEBUG") + ProxyHTTPPort = ":" + getEnv("GOPROXY_HTTP_PORT", "80") + ProxyHTTPSPort = ":" + getEnv("GOPROXY_HTTPS_PORT", "443") + APIHTTPPort = ":" + getEnv("GOPROXY_API_PORT", "8888") +) func getEnvBool(key string) bool { return U.ParseBool(os.Getenv(key)) } + +func getEnv(key string, defaultValue string) string { + value, ok := os.LookupEnv(key) + if !ok { + value = defaultValue + } + return value +} diff --git a/src/main.go b/src/main.go index 60245ee..e17485c 100755 --- a/src/main.go +++ b/src/main.go @@ -23,7 +23,6 @@ import ( R "github.com/yusing/go-proxy/route" "github.com/yusing/go-proxy/server" F "github.com/yusing/go-proxy/utils/functional" - W "github.com/yusing/go-proxy/watcher" ) func main() { @@ -92,7 +91,6 @@ func main() { l.Warn(err) } - W.InitFileWatcherHelper() cfg.WatchChanges() onShutdown.Add(docker.CloseAllClients) diff --git a/src/models/proxy_entry.go b/src/models/proxy_entry.go index dabb971..8d5e5f5 100644 --- a/src/models/proxy_entry.go +++ b/src/models/proxy_entry.go @@ -30,6 +30,10 @@ type ( var NewProxyEntries = F.NewMapOf[string, *ProxyEntry] func (e *ProxyEntry) SetDefaults() { + if e.ProxyProperties == nil { + e.ProxyProperties = &D.ProxyProperties{} + } + if e.Scheme == "" { switch { case strings.ContainsRune(e.Port, ':'): diff --git a/src/watcher/file_watcher.go b/src/watcher/file_watcher.go index f4dbc70..2ce7939 100644 --- a/src/watcher/file_watcher.go +++ b/src/watcher/file_watcher.go @@ -20,11 +20,10 @@ func NewFileWatcher(filename string) Watcher { } func (f *fileWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.NestedError) { + if fwHelper == nil { + fwHelper = newFileWatcherHelper(common.ConfigBasePath) + } return fwHelper.Add(ctx, f) } -func InitFileWatcherHelper() { - fwHelper = newFileWatcherHelper(common.ConfigBasePath) -} - var fwHelper *fileWatcherHelper