This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoc.go
79 lines (58 loc) · 1.93 KB
/
doc.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Package cors is a handler for the core (https://godoc.org/github.com/volatile/core).
It provides Cross-Origin Resource Sharing support.
Usage
When using CORS (globally or locally), there is always a parameter of type OriginsMap.
It can contain a map of allowed origins and their specific options.
Use nil to allow all headers, methods and origins:
cors.Use(nil)
Use nil for origin to allow all headers and methods for this origin.
cors.Use(&cors.OriginsMap{
"example.com": nil,
})
Use AllOrigins to set options for all origins.
cors.Use(&cors.OriginsMap{
"example.com": nil, // All is allowed for this origin.
cors.AllOrigins: &cors.Options{
AllowedMethods: []string{"GET"}, // Only the GET method is allowed for the others.
},
})
Global usage
Use sets a global CORS configuration for all the handlers downstream:
cors.Use(nil)
// All is allowed for the root path.
core.Use(func(c *core.Context) {
if c.Request.URL.Path == "/" {
fmt.Fprint(c.ResponseWriter, "Hello, World!")
} else {
c.Next()
}
})
// Previous CORS options are overwritten.
cors.Use(&cors.OriginsMap{
cors.AllOrigins: &cors.Options{
AllowedMethods: []string{"GET"},
},
})
// Only the GET method is allowed for this handler.
core.Use(func(c *core.Context) {
fmt.Fprint(c.ResponseWriter, "Read only")
})
Make sure to include the handler above any other handler that alter the response body.
Local usage
LocalUse sets CORS locally, inside a single handler.
This setting takes precedence over he global CORS options (if set).
core.Use(func(c *core.Context) {
cors.LocalUse(c, &cors.OriginsMap{
cors.AllOrigins: &cors.Options{AllowedMethods: []string{"GET"}},
}, func() {
response.Status(c, http.StatusOK)
})
})
Documentation
For more information:
W3C http://www.w3.org/TR/cors/
Mozilla https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
HTML5 Rocks http://www.html5rocks.com/en/tutorials/cors/
*/
package cors