mirror of
https://github.com/rodneyosodo/gophercon-africa-2024.git
synced 2026-06-23 04:10:06 +00:00
refactor: use env variables rather than flags
Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>
This commit is contained in:
+20
-15
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -10,6 +9,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/rodneyosodo/gophercon/calculator"
|
||||
@@ -25,21 +25,26 @@ import (
|
||||
|
||||
const defHTTPTimeout = 10 * time.Second
|
||||
|
||||
type config struct {
|
||||
LogLevel string `env:"GOPHERCON_LOG_LEVEL" envDefault:"info"`
|
||||
Addr string `env:"GOPHERCON_ADDR" envDefault:":11211"`
|
||||
PrometheusEndpoint string `env:"GOPHERCON_PROMETHEUS_ENDPOINT" envDefault:":9464"`
|
||||
ReadTimeout time.Duration `env:"GOPHERCON_READ_TIMEOUT" envDefault:"10s"`
|
||||
WriteTimeout time.Duration `env:"GOPHERCON_WRITE_TIMEOUT" envDefault:"10s"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var g *errgroup.Group
|
||||
g, _ = errgroup.WithContext(ctx)
|
||||
|
||||
logLevel := flag.String("log_level", "info", "log level")
|
||||
addr := flag.String("addr", ":11211", "gophercon calculator server address")
|
||||
prometheusEndpoint := flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint")
|
||||
readTimeout := flag.Duration("read_timeout", defHTTPTimeout, "the read timeout for the server")
|
||||
writeTimeout := flag.Duration("write_timeout", defHTTPTimeout, "the write timeout for the server")
|
||||
|
||||
flag.Parse()
|
||||
cfg := config{}
|
||||
if err := env.Parse(&cfg); err != nil {
|
||||
log.Fatalf("failed to load configuration : %s", err.Error())
|
||||
}
|
||||
|
||||
var level slog.Level
|
||||
if err := level.UnmarshalText([]byte(*logLevel)); err != nil {
|
||||
if err := level.UnmarshalText([]byte(cfg.LogLevel)); err != nil {
|
||||
log.Fatalf("failed to parse log level: %s", err.Error())
|
||||
}
|
||||
logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||
@@ -58,21 +63,21 @@ func main() {
|
||||
|
||||
g.Go(func() error {
|
||||
server := &http.Server{
|
||||
Addr: *prometheusEndpoint,
|
||||
Addr: cfg.PrometheusEndpoint,
|
||||
Handler: promhttp.Handler(),
|
||||
ReadTimeout: *readTimeout,
|
||||
WriteTimeout: *writeTimeout,
|
||||
ReadTimeout: cfg.ReadTimeout,
|
||||
WriteTimeout: cfg.WriteTimeout,
|
||||
}
|
||||
|
||||
return server.ListenAndServe()
|
||||
})
|
||||
logger.Info("Prometheus exporter started", slog.String("endpoint", *prometheusEndpoint))
|
||||
logger.Info("Prometheus exporter started", slog.String("endpoint", cfg.PrometheusEndpoint))
|
||||
|
||||
so := opentelemetry.ServerOption(
|
||||
opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: provider}},
|
||||
)
|
||||
|
||||
listener, err := net.Listen("tcp", *addr)
|
||||
listener, err := net.Listen("tcp", cfg.Addr)
|
||||
if err != nil {
|
||||
logger.Error("Failed to listen", slog.String("error", err.Error()))
|
||||
cancel()
|
||||
@@ -95,7 +100,7 @@ func main() {
|
||||
return server.Serve(listener)
|
||||
})
|
||||
|
||||
logger.Info("Calculator server started", slog.String("address", *addr))
|
||||
logger.Info("Calculator server started", slog.String("address", cfg.Addr))
|
||||
|
||||
if err := g.Wait(); err != nil {
|
||||
logger.Error("Failed to serve", slog.String("error", err.Error()))
|
||||
|
||||
@@ -3,6 +3,7 @@ module github.com/rodneyosodo/gophercon
|
||||
go 1.23.2
|
||||
|
||||
require (
|
||||
github.com/caarlos0/env/v11 v11.2.2
|
||||
github.com/hashicorp/go-retryablehttp v0.7.7
|
||||
github.com/prometheus/client_golang v1.20.5
|
||||
go.opentelemetry.io/otel v1.31.0
|
||||
|
||||
@@ -4,6 +4,8 @@ cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJ
|
||||
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
|
||||
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
|
||||
github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g=
|
||||
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
@@ -33,8 +35,6 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
|
||||
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
|
||||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
@@ -71,8 +71,6 @@ go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4Jjx
|
||||
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
|
||||
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
|
||||
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
|
||||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
||||
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
||||
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
|
||||
@@ -81,20 +79,14 @@ golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 h1:hjSy6tcFQZ171igDaN5QHOw2n6vx40juYbC/x67CEhc=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:qpvKtACPCQhAdu3PyQgV4l3LMXZEtft7y8QcarRsp9I=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 h1:X58yt85/IXCx0Y3ZwN6sEIKZzQtDEYaBWrDvErdXrRE=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
|
||||
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
|
||||
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
|
||||
google.golang.org/grpc/stats/opentelemetry v0.0.0-20241016195115-6cd00c93260b h1:ut5YTSkHaayJBGRoskBOfk8yam7lcf/u3PTHEDNXSQY=
|
||||
google.golang.org/grpc/stats/opentelemetry v0.0.0-20241016195115-6cd00c93260b/go.mod h1:xwT0YrcBcgR1ZSSLJtUgCjF5QlvTOhiwA/I9TcYf3Gg=
|
||||
google.golang.org/grpc/stats/opentelemetry v0.0.0-20241016200935-ec10e73f02a3 h1:SI+r2zromfd3aGS+7QTZ5a17ip8znCO8rHWv35iM+Nw=
|
||||
google.golang.org/grpc/stats/opentelemetry v0.0.0-20241016200935-ec10e73f02a3/go.mod h1:xwT0YrcBcgR1ZSSLJtUgCjF5QlvTOhiwA/I9TcYf3Gg=
|
||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
||||
|
||||
Reference in New Issue
Block a user