Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify the server Timezone at egg level #21

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading