fermentord/internal/api/http.go

50 lines
851 B
Go
Raw Normal View History

2021-08-30 20:46:38 +00:00
package api
import (
"encoding/json"
2022-02-18 20:59:32 +00:00
"log"
2021-08-30 20:46:38 +00:00
"net/http"
"git.joco.dk/sng/fermentord/internal/controllers"
"github.com/getsentry/sentry-go"
2022-02-18 20:59:32 +00:00
"github.com/nats-io/nats.go"
2021-08-30 20:46:38 +00:00
)
type API struct {
2021-11-16 05:19:24 +00:00
config *controllers.ControllerConfig
2021-08-30 20:46:38 +00:00
configChangeCallback func()
2022-02-18 20:59:32 +00:00
nc *nats.Conn
2021-08-30 20:46:38 +00:00
}
2022-02-18 20:59:32 +00:00
func NewAPI(nc *nats.Conn) *API {
2021-08-30 20:46:38 +00:00
return &API{
2022-02-18 20:59:32 +00:00
nc: nc,
2021-08-30 20:46:38 +00:00
}
}
func (a *API) HealthCheck(w http.ResponseWriter, r *http.Request) {
2022-02-18 20:59:32 +00:00
// Check NATS
switch a.nc.Status() {
case nats.CONNECTED:
break
2021-08-30 20:46:38 +00:00
2022-02-18 20:59:32 +00:00
default:
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("Unhealthy"))
2021-08-30 20:46:38 +00:00
}
2022-02-18 20:59:32 +00:00
// Respond OK
2021-08-30 20:46:38 +00:00
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 {
2022-02-18 20:59:32 +00:00
log.Panic(err)
2021-08-30 20:46:38 +00:00
}
w.Write(resp)
}