package http

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sync/atomic"
)

var open atomic.Bool

type StateListener struct {
}

func (a StateListener) StateChanged(service, state string) {
	if "open" == state {
		open.Store(true)
	} else {
		open.Store(false)
	}
}

func AvailabilityMiddleware() func(ginCtx *gin.Context) {

	return func(ginCtx *gin.Context) {

		if open.Load() {
			ginCtx.AbortWithStatus(http.StatusBadGateway)
			return
		}

		ginCtx.Next()
	}

}
