mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2026-07-23 22:45:46 +00:00
2e32a20f60
* 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
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Timeouts struct {
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
}
|
|
|
|
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,
|
|
addr: addr,
|
|
timeouts: timeouts,
|
|
}
|
|
}
|
|
|
|
func (t *TCP) Start() {
|
|
t.server = &http.Server{
|
|
Addr: t.addr,
|
|
Handler: t.handler,
|
|
ReadTimeout: t.timeouts.ReadTimeout,
|
|
WriteTimeout: t.timeouts.WriteTimeout,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (t *TCP) Stop() {
|
|
log.Print("Stopping TCP server...")
|
|
ctx, cancel := context.WithTimeout(t.ctx, shutdownTimeout)
|
|
defer cancel()
|
|
if err := t.server.Shutdown(ctx); err != nil {
|
|
log.Printf("TCP server forced to shutdown: %s", err)
|
|
}
|
|
}
|