feature: #350 Environment variable PORT can be used to override the default 1337, and internal services will calculate their port based on that (#358)

This commit is contained in:
James Read 2024-07-15 15:28:53 +01:00 committed by GitHub
parent 09016e1d5f
commit 31d7168aac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 8 deletions

View File

@ -20,6 +20,7 @@ import (
config "github.com/OliveTin/OliveTin/internal/config" config "github.com/OliveTin/OliveTin/internal/config"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
"strconv"
"os" "os"
) )
@ -76,6 +77,32 @@ func initCliFlags() string {
return configDir 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) { func initViperConfig(configDir string) {
viper.AutomaticEnv() viper.AutomaticEnv()
viper.SetConfigName("config.yaml") viper.SetConfigName("config.yaml")
@ -89,7 +116,7 @@ func initViperConfig(configDir string) {
os.Exit(1) os.Exit(1)
} }
cfg = config.DefaultConfig() cfg = config.DefaultConfigWithBasePort(getBasePort())
viper.WatchConfig() viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) { viper.OnConfigChange(func(e fsnotify.Event) {

View File

@ -1,5 +1,9 @@
package config package config
import (
"fmt"
)
// Action represents the core functionality of OliveTin - commands that show up // Action represents the core functionality of OliveTin - commands that show up
// as buttons in the UI. // as buttons in the UI.
type Action struct { type Action struct {
@ -148,19 +152,18 @@ type DashboardComponent struct {
Contents []DashboardComponent Contents []DashboardComponent
} }
// DefaultConfig gets a new Config structure with sensible default values.
func DefaultConfig() *Config { func DefaultConfig() *Config {
return DefaultConfigWithBasePort(1337)
}
// DefaultConfig gets a new Config structure with sensible default values.
func DefaultConfigWithBasePort(basePort int) *Config {
config := Config{} config := Config{}
config.UseSingleHTTPFrontend = true config.UseSingleHTTPFrontend = true
config.PageTitle = "OliveTin" config.PageTitle = "OliveTin"
config.ShowFooter = true config.ShowFooter = true
config.ShowNavigation = true config.ShowNavigation = true
config.ShowNewVersions = 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.ExternalRestAddress = "."
config.LogLevel = "INFO" config.LogLevel = "INFO"
config.CheckForUpdates = false config.CheckForUpdates = false
@ -183,5 +186,11 @@ func DefaultConfig() *Config {
config.DefaultIconForDirectories = "&#128193" config.DefaultIconForDirectories = "&#128193"
config.DefaultIconForBack = "«" 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 return &config
} }

View File

@ -382,6 +382,10 @@ func (api *oliveTinAPI) GetReadyz(ctx ctx.Context, req *pb.GetReadyzRequest) (*p
func Start(globalConfig *config.Config, ex *executor.Executor) { func Start(globalConfig *config.Config, ex *executor.Executor) {
cfg = globalConfig cfg = globalConfig
log.WithFields(log.Fields{
"address": cfg.ListenAddressGrpcActions,
}).Info("Starting gRPC API")
lis, err := net.Listen("tcp", cfg.ListenAddressGrpcActions) lis, err := net.Listen("tcp", cfg.ListenAddressGrpcActions)
if err != nil { if err != nil {

View File

@ -75,7 +75,7 @@ func startRestAPIServer(globalConfig *config.Config) error {
cfg = globalConfig cfg = globalConfig
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"address": cfg.ListenAddressGrpcActions, "address": cfg.ListenAddressRestActions,
}).Info("Starting REST API") }).Info("Starting REST API")
mux := newMux() mux := newMux()