diff --git a/cmd/main.go b/cmd/main.go index 371c38f..1792938 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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") - - router.SetupRoutes(app) - config.StartServer(app) + 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) } diff --git a/presentation/role_route.go b/presentation/role_route.go index d4652a1..1895e7f 100644 --- a/presentation/role_route.go +++ b/presentation/role_route.go @@ -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) }