Skip to content

Commit

Permalink
Parse ENV vars for run config
Browse files Browse the repository at this point in the history
Previously we were translating them in the `Dockerfile`, but that was
requiring running the command in a shell, which interferes with signals.
Also it means it only works when running in Docker.

Instead, we can check the environment variables when setting up the
default values.

We allow them to be set with or without a prefix, to avoid name clashes.
For example, `--http-port` can be set in the environment as either
`HTTP_PORT` or `MPROXY_HTTP_PORT`.
  • Loading branch information
kevinmcconnell committed Mar 25, 2024
1 parent 4269c8e commit 1982299
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
9 changes: 2 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ RUN make
FROM ubuntu as base
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /app/bin/mproxy /usr/local/bin/
EXPOSE 80 443

ENV HTTP_PORT=80
ENV HTTPS_PORT=443
ENV DEBUG=false

EXPOSE $HTTP_PORT $HTTPS_PORT

CMD mproxy run --http-port=${HTTP_PORT} --https-port=${HTTPS_PORT} --debug=${DEBUG}
CMD [ "mproxy", "run" ]
6 changes: 3 additions & 3 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func newRunCommand() *runCommand {
RunE: runCommand.run,
}

runCommand.cmd.Flags().BoolVar(&runCommand.debugLogsEnabled, "debug", false, "Include debugging logs")
runCommand.cmd.Flags().IntVar(&globalConfig.HttpPort, "http-port", server.DefaultHttpPort, "Port to serve HTTP traffic on")
runCommand.cmd.Flags().IntVar(&globalConfig.HttpsPort, "https-port", server.DefaultHttpsPort, "Port to serve HTTPS traffic on")
runCommand.cmd.Flags().BoolVar(&runCommand.debugLogsEnabled, "debug", getEnvBool("DEBUG", false), "Include debugging logs")
runCommand.cmd.Flags().IntVar(&globalConfig.HttpPort, "http-port", getEnvInt("HTTP_PORT", server.DefaultHttpPort), "Port to serve HTTP traffic on")
runCommand.cmd.Flags().IntVar(&globalConfig.HttpsPort, "https-port", getEnvInt("HTTPS_PORT", server.DefaultHttpsPort), "Port to serve HTTPS traffic on")

return runCommand
}
Expand Down
48 changes: 48 additions & 0 deletions internal/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package cmd

import (
"net/rpc"
"os"
"strconv"
)

const (
ENV_PREFIX = "MPROXY_"
)

func withRPCClient(socketPath string, fn func(client *rpc.Client) error) error {
Expand All @@ -12,3 +18,45 @@ func withRPCClient(socketPath string, fn func(client *rpc.Client) error) error {
defer client.Close()
return fn(client)
}

func findEnv(key string) (string, bool) {
value, ok := os.LookupEnv(ENV_PREFIX + key)
if ok {
return value, true
}

value, ok = os.LookupEnv(key)
if ok {
return value, true
}

return "", false
}

func getEnvInt(key string, defaultValue int) int {
value, ok := findEnv(key)
if !ok {
return defaultValue
}

intValue, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}

return intValue
}

func getEnvBool(key string, defaultValue bool) bool {
value, ok := findEnv(key)
if !ok {
return defaultValue
}

boolValue, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}

return boolValue
}

0 comments on commit 1982299

Please sign in to comment.