From 31d7168aac48f961a127a385c9a8bdb970c6119a Mon Sep 17 00:00:00 2001 From: James Read Date: Mon, 15 Jul 2024 15:28:53 +0100 Subject: [PATCH] feature: #350 Environment variable PORT can be used to override the default 1337, and internal services will calculate their port based on that (#358) --- cmd/OliveTin/main.go | 29 ++++++++++++++++++++++++++++- internal/config/config.go | 21 +++++++++++++++------ internal/grpcapi/grpcApi.go | 4 ++++ internal/httpservers/restapi.go | 2 +- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/cmd/OliveTin/main.go b/cmd/OliveTin/main.go index 4ca8981..b1ccf4a 100644 --- a/cmd/OliveTin/main.go +++ b/cmd/OliveTin/main.go @@ -20,6 +20,7 @@ import ( config "github.com/OliveTin/OliveTin/internal/config" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" + "strconv" "os" ) @@ -76,6 +77,32 @@ func initCliFlags() string { return configDir } +func getBasePort() int { + var err error + + defaultPort := 1337 + basePort := defaultPort + + envPort := os.Getenv("PORT") + + if envPort != "" { + basePort, err = strconv.Atoi(os.Getenv("PORT")) + + if err != nil { + log.Errorf("Error converting port to int. %s", err) + os.Exit(1) + } + } + + if defaultPort != basePort { + log.WithFields(log.Fields{ + "basePort": basePort, + }).Debug("Base port") + } + + return basePort +} + func initViperConfig(configDir string) { viper.AutomaticEnv() viper.SetConfigName("config.yaml") @@ -89,7 +116,7 @@ func initViperConfig(configDir string) { os.Exit(1) } - cfg = config.DefaultConfig() + cfg = config.DefaultConfigWithBasePort(getBasePort()) viper.WatchConfig() viper.OnConfigChange(func(e fsnotify.Event) { diff --git a/internal/config/config.go b/internal/config/config.go index 9a33000..59350aa 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,5 +1,9 @@ package config +import ( + "fmt" +) + // Action represents the core functionality of OliveTin - commands that show up // as buttons in the UI. type Action struct { @@ -148,19 +152,18 @@ type DashboardComponent struct { Contents []DashboardComponent } -// DefaultConfig gets a new Config structure with sensible default values. func DefaultConfig() *Config { + return DefaultConfigWithBasePort(1337) +} + +// DefaultConfig gets a new Config structure with sensible default values. +func DefaultConfigWithBasePort(basePort int) *Config { config := Config{} config.UseSingleHTTPFrontend = true config.PageTitle = "OliveTin" config.ShowFooter = true config.ShowNavigation = true config.ShowNewVersions = true - config.ListenAddressSingleHTTPFrontend = "0.0.0.0:1337" - config.ListenAddressRestActions = "localhost:1338" - config.ListenAddressGrpcActions = "localhost:1339" - config.ListenAddressWebUI = "localhost:1340" - config.ListenAddressPrometheus = "localhost:1341" config.ExternalRestAddress = "." config.LogLevel = "INFO" config.CheckForUpdates = false @@ -183,5 +186,11 @@ func DefaultConfig() *Config { config.DefaultIconForDirectories = "📁" config.DefaultIconForBack = "«" + config.ListenAddressSingleHTTPFrontend = fmt.Sprintf("0.0.0.0:%d", basePort) + config.ListenAddressRestActions = fmt.Sprintf("localhost:%d", basePort+1) + config.ListenAddressGrpcActions = fmt.Sprintf("localhost:%d", basePort+2) + config.ListenAddressWebUI = fmt.Sprintf("localhost:%d", basePort+3) + config.ListenAddressPrometheus = fmt.Sprintf("localhost:%d", basePort+4) + return &config } diff --git a/internal/grpcapi/grpcApi.go b/internal/grpcapi/grpcApi.go index 94139ab..91806b3 100644 --- a/internal/grpcapi/grpcApi.go +++ b/internal/grpcapi/grpcApi.go @@ -382,6 +382,10 @@ func (api *oliveTinAPI) GetReadyz(ctx ctx.Context, req *pb.GetReadyzRequest) (*p func Start(globalConfig *config.Config, ex *executor.Executor) { cfg = globalConfig + log.WithFields(log.Fields{ + "address": cfg.ListenAddressGrpcActions, + }).Info("Starting gRPC API") + lis, err := net.Listen("tcp", cfg.ListenAddressGrpcActions) if err != nil { diff --git a/internal/httpservers/restapi.go b/internal/httpservers/restapi.go index f3a8d5f..616f8c9 100644 --- a/internal/httpservers/restapi.go +++ b/internal/httpservers/restapi.go @@ -75,7 +75,7 @@ func startRestAPIServer(globalConfig *config.Config) error { cfg = globalConfig log.WithFields(log.Fields{ - "address": cfg.ListenAddressGrpcActions, + "address": cfg.ListenAddressRestActions, }).Info("Starting REST API") mux := newMux()