mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Add health endpoint.
This commit is contained in:
parent
cd197e855c
commit
7a74ae251c
7
main.go
7
main.go
@ -164,9 +164,13 @@ func main() {
|
|||||||
// Handlers
|
// Handlers
|
||||||
heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc}
|
heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc}
|
||||||
summaryHandler := &routes.SummaryHandler{SummarySrvc: summarySrvc}
|
summaryHandler := &routes.SummaryHandler{SummarySrvc: summarySrvc}
|
||||||
|
healthHandler := &routes.HealthHandler{Db: db}
|
||||||
|
|
||||||
// Middlewares
|
// Middlewares
|
||||||
authenticateMiddleware := &middlewares.AuthenticateMiddleware{UserSrvc: userSrvc}
|
authenticateMiddleware := &middlewares.AuthenticateMiddleware{
|
||||||
|
UserSrvc: userSrvc,
|
||||||
|
WhitelistPaths: []string{"/api/health"},
|
||||||
|
}
|
||||||
basicAuthMiddleware := &middlewares.RequireBasicAuthMiddleware{}
|
basicAuthMiddleware := &middlewares.RequireBasicAuthMiddleware{}
|
||||||
corsMiddleware := cors.New(cors.Options{
|
corsMiddleware := cors.New(cors.Options{
|
||||||
AllowedOrigins: []string{"*"},
|
AllowedOrigins: []string{"*"},
|
||||||
@ -185,6 +189,7 @@ func main() {
|
|||||||
// API Routes
|
// API Routes
|
||||||
apiRouter.Path("/heartbeat").Methods(http.MethodPost).HandlerFunc(heartbeatHandler.ApiPost)
|
apiRouter.Path("/heartbeat").Methods(http.MethodPost).HandlerFunc(heartbeatHandler.ApiPost)
|
||||||
apiRouter.Path("/summary").Methods(http.MethodGet).HandlerFunc(summaryHandler.ApiGet)
|
apiRouter.Path("/summary").Methods(http.MethodGet).HandlerFunc(summaryHandler.ApiGet)
|
||||||
|
apiRouter.Path("/health").Methods(http.MethodGet).HandlerFunc(healthHandler.ApiGet)
|
||||||
|
|
||||||
// Static Routes
|
// Static Routes
|
||||||
router.PathPrefix("/assets").Handler(negroni.Classic().With(negroni.Wrap(http.FileServer(http.Dir("./static")))))
|
router.PathPrefix("/assets").Handler(negroni.Classic().With(negroni.Wrap(http.FileServer(http.Dir("./static")))))
|
||||||
|
@ -18,9 +18,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AuthenticateMiddleware struct {
|
type AuthenticateMiddleware struct {
|
||||||
UserSrvc *services.UserService
|
UserSrvc *services.UserService
|
||||||
Cache *cache.Cache
|
Cache *cache.Cache
|
||||||
Initialized bool
|
WhitelistPaths []string
|
||||||
|
Initialized bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AuthenticateMiddleware) Init() {
|
func (m *AuthenticateMiddleware) Init() {
|
||||||
@ -35,6 +36,13 @@ func (m *AuthenticateMiddleware) Handle(w http.ResponseWriter, r *http.Request,
|
|||||||
m.Init()
|
m.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, p := range m.WhitelistPaths {
|
||||||
|
if strings.HasPrefix(r.URL.Path, p) || r.URL.Path == p {
|
||||||
|
next(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var user *models.User
|
var user *models.User
|
||||||
var userKey string
|
var userKey string
|
||||||
user, userKey, err := m.tryGetUserByPassword(r)
|
user, userKey, err := m.tryGetUserByPassword(r)
|
||||||
|
21
routes/health.go
Normal file
21
routes/health.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HealthHandler struct {
|
||||||
|
Db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HealthHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var dbStatus int
|
||||||
|
if err := h.Db.DB().Ping(); err == nil {
|
||||||
|
dbStatus = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
w.Write([]byte(fmt.Sprintf("app=1\ndb=%d", dbStatus)))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user