fermentord/internal/api/http.go

52 lines
891 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"
2024-06-15 14:31:02 +00:00
"git.joco.dk/snr/fermentord/internal/configuration"
2021-08-30 20:46:38 +00:00
"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 {
2022-03-15 16:20:47 +00:00
config *configuration.Configuration
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"))
2022-07-19 08:38:21 +00:00
return
2021-08-30 20:46:38 +00:00
}
2022-02-18 20:59:32 +00:00
// Respond OK
2022-07-19 08:38:21 +00:00
w.WriteHeader(http.StatusOK)
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)
}