Skip to content

Commit

Permalink
Merge pull request #3 from parkervcp/pasre_start_command
Browse files Browse the repository at this point in the history
Parse start command in wings
  • Loading branch information
iamkubi authored Apr 11, 2024
2 parents cccf466 + db74fe4 commit 95b0847
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -143,13 +144,33 @@ func (s *Server) Context() context.Context {
return s.ctx
}

// 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) {
// replace "{{" and "}}" with "${" and "}" respectively
invocation = strings.ReplaceAll(invocation, "{{", "${")
invocation = strings.ReplaceAll(invocation, "}}", "}")

// replaces ${varname} with varval
for varname, varval := range envvars {
invocation = strings.ReplaceAll(invocation, fmt.Sprintf("${%s}", varname), fmt.Sprint(varval))
}

// replace the defaults with their configured values.
invocation = strings.ReplaceAll(invocation, "${SERVER_PORT}", strconv.Itoa(port))
invocation = strings.ReplaceAll(invocation, "${SERVER_MEMORY}", strconv.Itoa(int(memory)))
invocation = strings.ReplaceAll(invocation, "${SERVER_IP}", ip)

return invocation
}

// Returns all of the environment variables that should be assigned to a running
// 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("STARTUP=%s", s.Config().Invocation),
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),
fmt.Sprintf("SERVER_PORT=%d", s.Config().Allocations.DefaultMapping.Port),
Expand Down

0 comments on commit 95b0847

Please sign in to comment.