-
Notifications
You must be signed in to change notification settings - Fork 44
/
cors.go
42 lines (35 loc) · 765 Bytes
/
cors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Package middleware contains:
//
// - CORS
// - Application firewall
// - Pongo2 template engine
// - JWT
// - Sentry logger
// - Two-factor auth validator
package middleware
// github.com/pilinux/gorest
// The MIT License (MIT)
// Copyright (c) 2022 pilinux
import (
"net/http"
"github.com/gin-gonic/gin"
)
// CORSPolicy struct to handle all policies
type CORSPolicy struct {
Key string
Value string
}
// CORS - Cross-Origin Resource Sharing
func CORS(cp []CORSPolicy) gin.HandlerFunc {
return func(c *gin.Context) {
for _, _cp := range cp {
c.Writer.Header().Set(_cp.Key, _cp.Value)
}
// required for browser-based HTTP clients
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}