|
| 1 | +/* Demonstrate how to implement a custom CORS middleware, used to on all endpoints. |
| 2 | +
|
| 3 | +The curl demo: |
| 4 | +
|
| 5 | + curl -i http://127.0.0.1:8080/countries |
| 6 | +
|
| 7 | +*/ |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "github.com/ant0ine/go-json-rest/rest" |
| 12 | + "net/http" |
| 13 | +) |
| 14 | + |
| 15 | +type MyCorsMiddleware struct{} |
| 16 | + |
| 17 | +func (mw *MyCorsMiddleware) MiddlewareFunc(handler rest.HandlerFunc) rest.HandlerFunc { |
| 18 | + return func(writer rest.ResponseWriter, request *rest.Request) { |
| 19 | + |
| 20 | + corsInfo := request.GetCorsInfo() |
| 21 | + |
| 22 | + // Be nice with non CORS requests, continue |
| 23 | + // Alternatively, you may also chose to only allow CORS requests, and return an error. |
| 24 | + if !corsInfo.IsCors { |
| 25 | + // continure, execute the wrapped middleware |
| 26 | + handler(writer, request) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + // Validate the Origin |
| 31 | + // More sophisticated validations can be implemented, regexps, DB lookups, ... |
| 32 | + if corsInfo.Origin != "http://my.other.host" { |
| 33 | + rest.Error(writer, "Invalid Origin", http.StatusForbidden) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + if corsInfo.IsPreflight { |
| 38 | + // check the request methods |
| 39 | + allowedMethods := map[string]bool{ |
| 40 | + "GET": true, |
| 41 | + "POST": true, |
| 42 | + "PUT": true, |
| 43 | + // don't allow DELETE, for instance |
| 44 | + } |
| 45 | + if !allowedMethods[corsInfo.AccessControlRequestMethod] { |
| 46 | + rest.Error(writer, "Invalid Preflight Request", http.StatusForbidden) |
| 47 | + return |
| 48 | + } |
| 49 | + // check the request headers |
| 50 | + allowedHeaders := map[string]bool{ |
| 51 | + "Accept": true, |
| 52 | + "Content-Type": true, |
| 53 | + "X-Custom-Header": true, |
| 54 | + } |
| 55 | + for _, requestedHeader := range corsInfo.AccessControlRequestHeaders { |
| 56 | + if !allowedHeaders[requestedHeader] { |
| 57 | + rest.Error(writer, "Invalid Preflight Request", http.StatusForbidden) |
| 58 | + return |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + for allowedMethod, _ := range allowedMethods { |
| 63 | + writer.Header().Add("Access-Control-Allow-Methods", allowedMethod) |
| 64 | + } |
| 65 | + for allowedHeader, _ := range allowedHeaders { |
| 66 | + writer.Header().Add("Access-Control-Allow-Headers", allowedHeader) |
| 67 | + } |
| 68 | + writer.Header().Set("Access-Control-Allow-Origin", corsInfo.Origin) |
| 69 | + writer.Header().Set("Access-Control-Allow-Credentials", "true") |
| 70 | + writer.Header().Set("Access-Control-Max-Age", "3600") |
| 71 | + writer.WriteHeader(http.StatusOK) |
| 72 | + return |
| 73 | + } else { |
| 74 | + writer.Header().Set("Access-Control-Expose-Headers", "X-Powered-By") |
| 75 | + writer.Header().Set("Access-Control-Allow-Origin", corsInfo.Origin) |
| 76 | + writer.Header().Set("Access-Control-Allow-Credentials", "true") |
| 77 | + // continure, execute the wrapped middleware |
| 78 | + handler(writer, request) |
| 79 | + return |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func main() { |
| 85 | + |
| 86 | + handler := rest.ResourceHandler{ |
| 87 | + PreRoutingMiddlewares: []rest.Middleware{ |
| 88 | + &MyCorsMiddleware{}, |
| 89 | + }, |
| 90 | + } |
| 91 | + handler.SetRoutes( |
| 92 | + &rest.Route{"GET", "/countries", GetAllCountries}, |
| 93 | + ) |
| 94 | + http.ListenAndServe(":8080", &handler) |
| 95 | +} |
| 96 | + |
| 97 | +type Country struct { |
| 98 | + Code string |
| 99 | + Name string |
| 100 | +} |
| 101 | + |
| 102 | +func GetAllCountries(w rest.ResponseWriter, r *rest.Request) { |
| 103 | + w.WriteJson( |
| 104 | + []Country{ |
| 105 | + Country{ |
| 106 | + Code: "FR", |
| 107 | + Name: "France", |
| 108 | + }, |
| 109 | + Country{ |
| 110 | + Code: "US", |
| 111 | + Name: "United States", |
| 112 | + }, |
| 113 | + }, |
| 114 | + ) |
| 115 | +} |
0 commit comments