From 570912e752617f6eb83f2ea41fe6c35d4a1e5508 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 22 Dec 2022 08:16:37 +0100 Subject: [PATCH] start: introduce function-args as alternative to env variable params the Start function always assumes there are environment variables such as HOST and PORT on start up. this is not always desirable and especially makes it hard to run independent tests concurrently. this commit introduces StartConf, an alternative to Start where the same settings like host and port are passed in a function argument instead of the global process environment. --- start.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/start.go b/start.go index a8412ac..0733971 100644 --- a/start.go +++ b/start.go @@ -11,24 +11,30 @@ import ( "github.com/rs/zerolog" ) +// Settings specify initial startup parameters for a relay server. +// See StartConf for details. type Settings struct { Host string `envconfig:"HOST" default:"0.0.0.0"` Port string `envconfig:"PORT" default:"7447"` } -var ( - s Settings - log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stderr}) -) +var log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stderr}) var Router = mux.NewRouter() +// Start calls StartConf with Settings parsed from the process environment. func Start(relay Relay) { - // read host/port (implementations can read other stuff on their own if they need) + var s Settings if err := envconfig.Process("", &s); err != nil { log.Panic().Err(err).Msg("couldn't process envconfig") } + StartConf(s, relay) +} +// StartConf initalizes the relay and its storage using their respective Init methods, +// and starts listening for HTTP requests on host:port, as specified in the settings. +// It never returns until process termination. +func StartConf(s Settings, relay Relay) { // allow implementations to do initialization stuff if err := relay.Init(); err != nil { Log.Fatal().Err(err).Msg("failed to start")