mux/router_serve.go

63 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-10-12 14:04:17 +00:00
package mux
import (
"context"
2024-10-12 15:27:57 +00:00
"errors"
2024-11-04 05:30:02 +00:00
"io"
2024-10-12 14:04:17 +00:00
"log/slog"
"net/http"
"os"
"os/signal"
)
type ServeCB func(srv *http.Server) error
2024-10-12 15:27:57 +00:00
// Serve with graceful shutdown
2024-10-12 14:04:17 +00:00
func (r *Router) Serve(cb ServeCB) {
2024-11-04 05:30:02 +00:00
// catch all options
// lets get it thorugh all middlewares
r.mux.Handle("OPTIONS /", optionsHandler{})
2024-10-12 14:04:17 +00:00
srv := &http.Server{
Handler: r,
}
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
// We received an interrupt signal, shut down.
if err := srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
slog.Error("server shutdown error", "error", err)
} else {
slog.Info("server shutdown")
}
close(idleConnsClosed)
}()
2024-10-12 15:27:57 +00:00
if err := cb(srv); !errors.Is(err, http.ErrServerClosed) {
2024-10-12 14:04:17 +00:00
// Error starting or closing listener:
slog.Error("start server error", "error", err)
}
<-idleConnsClosed
}
2024-11-04 05:30:02 +00:00
type optionsHandler struct{}
func (optionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "0")
if r.ContentLength != 0 {
// Read up to 4KB of OPTIONS body (as mentioned in the
// spec as being reserved for future use), but anything
// over that is considered a waste of server resources
// (or an attack) and we abort and close the connection,
// courtesy of MaxBytesReader's EOF behavior.
mb := http.MaxBytesReader(w, r.Body, 4<<10)
io.Copy(io.Discard, mb)
}
}