feat: added server configuration using env vars

This commit is contained in:
Felipe M 2022-08-09 17:32:40 +02:00
parent e69ecef6c8
commit ba4be3f48f
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
5 changed files with 97 additions and 21 deletions

View File

@ -1,6 +1,8 @@
package main
import (
"context"
"github.com/fmartingr/bazaar/internal/server"
"github.com/fmartingr/bazaar/pkg/manager"
"github.com/fmartingr/bazaar/pkg/shop/akiracomics"
@ -20,11 +22,11 @@ func main() {
m.Register(gtmstore.Domains, gtmstore.NewGTMStoreShopFactory())
m.Register(casadellibro.Domains, casadellibro.NewCasaDelLibroShopFactory())
server := server.NewServer(server.ServerConf{
HttpPort: 8080,
}, &m)
ctx := context.Background()
server.Start()
server := server.NewServer(server.ParseServerConfiguration(ctx), &m)
server.Start(ctx)
server.WaitStop()
}

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.19
require (
github.com/PuerkitoBio/goquery v1.8.0
github.com/goodsign/monday v1.0.0
github.com/sethvargo/go-envconfig v0.8.2
github.com/stretchr/testify v1.8.0
)

3
go.sum
View File

@ -7,8 +7,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goodsign/monday v1.0.0 h1:Yyk/s/WgudMbAJN6UWSU5xAs8jtNewfqtVblAlw0yoc=
github.com/goodsign/monday v1.0.0/go.mod h1:r4T4breXpoFwspQNM+u2sLxJb2zyTaxVGqUfTBjWOu8=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sethvargo/go-envconfig v0.8.2 h1:DDUVuG21RMgeB/bn4leclUI/837y6cQCD4w8hb5797k=
github.com/sethvargo/go-envconfig v0.8.2/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

64
internal/server/config.go Normal file
View File

@ -0,0 +1,64 @@
package server
import (
"bufio"
"context"
"log"
"os"
"strings"
"github.com/sethvargo/go-envconfig"
)
// readDotEnv reads the configuration from variables in a .env file (only for contributing)
func readDotEnv() map[string]string {
file, err := os.Open(".env")
if err != nil {
return nil
}
defer file.Close()
result := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
keyval := strings.SplitN(line, "=", 2)
result[keyval[0]] = keyval[1]
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return result
}
type ServerConfig struct {
Hostname string `env:"HOSTNAME,required"`
Http struct {
Enabled bool `env:"HTTP_ENABLED,default=True"`
Port int `env:"HTTP_PORT,default=8080"`
}
}
func ParseServerConfiguration(ctx context.Context) *ServerConfig {
var cfg ServerConfig
lookuper := envconfig.MultiLookuper(
envconfig.MapLookuper(map[string]string{"HOSTNAME": os.Getenv("HOSTNAME")}),
envconfig.MapLookuper(readDotEnv()),
envconfig.PrefixLookuper("BAZAAR_", envconfig.OsLookuper()),
envconfig.OsLookuper(),
)
if err := envconfig.ProcessWith(ctx, &cfg, lookuper); err != nil {
log.Fatalf("Error parsing configuration: %s", err)
}
return &cfg
}

View File

@ -14,25 +14,24 @@ import (
"github.com/fmartingr/bazaar/pkg/manager"
)
type ServerConf struct {
HttpPort int
}
type Server struct {
http internalModels.Server
Http internalModels.Server
config *ServerConfig
cancel context.CancelFunc
}
func (s *Server) Start() error {
ctx, cancel := context.WithCancel(context.Background())
func (s *Server) Start(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
s.cancel = cancel
go func() {
if err := s.http.Start(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("error starting server: %s", err)
}
}()
if s.config.Http.Enabled {
go func() {
if err := s.Http.Start(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("error starting server: %s", err)
}
}()
}
return nil
}
@ -53,15 +52,22 @@ func (s *Server) Stop() error {
shuwdownContext, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := s.http.Stop(shuwdownContext); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("error shutting down http server: %s", err)
if s.config.Http.Enabled {
if err := s.Http.Stop(shuwdownContext); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("error shutting down http server: %s", err)
}
}
return nil
}
func NewServer(serverConf ServerConf, m *manager.Manager) *Server {
return &Server{
http: NewHttpServer(serverConf.HttpPort, m),
func NewServer(conf *ServerConfig, m *manager.Manager) *Server {
server := &Server{
config: conf,
}
if conf.Http.Enabled {
server.Http = NewHttpServer(conf.Http.Port, m)
}
return server
}