feat: add shutdown url

This commit is contained in:
Liangzhao Che 2023-06-01 20:25:54 +08:00
parent d37f178ddd
commit 3473adabbc
2 changed files with 19 additions and 2 deletions

View File

@ -1,7 +1,7 @@
version: "3.9" version: "3.9"
services: services:
fiber-application: fiber-application:
restart: always restart: on-failure
image: docker-mariadb-clean-arch:latest image: docker-mariadb-clean-arch:latest
build: build:
context: . context: .
@ -10,6 +10,7 @@ services:
ports: ports:
- 8080:8080 - 8080:8080
environment: environment:
- ENV=prod
- API_USERID=1 - API_USERID=1
- API_USERNAME=fiber - API_USERNAME=fiber
- API_PASSWORD=fiber - API_PASSWORD=fiber

View File

@ -1,6 +1,10 @@
package misc package misc
import "github.com/gofiber/fiber/v2" import (
"github.com/gofiber/fiber/v2"
"os"
"time"
)
// Create a handler. Leave this empty, as we have no domains nor use-cases. // Create a handler. Leave this empty, as we have no domains nor use-cases.
type MiscHandler struct{} type MiscHandler struct{}
@ -11,6 +15,7 @@ func NewMiscHandler(miscRoute fiber.Router) {
// Declare routing. // Declare routing.
miscRoute.Get("", handler.healthCheck) miscRoute.Get("", handler.healthCheck)
miscRoute.Get("/shutdown", handler.shutdown)
} }
// Check for the health of the API. // Check for the health of the API.
@ -20,3 +25,14 @@ func (h *MiscHandler) healthCheck(c *fiber.Ctx) error {
"message": "Hello World!", "message": "Hello World!",
}) })
} }
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",
})
}