bill-server-go/internal/misc/handler.go

39 lines
819 B
Go
Raw Normal View History

2023-05-05 20:27:33 +08:00
package misc
2023-06-01 20:25:54 +08:00
import (
"github.com/gofiber/fiber/v2"
"os"
"time"
)
2023-05-05 20:27:33 +08:00
// Create a handler. Leave this empty, as we have no domains nor use-cases.
type MiscHandler struct{}
// Represents a new handler.
func NewMiscHandler(miscRoute fiber.Router) {
handler := &MiscHandler{}
// Declare routing.
miscRoute.Get("", handler.healthCheck)
2023-06-01 20:25:54 +08:00
miscRoute.Get("/shutdown", handler.shutdown)
2023-05-05 20:27:33 +08:00
}
// Check for the health of the API.
func (h *MiscHandler) healthCheck(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
"status": "success",
"message": "Hello World!",
})
}
2023-06-01 20:25:54 +08:00
func (h *MiscHandler) shutdown(ctx *fiber.Ctx) error {
go func() {
time.Sleep(1 * time.Second)
os.Exit(0)
}()
return ctx.Status(fiber.StatusOK).JSON(&fiber.Map{
"status": "success",
"message": "server shutdown",
})
}