From 74caf53fe9d61f092e7b542b6a18c8be3aed0032 Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Fri, 28 Jul 2017 13:58:32 -0400 Subject: [PATCH] Include _example program --- _example/main.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 _example/main.go diff --git a/_example/main.go b/_example/main.go new file mode 100644 index 0000000..fd8bc02 --- /dev/null +++ b/_example/main.go @@ -0,0 +1,84 @@ +// cors example +// +// ie. +// +// Unsuccessful Preflight request: +// =============================== +// $ curl -i http://localhost:3000/ -H "Origin: http://no.com" -H "Access-Control-Request-Method: GET" -X OPTIONS +// HTTP/1.1 200 OK +// Vary: Origin +// Vary: Access-Control-Request-Method +// Vary: Access-Control-Request-Headers +// Date: Fri, 28 Jul 2017 17:55:47 GMT +// Content-Length: 0 +// Content-Type: text/plain; charset=utf-8 +// +// +// Successful Preflight request: +// ============================= +// $ curl -i http://localhost:3000/ -H "Origin: http://example.com" -H "Access-Control-Request-Method: GET" -X OPTIONS +// HTTP/1.1 200 OK +// Access-Control-Allow-Credentials: true +// Access-Control-Allow-Methods: GET +// Access-Control-Allow-Origin: http://example.com +// Access-Control-Max-Age: 300 +// Vary: Origin +// Vary: Access-Control-Request-Method +// Vary: Access-Control-Request-Headers +// Date: Fri, 28 Jul 2017 17:56:44 GMT +// Content-Length: 0 +// Content-Type: text/plain; charset=utf-8 +// +// +// Content request (after a successful preflight): +// =============================================== +// $ curl -i http://localhost:3000/ -H "Origin: http://example.com" +// HTTP/1.1 200 OK +// Access-Control-Allow-Credentials: true +// Access-Control-Allow-Origin: http://example.com +// Access-Control-Expose-Headers: Link +// Vary: Origin +// Date: Fri, 28 Jul 2017 17:57:52 GMT +// Content-Length: 7 +// Content-Type: text/plain; charset=utf-8 +// +// welcome% +// +package main + +import ( + "net/http" + + "github.com/go-chi/chi" + "github.com/go-chi/cors" +) + +func main() { + r := chi.NewRouter() + + // Basic CORS + // for more ideas, see: https://developer.github.com/v3/#cross-origin-resource-sharing + cors := cors.New(cors.Options{ + AllowOriginFunc: AllowOriginFunc, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: true, + MaxAge: 300, // Maximum value not ignored by any of major browsers + }) + r.Use(cors.Handler) + + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("welcome")) + }) + + http.ListenAndServe(":3000", r) +} + +func AllowOriginFunc(r *http.Request, origin string) bool { + if origin == "http://example.com" { + return true + } + + return false +}