[api/health] duplicate health endpoint to root

This commit is contained in:
Aleksandr Soloshenko 2024-05-24 00:47:08 +07:00
parent a14233194d
commit 4abe6f515a
7 changed files with 187 additions and 92 deletions

View File

@ -3,6 +3,9 @@
@mobileToken={{$dotenv MOBILE__TOKEN}}
@phone={{$dotenv PHONE}}
###
GET http://localhost:3000/health HTTP/1.1
###
GET {{baseUrl}}/api/3rdparty/v1/health HTTP/1.1

View File

@ -61,32 +61,6 @@
}
}
},
"/3rdparty/v1/health": {
"get": {
"description": "Checks if service is healthy",
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "Health check",
"responses": {
"200": {
"description": "Health check result",
"schema": {
"$ref": "#/definitions/smsgateway.HealthResponse"
}
},
"500": {
"description": "Service is unhealthy",
"schema": {
"$ref": "#/definitions/smsgateway.HealthResponse"
}
}
}
}
},
"/3rdparty/v1/message": {
"get": {
"security": [
@ -208,6 +182,58 @@
}
}
},
"/api/3rdparty/v1/health": {
"get": {
"description": "Checks if service is healthy",
"produces": [
"application/json"
],
"tags": [
"System"
],
"summary": "Health check",
"responses": {
"200": {
"description": "Health check result",
"schema": {
"$ref": "#/definitions/smsgateway.HealthResponse"
}
},
"500": {
"description": "Service is unhealthy",
"schema": {
"$ref": "#/definitions/smsgateway.HealthResponse"
}
}
}
}
},
"/health": {
"get": {
"description": "Checks if service is healthy",
"produces": [
"application/json"
],
"tags": [
"System"
],
"summary": "Health check",
"responses": {
"200": {
"description": "Health check result",
"schema": {
"$ref": "#/definitions/smsgateway.HealthResponse"
}
},
"500": {
"description": "Service is unhealthy",
"schema": {
"$ref": "#/definitions/smsgateway.HealthResponse"
}
}
}
}
},
"/mobile/v1/device": {
"post": {
"description": "Registers new device and returns credentials",

View File

@ -306,23 +306,6 @@ paths:
summary: List devices
tags:
- User
/3rdparty/v1/health:
get:
description: Checks if service is healthy
produces:
- application/json
responses:
"200":
description: Health check result
schema:
$ref: '#/definitions/smsgateway.HealthResponse'
"500":
description: Service is unhealthy
schema:
$ref: '#/definitions/smsgateway.HealthResponse'
summary: Health check
tags:
- User
/3rdparty/v1/message:
get:
description: Returns message state by ID
@ -402,6 +385,40 @@ paths:
tags:
- User
- Messages
/api/3rdparty/v1/health:
get:
description: Checks if service is healthy
produces:
- application/json
responses:
"200":
description: Health check result
schema:
$ref: '#/definitions/smsgateway.HealthResponse'
"500":
description: Service is unhealthy
schema:
$ref: '#/definitions/smsgateway.HealthResponse'
summary: Health check
tags:
- System
/health:
get:
description: Checks if service is healthy
produces:
- application/json
responses:
"200":
description: Health check result
schema:
$ref: '#/definitions/smsgateway.HealthResponse'
"500":
description: Service is unhealthy
schema:
$ref: '#/definitions/smsgateway.HealthResponse'
summary: Health check
tags:
- System
/mobile/v1/device:
patch:
consumes:

View File

@ -6,11 +6,9 @@ import (
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth"
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/health"
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/messages"
"github.com/capcom6/sms-gateway/internal/sms-gateway/repositories"
"github.com/capcom6/sms-gateway/internal/sms-gateway/services"
"github.com/capcom6/sms-gateway/pkg/maps"
"github.com/capcom6/sms-gateway/pkg/smsgateway"
"github.com/capcom6/sms-gateway/pkg/types"
"github.com/go-playground/validator/v10"
@ -27,10 +25,11 @@ const (
type ThirdPartyHandlerParams struct {
fx.In
HealthHandler *healthHandler
AuthSvc *auth.Service
MessagesSvc *messages.Service
DevicesSvc *services.DevicesService
HealthSvc *health.Service
Logger *zap.Logger
Validator *validator.Validate
@ -39,47 +38,11 @@ type ThirdPartyHandlerParams struct {
type thirdPartyHandler struct {
Handler
healthHandler *healthHandler
authSvc *auth.Service
messagesSvc *messages.Service
devicesSvc *services.DevicesService
healthSvc *health.Service
}
// @Summary Health check
// @Description Checks if service is healthy
// @Tags User
// @Produce json
// @Success 200 {object} smsgateway.HealthResponse "Health check result"
// @Failure 500 {object} smsgateway.HealthResponse "Service is unhealthy"
// @Router /3rdparty/v1/health [get]
//
// Health check
func (h *thirdPartyHandler) getHealth(c *fiber.Ctx) error {
check, err := h.healthSvc.HealthCheck(c.Context())
if err != nil {
return err
}
res := smsgateway.HealthResponse{
Status: smsgateway.HealthStatus(check.Status),
Checks: maps.MapValues(
check.Checks,
func(c health.CheckDetail) smsgateway.HealthCheck {
return smsgateway.HealthCheck{
Description: c.Description,
ObservedUnit: c.ObservedUnit,
ObservedValue: c.ObservedValue,
Status: smsgateway.HealthStatus(c.Status),
}
},
),
}
if check.Status == health.StatusFail {
return c.Status(fiber.StatusInternalServerError).JSON(res)
}
return c.Status(fiber.StatusOK).JSON(res)
}
// @Summary List devices
@ -214,6 +177,8 @@ func (h *thirdPartyHandler) authorize(handler func(models.User, *fiber.Ctx) erro
return fiber.ErrUnauthorized
}
c.Locals("user", user)
return handler(user, c)
}
}
@ -221,7 +186,7 @@ func (h *thirdPartyHandler) authorize(handler func(models.User, *fiber.Ctx) erro
func (h *thirdPartyHandler) Register(router fiber.Router) {
router = router.Group("/3rdparty/v1")
router.Get("/health", h.getHealth)
h.healthHandler.Register(router)
router.Use(basicauth.New(basicauth.Config{
Authorizer: func(username string, password string) bool {
@ -237,10 +202,10 @@ func (h *thirdPartyHandler) Register(router fiber.Router) {
func newThirdPartyHandler(params ThirdPartyHandlerParams) *thirdPartyHandler {
return &thirdPartyHandler{
Handler: Handler{Logger: params.Logger.Named("ThirdPartyHandler"), Validator: params.Validator},
authSvc: params.AuthSvc,
messagesSvc: params.MessagesSvc,
devicesSvc: params.DevicesSvc,
healthSvc: params.HealthSvc,
Handler: Handler{Logger: params.Logger.Named("ThirdPartyHandler"), Validator: params.Validator},
healthHandler: params.HealthHandler,
authSvc: params.AuthSvc,
messagesSvc: params.MessagesSvc,
devicesSvc: params.DevicesSvc,
}
}

View File

@ -0,0 +1,76 @@
package handlers
import (
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/health"
"github.com/capcom6/sms-gateway/pkg/maps"
"github.com/capcom6/sms-gateway/pkg/smsgateway"
"github.com/gofiber/fiber/v2"
"go.uber.org/fx"
"go.uber.org/zap"
)
type healthHanlderParams struct {
fx.In
HealthSvc *health.Service
Logger *zap.Logger
}
type healthHandler struct {
Handler
healthSvc *health.Service
logger *zap.Logger
}
// @Summary Health check
// @Description Checks if service is healthy
// @Tags System
// @Produce json
// @Success 200 {object} smsgateway.HealthResponse "Health check result"
// @Failure 500 {object} smsgateway.HealthResponse "Service is unhealthy"
// @Router /health [get]
// @Router /api/3rdparty/v1/health [get]
//
// Health check
func (h *healthHandler) getHealth(c *fiber.Ctx) error {
check, err := h.healthSvc.HealthCheck(c.Context())
if err != nil {
return err
}
res := smsgateway.HealthResponse{
Status: smsgateway.HealthStatus(check.Status),
Checks: maps.MapValues(
check.Checks,
func(c health.CheckDetail) smsgateway.HealthCheck {
return smsgateway.HealthCheck{
Description: c.Description,
ObservedUnit: c.ObservedUnit,
ObservedValue: c.ObservedValue,
Status: smsgateway.HealthStatus(c.Status),
}
},
),
}
if check.Status == health.StatusFail {
return c.Status(fiber.StatusInternalServerError).JSON(res)
}
return c.Status(fiber.StatusOK).JSON(res)
}
func (h *healthHandler) Register(router fiber.Router) {
router.Get("/health", h.getHealth)
}
func newHealthHandler(params healthHanlderParams) *healthHandler {
return &healthHandler{
Handler: Handler{Logger: params.Logger, Validator: nil},
healthSvc: params.HealthSvc,
logger: params.Logger,
}
}

View File

@ -17,4 +17,8 @@ var Module = fx.Module(
http.AsApiHandler(newMobileHandler),
http.AsApiHandler(newUpstreamHandler),
),
fx.Provide(
newHealthHandler,
fx.Private,
),
)

View File

@ -5,12 +5,16 @@ import (
)
type rootHandler struct {
healthHandler *healthHandler
}
func (h *rootHandler) Register(app *fiber.App) {
h.healthHandler.Register(app)
app.Static("/", "static")
}
func newRootHandler() *rootHandler {
return &rootHandler{}
func newRootHandler(healthHandler *healthHandler) *rootHandler {
return &rootHandler{
healthHandler: healthHandler,
}
}