Skip to content

Commit

Permalink
fix: handle LISTEN_ADDRESS env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
leonsteinhaeuser committed Nov 21, 2024
1 parent 0a6d958 commit c4d7edf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
13 changes: 11 additions & 2 deletions services/number/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ import (
"os"
)

var (
envListenAddress = os.Getenv("LISTEN_ADDRESS")
)

func init() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}))
slog.SetDefault(logger)

if envListenAddress == "" {
slog.Warn("LISTEN_ADDRESS is not set, using default value", "default", ":8081")
envListenAddress = ":8081"
}
}

func main() {
Expand All @@ -22,8 +31,8 @@ func main() {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("GET /number", logMiddleware(getNumber))
slog.Info("starting server on port 8081")
http.ListenAndServe(":8081", nil)
slog.Info("starting server on: " + envListenAddress)
http.ListenAndServe(envListenAddress, nil)
}

func logMiddleware(next http.HandlerFunc) http.HandlerFunc {
Expand Down
5 changes: 5 additions & 0 deletions services/status/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const (
)

func init() {
if envListenAddress == "" {
slog.Warn("LISTEN_ADDRESS is not set, using default value", "default", ":8082")
envListenAddress = ":8082"
}

// Parse the envExternalServicesToWatch and populate the externalServicesToWatch map.
// If the envExternalServicesToWatch is empty, then we should watch the default services.
if envExternalServicesToWatch == "" {
Expand Down
10 changes: 8 additions & 2 deletions services/view/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var (
envNumberServiceURL = os.Getenv("NUMBER_SERVICE_URL")
envStatusServiceURL = os.Getenv("STATUS_SERVICE_URL")
envListenAddress = os.Getenv("LISTEN_ADDRESS")

//go:embed view.html
websiteHTML string
Expand All @@ -24,6 +25,11 @@ func init() {
Level: slog.LevelDebug,
}))
slog.SetDefault(logger)

if envListenAddress == "" {
slog.Warn("LISTEN_ADDRESS is not set, using default value", "default", ":8080")
envListenAddress = ":8080"
}
}

func main() {
Expand All @@ -33,8 +39,8 @@ func main() {
})
http.HandleFunc("GET /api/v1/status", proxyEndpoint)
http.HandleFunc("GET /", logMiddleware(getMain))
slog.Info("starting server on port 8080")
http.ListenAndServe(":8080", nil)
slog.Info("starting server on port: " + envListenAddress)
http.ListenAndServe(envListenAddress, nil)
}

func proxyEndpoint(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit c4d7edf

Please sign in to comment.