Skip to content

Commit b2b9f53

Browse files
committed
CORS added
1 parent 495963c commit b2b9f53

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func main() {
1515
app := config.App
1616

1717
// middlewares
18+
app.Use(middlewares.CorsHandler)
1819
app.Use(middlewares.SimpleLogger)
1920

2021
// routes mapping using sub-groups

server/middlewares/cors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package middlewares
2+
3+
import "net/http"
4+
5+
func CorsHandler(next http.Handler) http.Handler {
6+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7+
// Allows all origins for development; can be bound to specific domains via env later
8+
w.Header().Set("Access-Control-Allow-Origin", "*")
9+
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
10+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
11+
w.Header().Set("Access-Control-Expose-Headers", "Content-Length")
12+
13+
if r.Method == "OPTIONS" {
14+
w.WriteHeader(http.StatusNoContent)
15+
return
16+
}
17+
18+
next.ServeHTTP(w, r)
19+
})
20+
}

0 commit comments

Comments
 (0)