-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ubuntu
committed
Jun 23, 2022
1 parent
1fa5db7
commit 72628a6
Showing
3 changed files
with
145 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,53 @@ | ||
# cors | ||
解决跨域插件 | ||
|
||
可以对单个API解决跨域问题: | ||
```go | ||
package main | ||
|
||
import "github.com/quixote-liu/cors" | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.HandlerFunc("/hello", cors.WarpH(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "hello, %s", "world") | ||
})) | ||
log.Fatal(http.ListenAndServe(":yourPort", mux)) | ||
} | ||
``` | ||
|
||
也可以对全局的请求设置cors跨域 | ||
```go | ||
package main | ||
|
||
import "github.com/quixote-liu/cors" | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.HandlerFunc("/hello", cors.WarpH(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "hello, %s", "world") | ||
})) | ||
log.Fatal(http.ListenAndServe(":yourPort", mux)) | ||
} | ||
|
||
|
||
type mux struct { | ||
*http.ServeMux | ||
} | ||
|
||
func NewServerMux() *mux { | ||
return &mux{ServeMux: http.NewServeMux()} | ||
} | ||
|
||
func (m *mux) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
status, ok := cors.Handler(w, r) | ||
if !ok { | ||
w.WriteHeader(status) | ||
return | ||
} | ||
m.ServeMux.ServeHTTP(w, r) | ||
} | ||
|
||
``` | ||
|
||
如果使用中间件,也可以类似的在中间件实现 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cors | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Client struct { | ||
allowMethods []string | ||
exposeHeaders []string | ||
|
||
// the default is 14400s(4 hours), the -1 disableds cache. | ||
maxAge string | ||
|
||
// set Cookies, the default is true. | ||
withCookies bool | ||
} | ||
|
||
func New() *Client { | ||
return &Client{ | ||
allowMethods: []string{"GET", "DELETE", "HEAD", "PATCH", "POST", "PUT"}, | ||
maxAge: "14400", | ||
withCookies: true, | ||
} | ||
} | ||
|
||
func (c *Client) WrapH(h http.HandlerFunc) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
status, ok := c.Handler(w, r) | ||
if !ok { | ||
w.WriteHeader(status) | ||
return | ||
} | ||
h(w, r) | ||
} | ||
} | ||
|
||
func (c *Client) Handler(w http.ResponseWriter, r *http.Request) (status int, isOptions bool) { | ||
origin := r.Header.Get("Origin") | ||
if origin == "" { | ||
origin = "*" | ||
} | ||
w.Header().Set("Access-Control-Allow-Origin", origin) | ||
|
||
if c.withCookies { | ||
w.Header().Set("Access-Control-Allow-Credentials", "true") | ||
} | ||
|
||
if r.Method == "OPTIONS" { | ||
w.Header().Set("Access-Control-Allow-Methods", strings.Join(c.allowMethods, ", ")) | ||
w.Header().Set("Access-Control-Allow-Headers", r.Header.Get("Access-Control-Request-Headers")) | ||
w.Header().Set("Access-Control-Max-Age", c.maxAge) | ||
return 204, false | ||
} | ||
|
||
if len(c.exposeHeaders) != 0 { | ||
w.Header().Set("Access-Control-Expose-Headers", strings.Join(c.exposeHeaders, ", ")) | ||
} | ||
return 0, true | ||
} | ||
|
||
func (c *Client) SetCookie(take bool) *Client { | ||
c.withCookies = take | ||
return c | ||
} | ||
|
||
func (c *Client) SetAllowMethods(methods []string) *Client { | ||
var mm []string | ||
copy(mm, methods) | ||
c.allowMethods = mm | ||
return c | ||
} | ||
|
||
func (c *Client) SetExposeHeaders(headers []string) *Client { | ||
var hh []string | ||
copy(hh, headers) | ||
c.exposeHeaders = headers | ||
return c | ||
} | ||
|
||
func (c *Client) SetMaxAge(maxAge int) *Client { | ||
if maxAge < 0 { | ||
maxAge = -1 | ||
} | ||
c.maxAge = strconv.FormatInt(int64(maxAge), 10) | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,13 @@ | ||
package cors | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
import "net/http" | ||
|
||
type Client struct { | ||
allowMethods []string | ||
exposeHeaders []string | ||
var cors = New() | ||
|
||
// the default is 14400s(4 hours), the -1 disableds cache. | ||
maxAge string | ||
|
||
// set Cookies, the default is true. | ||
withCookies bool | ||
} | ||
|
||
func New() *Client { | ||
return &Client{ | ||
allowMethods: []string{"GET", "DELETE", "HEAD", "PATCH", "POST", "PUT"}, | ||
maxAge: "14400", | ||
withCookies: true, | ||
} | ||
} | ||
|
||
func (c *Client) WrapH(h http.HandlerFunc) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
status, ok := c.Handler(w, r) | ||
if !ok { | ||
w.WriteHeader(status) | ||
return | ||
} | ||
h(w, r) | ||
} | ||
} | ||
|
||
func (c *Client) Handler(w http.ResponseWriter, r *http.Request) (status int, isOptions bool) { | ||
origin := r.Header.Get("Origin") | ||
if origin == "" { | ||
origin = "*" | ||
} | ||
w.Header().Set("Access-Control-Allow-Origin", origin) | ||
|
||
if c.withCookies { | ||
w.Header().Set("Access-Control-Allow-Credentials", "true") | ||
} | ||
|
||
if r.Method == "OPTIONS" { | ||
w.Header().Set("Access-Control-Allow-Methods", strings.Join(c.allowMethods, ", ")) | ||
w.Header().Set("Access-Control-Allow-Headers", r.Header.Get("Access-Control-Request-Headers")) | ||
w.Header().Set("Access-Control-Max-Age", c.maxAge) | ||
return 204, false | ||
} | ||
|
||
if len(c.exposeHeaders) != 0 { | ||
w.Header().Set("Access-Control-Expose-Headers", strings.Join(c.exposeHeaders, ", ")) | ||
} | ||
return 0, true | ||
} | ||
|
||
func (c *Client) SetCookie(take bool) *Client { | ||
c.withCookies = take | ||
return c | ||
} | ||
|
||
func (c *Client) SetAllowMethods(methods []string) *Client { | ||
var mm []string | ||
copy(mm, methods) | ||
c.allowMethods = mm | ||
return c | ||
} | ||
|
||
func (c *Client) SetExposeHeaders(headers []string) *Client { | ||
var hh []string | ||
copy(hh, headers) | ||
c.exposeHeaders = headers | ||
return c | ||
func Handler(w http.ResponseWriter, r *http.Request) (status int, isOption bool) { | ||
return cors.Handler(w, r) | ||
} | ||
|
||
func (c *Client) SetMaxAge(maxAge int) *Client { | ||
if maxAge < 0 { | ||
maxAge = -1 | ||
} | ||
c.maxAge = strconv.FormatInt(int64(maxAge), 10) | ||
return c | ||
func WrapH(h http.HandlerFunc) http.HandlerFunc { | ||
return cors.WrapH(h) | ||
} |