mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-21 13:23:03 +01:00
This features adds rfc7807 Problem detail responses when an error happens processing a request. This will greatly improve the common issues with "blank pages" and "404 pages" issues which should now properly tell the user what input was wrong (group that does not exist, container name that does not exist, etc.)
41 lines
858 B
Go
41 lines
858 B
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Health struct {
|
|
TerminatingStatusCode int `description:"Terminating status code" json:"terminatingStatusCode,omitempty" yaml:"terminatingStatusCode,omitempty" export:"true"`
|
|
terminating bool
|
|
}
|
|
|
|
func (h *Health) SetDefaults() {
|
|
h.TerminatingStatusCode = http.StatusServiceUnavailable
|
|
}
|
|
|
|
func (h *Health) WithContext(ctx context.Context) {
|
|
go func() {
|
|
<-ctx.Done()
|
|
h.terminating = true
|
|
}()
|
|
}
|
|
|
|
func (h *Health) ServeHTTP(c *gin.Context) {
|
|
statusCode := http.StatusOK
|
|
if h.terminating {
|
|
statusCode = h.TerminatingStatusCode
|
|
}
|
|
|
|
c.String(statusCode, http.StatusText(statusCode))
|
|
}
|
|
|
|
func Healthcheck(router *gin.RouterGroup, ctx context.Context) {
|
|
health := Health{}
|
|
health.SetDefaults()
|
|
health.WithContext(ctx)
|
|
router.GET("/health", health.ServeHTTP)
|
|
}
|