Skip to content

Commit 64a375b

Browse files
committed
Adding cors header
1 parent 4b78e84 commit 64a375b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/gofr/http/middleware/cors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package middleware
2+
3+
import "net/http"
4+
5+
func CORS() func(inner http.Handler) http.Handler {
6+
return func(inner http.Handler) http.Handler {
7+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8+
9+
w.Header().Set("Access-Control-Allow-Origin", "*")
10+
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
11+
12+
if r.Method == http.MethodOptions {
13+
w.WriteHeader(http.StatusOK)
14+
return
15+
}
16+
17+
inner.ServeHTTP(w, r)
18+
})
19+
}
20+
}

pkg/gofr/http/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func NewRouter() *Router {
2121
muxRouter.Use(
2222
middleware.Tracer,
2323
middleware.Logging(logging.NewLogger(logging.INFO)),
24+
middleware.CORS(),
2425
)
2526

2627
return &Router{

0 commit comments

Comments
 (0)