add redis status check
This commit is contained in:
parent
f7f1535695
commit
89ce1f361c
2 changed files with 43 additions and 1 deletions
39
server/routers/service_state.go
Normal file
39
server/routers/service_state.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"recipe-manager/data"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type ServiceCheckRouter struct {
|
||||
cache_db *data.RedisCli
|
||||
// add service here!
|
||||
}
|
||||
|
||||
func NewServiceCheckRouter(cache_db *data.RedisCli) *ServiceCheckRouter {
|
||||
return &ServiceCheckRouter{cache_db: cache_db}
|
||||
}
|
||||
|
||||
func (nscr *ServiceCheckRouter) Route(r chi.Router) {
|
||||
r.Route("/health", func(r chi.Router) {
|
||||
r.Get("/redis", nscr.RedisHealth)
|
||||
})
|
||||
}
|
||||
|
||||
func (nscr *ServiceCheckRouter) RedisHealth(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// {status: online/offline}
|
||||
err := nscr.cache_db.HealthCheck()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(`{"status":"Offline"}`))
|
||||
return
|
||||
}
|
||||
// no error = online
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"status":"Online"}`))
|
||||
|
||||
}
|
||||
|
|
@ -139,7 +139,7 @@ func (s *Server) createHandler() {
|
|||
ur := routers.NewUserRouter(s.taoLogger, userService)
|
||||
ur.Route(r)
|
||||
|
||||
fmt.Println("routers", r.Routes())
|
||||
// fmt.Println("routers", r.Routes())
|
||||
|
||||
})
|
||||
|
||||
|
|
@ -156,6 +156,9 @@ func (s *Server) createHandler() {
|
|||
})
|
||||
})
|
||||
|
||||
nscr := routers.NewServiceCheckRouter(s.cache_db)
|
||||
nscr.Route(r)
|
||||
|
||||
// routers.NewToppingRouter(s.data, s.taoLogger).Route(r)
|
||||
|
||||
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue