Skip to content

Commit

Permalink
Allow to specify the server Timezone at egg level (#21)
Browse files Browse the repository at this point in the history
* Allow to specify the server Timezone at egg level
  • Loading branch information
QuintenQVD0 authored Jun 2, 2024
1 parent 05ec511 commit d411477
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"
"sync"
"regexp"
"time"

"emperror.dev/errors"
"github.com/apex/log"
Expand Down Expand Up @@ -146,6 +147,25 @@ func (s *Server) Context() context.Context {
return s.ctx
}

// DetermineServerTimezone checks the envvars for a non-empty SERVER_TIMEZONE key,
// validates if it's a valid timezone, and returns it. If not, returns the defaultTimezone.
func DetermineServerTimezone(envvars map[string]interface{}, defaultTimezone string) string {
// Check if SERVER_TIMEZONE exists in envvars and is non-empty
timezone, ok := envvars["SERVER_TIMEZONE"].(string)
if ok && timezone != "" {
// Validate the timezone
_, err := time.LoadLocation(timezone)
if err == nil {
// Valid timezone, return it
return timezone
}
// Invalid timezone, fall through to return defaultTimezone
}

// Return the defaultTimezone if SERVER_TIMEZONE is not set, empty, or invalid
return defaultTimezone
}

// parseInvocation parses the start command in the same way we already do in the entrypoint
// We can use this to set the container command with all variables replaced.
func parseInvocation(invocation string, envvars map[string]interface{}, memory int64, port int, ip string) (parsed string) {
Expand Down Expand Up @@ -191,8 +211,7 @@ func parseInvocation(invocation string, envvars map[string]interface{}, memory i
// server instance.
func (s *Server) GetEnvironmentVariables() []string {
out := []string{
// TODO: allow this to be overridden by the user.
fmt.Sprintf("TZ=%s", config.Get().System.Timezone),
fmt.Sprintf("TZ=%s", DetermineServerTimezone(s.Config().EnvVars, config.Get().System.Timezone)),
fmt.Sprintf("STARTUP=%s", parseInvocation(s.Config().Invocation, s.Config().EnvVars, s.MemoryLimit(), s.Config().Allocations.DefaultMapping.Port, s.Config().Allocations.DefaultMapping.Ip)),
fmt.Sprintf("SERVER_MEMORY=%d", s.MemoryLimit()),
fmt.Sprintf("SERVER_IP=%s", s.Config().Allocations.DefaultMapping.Ip),
Expand Down

0 comments on commit d411477

Please sign in to comment.