Skip to content

Commit

Permalink
adding middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
p-shubh committed Jun 26, 2024
1 parent 477d35a commit 14fd293
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package middleware

import "github.com/gin-gonic/gin"

func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")

if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}

c.Next()
}
}
6 changes: 6 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package server

import (
"app.myriadflow.com/controllers"
"app.myriadflow.com/middleware"
"github.com/gin-gonic/gin"
)

func Router() {
router := gin.Default()
gin.SetMode(gin.DebugMode)

// adding middleware server
router.Use(middleware.CORSMiddleware())

// health check
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
Expand Down

0 comments on commit 14fd293

Please sign in to comment.