This repository was archived by the owner on Feb 28, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ import (
1414func SetupRoutes (app * fiber.App , assets embed.FS ) {
1515 api := app .Group ("/api/v1" )
1616 {
17+ api .Use (middlewares .APIResponseMiddleware ())
1718 api .Get ("health" , healthCheck )
1819 userGroup := api .Group ("user" )
1920 {
Original file line number Diff line number Diff line change 11package middlewares
22
33import (
4+ "encoding/json"
5+ "errors"
46 "strings"
57
68 "github.com/gofiber/fiber/v2"
@@ -13,6 +15,41 @@ const (
1315 USER_SESSION = "USER_SESSION"
1416)
1517
18+ func APIResponseMiddleware () func (c * fiber.Ctx ) error {
19+ return func (c * fiber.Ctx ) error {
20+ err := c .Next ()
21+ if err != nil {
22+ code := fiber .StatusInternalServerError
23+ var e * fiber.Error
24+ if errors .As (err , & e ) {
25+ code = e .Code
26+ }
27+ return c .Status (code ).JSON (map [string ]interface {}{
28+ "success" : false ,
29+ "error" : err .Error (),
30+ })
31+ }
32+ if err == nil {
33+ response := c .Response ()
34+ body := response .Body ()
35+ var data interface {}
36+ json .Unmarshal (body , & data )
37+
38+ if resMap , ok := data .(map [string ]interface {}); ok {
39+ if _ , ok := resMap ["success" ]; ok {
40+ return c .JSON (resMap )
41+ }
42+ }
43+
44+ return c .JSON (map [string ]interface {}{
45+ "success" : true ,
46+ "data" : data ,
47+ })
48+ }
49+ return nil
50+ }
51+ }
52+
1653// FindUserMiddleware is to find authenticated user before sending the request to next handler
1754func FindUserMiddleware () func (c * fiber.Ctx ) error {
1855 return func (c * fiber.Ctx ) error {
You can’t perform that action at this time.
0 commit comments