cors management and role showing in public access

This commit is contained in:
pahmiudahgede 2025-03-07 23:17:31 +07:00
parent 103b5b0418
commit 2072d047d4
2 changed files with 29 additions and 9 deletions

View File

@ -2,17 +2,39 @@ package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/pahmiudahgede/senggoldong/config"
"github.com/pahmiudahgede/senggoldong/router"
)
func main() {
config.SetupConfig()
app := fiber.New()
// app.Static(utils.BaseUrl+"/uploads", "./public"+utils.BaseUrl+"/uploads")
app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:3000",
AllowMethods: "GET,POST,PUT,DELETE,OPTIONS",
AllowHeaders: "Origin, Content-Type, Accept, Authorization, x-api-key",
AllowCredentials: true,
}))
app.Use(func(c *fiber.Ctx) error {
c.Set("Access-Control-Allow-Origin", "http://localhost:3000")
c.Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
c.Set("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization, x-api-key")
c.Set("Access-Control-Allow-Credentials", "true")
return c.Next()
})
app.Options("*", func(c *fiber.Ctx) error {
c.Set("Access-Control-Allow-Origin", "http://localhost:3000")
c.Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
c.Set("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization, x-api-key")
c.Set("Access-Control-Allow-Credentials", "true")
return c.SendStatus(fiber.StatusNoContent)
})
router.SetupRoutes(app)
config.StartServer(app)
config.StartServer(app)
}

View File

@ -6,8 +6,6 @@ import (
"github.com/pahmiudahgede/senggoldong/internal/handler"
"github.com/pahmiudahgede/senggoldong/internal/repositories"
"github.com/pahmiudahgede/senggoldong/internal/services"
"github.com/pahmiudahgede/senggoldong/middleware"
"github.com/pahmiudahgede/senggoldong/utils"
)
func RoleRouter(api fiber.Router) {
@ -15,6 +13,6 @@ func RoleRouter(api fiber.Router) {
roleService := services.NewRoleService(roleRepo)
roleHandler := handler.NewRoleHandler(roleService)
api.Get("/roles", middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator), roleHandler.GetRoles)
api.Get("/role/:role_id", middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator), roleHandler.GetRoleByID)
api.Get("/roles", roleHandler.GetRoles)
api.Get("/role/:role_id", roleHandler.GetRoleByID)
}