fermentord/internal/api/http.go

51 lines
894 B
Go

package api
import (
"encoding/json"
"log"
"net/http"
"git.joco.dk/sng/fermentord/internal/configuration"
"github.com/getsentry/sentry-go"
"github.com/nats-io/nats.go"
)
type API struct {
config *configuration.ControllerConfig
configChangeCallback func()
nc *nats.Conn
}
func NewAPI(nc *nats.Conn) *API {
return &API{
nc: nc,
}
}
func (a *API) HealthCheck(w http.ResponseWriter, r *http.Request) {
// Check NATS
switch a.nc.Status() {
case nats.CONNECTED:
break
default:
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("Unhealthy"))
return
}
// Respond OK
w.WriteHeader(http.StatusOK)
w.Write([]byte("Healthy"))
}
func (a *API) GetConfig(w http.ResponseWriter, r *http.Request) {
defer sentry.Recover()
resp, err := json.MarshalIndent(a.config, "", " ")
if err != nil {
log.Panic(err)
}
w.Write(resp)
}