initial commit

This commit is contained in:
Hafizh A. Y
2025-09-25 10:46:46 +07:00
parent c43544e5e8
commit 10506238ae
64 changed files with 3564 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
package middleware
import (
"strings"
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/config"
service "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/users/services"
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/utils"
"github.com/gofiber/fiber/v2"
)
func Auth(userService service.UserService, requiredRights ...string) fiber.Handler {
return func(c *fiber.Ctx) error {
authHeader := c.Get("Authorization")
token := strings.TrimSpace(strings.TrimPrefix(authHeader, "Bearer "))
if token == "" {
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
}
userID, err := utils.VerifyToken(token, config.JWTSecret, config.TokenTypeAccess)
if err != nil {
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
}
user, err := userService.GetOne(c, userID)
if err != nil || user == nil {
return fiber.NewError(fiber.StatusUnauthorized, "Please authenticate")
}
c.Locals("user", user)
// if len(requiredRights) > 0 {
// userRights, hasRights := config.RoleRights[user.Role]
// if (!hasRights || !hasAllRights(userRights, requiredRights)) && c.Params("userId") != userID {
// return fiber.NewError(fiber.StatusForbidden, "You don't have permission to access this resource")
// }
// }
return c.Next()
}
}
// func hasAllRights(userRights, requiredRights []string) bool {
// rightSet := make(map[string]struct{}, len(userRights))
// for _, right := range userRights {
// rightSet[right] = struct{}{}
// }
// for _, right := range requiredRights {
// if _, exists := rightSet[right]; !exists {
// return false
// }
// }
// return true
// }
+12
View File
@@ -0,0 +1,12 @@
package middleware
import (
jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
)
func JwtConfig() fiber.Handler {
return jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{Key: []byte("secret")},
})
}
+26
View File
@@ -0,0 +1,26 @@
package middleware
import (
"time"
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/response"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/limiter"
)
func LimiterConfig() fiber.Handler {
return limiter.New(limiter.Config{
Max: 20,
Expiration: 15 * time.Minute,
LimitReached: func(c *fiber.Ctx) error {
return c.Status(fiber.StatusTooManyRequests).
JSON(response.Common{
Code: fiber.StatusTooManyRequests,
Status: "error",
Message: "Too many requests, please try again later",
})
},
SkipSuccessfulRequests: true,
})
}
+13
View File
@@ -0,0 +1,13 @@
package middleware
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
)
func LoggerConfig() fiber.Handler {
return logger.New(logger.Config{
Format: "${time} ${method} ${status} ${path} in ${latency}\n",
TimeFormat: "15:04:05.00",
})
}
+12
View File
@@ -0,0 +1,12 @@
package middleware
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func RecoverConfig() fiber.Handler {
return recover.New(recover.Config{
EnableStackTrace: true,
})
}