mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2026-07-23 21:35:47 +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
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
type Prometheus struct {
|
|
server *http.Server
|
|
ctx context.Context
|
|
addr string
|
|
timeouts Timeouts
|
|
}
|
|
|
|
func NewPrometheusServer(ctx context.Context, addr string, timeouts Timeouts) *Prometheus {
|
|
return &Prometheus{
|
|
ctx: ctx,
|
|
addr: addr,
|
|
timeouts: timeouts,
|
|
}
|
|
}
|
|
|
|
func (p *Prometheus) Start() {
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/metrics", promhttp.Handler())
|
|
|
|
p.server = &http.Server{
|
|
Addr: p.addr,
|
|
Handler: mux,
|
|
ReadTimeout: p.timeouts.ReadTimeout,
|
|
WriteTimeout: p.timeouts.WriteTimeout,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (p *Prometheus) Stop() {
|
|
log.Print("Stopping Prometheus server...")
|
|
ctx, cancel := context.WithTimeout(p.ctx, shutdownTimeout)
|
|
defer cancel()
|
|
if err := p.server.Shutdown(ctx); err != nil {
|
|
log.Printf("Prometheus server forced to shutdown: %s", err)
|
|
}
|
|
}
|