mirror of
https://github.com/elyby/chrly.git
synced 2024-12-24 14:09:56 +05:30
Simplify stop signal handling
This commit is contained in:
parent
cecd07c113
commit
fdafbc4f0e
@ -39,11 +39,7 @@ func newHandlerFactory(
|
|||||||
}
|
}
|
||||||
|
|
||||||
router.StrictSlash(true)
|
router.StrictSlash(true)
|
||||||
requestEventsMiddleware := CreateRequestEventsMiddleware()
|
router.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
|
||||||
router.Use(requestEventsMiddleware)
|
|
||||||
// NotFoundHandler doesn't call for registered middlewares, so we must wrap it manually.
|
|
||||||
// See https://github.com/gorilla/mux/issues/416#issuecomment-600079279
|
|
||||||
router.NotFoundHandler = requestEventsMiddleware(http.HandlerFunc(NotFoundHandler))
|
|
||||||
|
|
||||||
if slices.Contains(enabledModules, "api") {
|
if slices.Contains(enabledModules, "api") {
|
||||||
var apiRouter *mux.Router
|
var apiRouter *mux.Router
|
||||||
|
@ -3,11 +3,11 @@ package http
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/mono83/slf"
|
"github.com/mono83/slf"
|
||||||
@ -19,52 +19,28 @@ import (
|
|||||||
func StartServer(server *http.Server, logger slf.Logger) {
|
func StartServer(server *http.Server, logger slf.Logger) {
|
||||||
logger.Debug("Chrly :v (:c)", wd.StringParam("v", version.Version()), wd.StringParam("c", version.Commit()))
|
logger.Debug("Chrly :v (:c)", wd.StringParam("v", version.Version()), wd.StringParam("c", version.Commit()))
|
||||||
|
|
||||||
done := make(chan bool, 1)
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, os.Kill)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
srvErr := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
logger.Info("Starting the server, HTTP on: :addr", wd.StringParam("addr", server.Addr))
|
logger.Info("Starting the server, HTTP on: :addr", wd.StringParam("addr", server.Addr))
|
||||||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
srvErr <- server.ListenAndServe()
|
||||||
logger.Emergency("Error in main(): :err", wd.ErrParam(err))
|
close(srvErr)
|
||||||
close(done)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
select {
|
||||||
s := waitForExitSignal()
|
case err := <-srvErr:
|
||||||
logger.Info("Got signal: :signal, starting graceful shutdown", wd.StringParam("signal", s.String()))
|
logger.Emergency("Error in main(): :err", wd.ErrParam(err))
|
||||||
_ = server.Shutdown(context.Background())
|
case <-ctx.Done():
|
||||||
logger.Info("Graceful shutdown succeed, exiting", wd.StringParam("signal", s.String()))
|
logger.Info("Got stop signal, starting graceful shutdown: :ctx")
|
||||||
close(done)
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-done
|
stopCtx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
}
|
defer cancelFunc()
|
||||||
|
|
||||||
func waitForExitSignal() os.Signal {
|
_ = server.Shutdown(stopCtx)
|
||||||
ch := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, os.Kill)
|
|
||||||
|
|
||||||
return <-ch
|
logger.Info("Graceful shutdown succeed, exiting")
|
||||||
}
|
|
||||||
|
|
||||||
type loggingResponseWriter struct {
|
|
||||||
http.ResponseWriter
|
|
||||||
statusCode int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lrw *loggingResponseWriter) WriteHeader(code int) {
|
|
||||||
lrw.statusCode = code
|
|
||||||
lrw.ResponseWriter.WriteHeader(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateRequestEventsMiddleware() mux.MiddlewareFunc {
|
|
||||||
return func(handler http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
||||||
lrw := &loggingResponseWriter{
|
|
||||||
ResponseWriter: resp,
|
|
||||||
statusCode: http.StatusOK,
|
|
||||||
}
|
|
||||||
handler.ServeHTTP(lrw, req)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,22 +11,6 @@ import (
|
|||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateRequestEventsMiddleware(t *testing.T) {
|
|
||||||
req := httptest.NewRequest("GET", "http://example.com", nil)
|
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
|
|
||||||
isHandlerCalled := false
|
|
||||||
middlewareFunc := CreateRequestEventsMiddleware()
|
|
||||||
middlewareFunc.Middleware(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
||||||
resp.WriteHeader(400)
|
|
||||||
isHandlerCalled = true
|
|
||||||
})).ServeHTTP(resp, req)
|
|
||||||
|
|
||||||
if !isHandlerCalled {
|
|
||||||
t.Fatal("Handler isn't called from the middleware")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type authCheckerMock struct {
|
type authCheckerMock struct {
|
||||||
mock.Mock
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user