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
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/quic-go/quic-go/http3"
|
|
)
|
|
|
|
type Quic struct {
|
|
server *http3.Server
|
|
tlsServer *TLS
|
|
ctx context.Context
|
|
addr string
|
|
crtPath string
|
|
keyPath string
|
|
}
|
|
|
|
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: q.addr,
|
|
Handler: q.tlsServer.server.Handler,
|
|
}
|
|
|
|
parentHandler := q.tlsServer.server.Handler
|
|
q.tlsServer.server.Handler = http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
if err := q.server.SetQUICHeaders(rw.Header()); err != nil {
|
|
log.Printf("Failed to set QUIC headers: %s", err)
|
|
}
|
|
|
|
parentHandler.ServeHTTP(rw, req)
|
|
})
|
|
|
|
log.Printf("Starting QUIC server listening on %s (udp)", q.addr)
|
|
go func() {
|
|
if err := q.server.ListenAndServeTLS(q.crtPath, q.keyPath); err != nil &&
|
|
!errors.Is(err, http.ErrServerClosed) {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (q *Quic) Stop() {
|
|
log.Print("Stopping QUIC server...")
|
|
ctx, cancel := context.WithTimeout(q.ctx, shutdownTimeout)
|
|
defer cancel()
|
|
if err := q.server.Shutdown(ctx); err != nil {
|
|
log.Printf("QUIC server forced to shutdown: %s", err)
|
|
}
|
|
}
|