refactor: dependency injection instead of package globals (#57)

* refactor: export Settings type, Setup returns value

* refactor: resolver.Setup takes explicit Settings struct

* refactor: GetHeadersWithoutTrustedHeaders takes explicit header params

* refactor: server constructors take narrow config

* refactor: Router struct with handler methods, remove geoSvc global

* refactor: wire DI through main, remove setting.App references

* refactor: remove App global, use returned Settings

* refactor: update router tests for DI

* chore: fix lint issues — rename ServerTimeouts to Timeouts, fix shadowed variables
This commit is contained in:
2026-07-21 18:37:21 +02:00
committed by GitHub
parent 3874f6af3f
commit 2e32a20f60
19 changed files with 298 additions and 253 deletions
+12 -9
View File
@@ -6,18 +6,21 @@ import (
"log"
"net/http"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Prometheus struct {
server *http.Server
ctx context.Context
server *http.Server
ctx context.Context
addr string
timeouts Timeouts
}
func NewPrometheusServer(ctx context.Context) *Prometheus {
func NewPrometheusServer(ctx context.Context, addr string, timeouts Timeouts) *Prometheus {
return &Prometheus{
ctx: ctx,
ctx: ctx,
addr: addr,
timeouts: timeouts,
}
}
@@ -26,13 +29,13 @@ func (p *Prometheus) Start() {
mux.Handle("/metrics", promhttp.Handler())
p.server = &http.Server{
Addr: setting.App.PrometheusAddress,
Addr: p.addr,
Handler: mux,
ReadTimeout: setting.App.Server.ReadTimeout,
WriteTimeout: setting.App.Server.WriteTimeout,
ReadTimeout: p.timeouts.ReadTimeout,
WriteTimeout: p.timeouts.WriteTimeout,
}
log.Printf("Starting Prometheus server listening on %s", setting.App.PrometheusAddress)
log.Printf("Starting Prometheus server listening on %s", p.addr)
go func() {
if err := p.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
+10 -5
View File
@@ -6,7 +6,6 @@ import (
"log"
"net/http"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/quic-go/quic-go/http3"
)
@@ -14,18 +13,24 @@ type Quic struct {
server *http3.Server
tlsServer *TLS
ctx context.Context
addr string
crtPath string
keyPath string
}
func NewQuicServer(ctx context.Context, tlsServer *TLS) *Quic {
func NewQuicServer(ctx context.Context, tlsServer *TLS, addr, crt, key string) *Quic {
return &Quic{
tlsServer: tlsServer,
ctx: ctx,
addr: addr,
crtPath: crt,
keyPath: key,
}
}
func (q *Quic) Start() {
q.server = &http3.Server{
Addr: setting.App.TLSAddress,
Addr: q.addr,
Handler: q.tlsServer.server.Handler,
}
@@ -38,9 +43,9 @@ func (q *Quic) Start() {
parentHandler.ServeHTTP(rw, req)
})
log.Printf("Starting QUIC server listening on %s (udp)", setting.App.TLSAddress)
log.Printf("Starting QUIC server listening on %s (udp)", q.addr)
go func() {
if err := q.server.ListenAndServeTLS(setting.App.TLSCrtPath, setting.App.TLSKeyPath); err != nil &&
if err := q.server.ListenAndServeTLS(q.crtPath, q.keyPath); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
+21 -13
View File
@@ -5,32 +5,40 @@ import (
"errors"
"log"
"net/http"
"github.com/dcarrillo/whatismyip/internal/setting"
"time"
)
type TCP struct {
server *http.Server
handler http.Handler
ctx context.Context
type Timeouts struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func NewTCPServer(ctx context.Context, handler http.Handler) *TCP {
type TCP struct {
server *http.Server
handler http.Handler
ctx context.Context
addr string
timeouts Timeouts
}
func NewTCPServer(ctx context.Context, handler http.Handler, addr string, timeouts Timeouts) *TCP {
return &TCP{
handler: handler,
ctx: ctx,
handler: handler,
ctx: ctx,
addr: addr,
timeouts: timeouts,
}
}
func (t *TCP) Start() {
t.server = &http.Server{
Addr: setting.App.BindAddress,
Addr: t.addr,
Handler: t.handler,
ReadTimeout: setting.App.Server.ReadTimeout,
WriteTimeout: setting.App.Server.WriteTimeout,
ReadTimeout: t.timeouts.ReadTimeout,
WriteTimeout: t.timeouts.WriteTimeout,
}
log.Printf("Starting TCP server listening on %s", setting.App.BindAddress)
log.Printf("Starting TCP server listening on %s", t.addr)
go func() {
if err := t.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
+19 -13
View File
@@ -6,37 +6,43 @@ import (
"errors"
"log"
"net/http"
"github.com/dcarrillo/whatismyip/internal/setting"
)
type TLS struct {
server *http.Server
handler http.Handler
ctx context.Context
server *http.Server
handler http.Handler
ctx context.Context
addr string
crtPath string
keyPath string
timeouts Timeouts
}
func NewTLSServer(ctx context.Context, handler http.Handler) *TLS {
func NewTLSServer(ctx context.Context, handler http.Handler, addr, crt, key string, timeouts Timeouts) *TLS {
return &TLS{
handler: handler,
ctx: ctx,
handler: handler,
ctx: ctx,
addr: addr,
crtPath: crt,
keyPath: key,
timeouts: timeouts,
}
}
func (t *TLS) Start() {
t.server = &http.Server{
Addr: setting.App.TLSAddress,
Addr: t.addr,
Handler: t.handler,
ReadTimeout: setting.App.Server.ReadTimeout,
WriteTimeout: setting.App.Server.WriteTimeout,
ReadTimeout: t.timeouts.ReadTimeout,
WriteTimeout: t.timeouts.WriteTimeout,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
log.Printf("Starting TLS server listening on %s", setting.App.TLSAddress)
log.Printf("Starting TLS server listening on %s", t.addr)
go func() {
if err := t.server.ListenAndServeTLS(setting.App.TLSCrtPath, setting.App.TLSKeyPath); err != nil &&
if err := t.server.ListenAndServeTLS(t.crtPath, t.keyPath); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}