Skip to content

Instantly share code, notes, and snippets.

@alinz
Created July 7, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alinz/39357f24e18a9402d83e3ad28d21a448 to your computer and use it in GitHub Desktop.
Save alinz/39357f24e18a9402d83e3ad28d21a448 to your computer and use it in GitHub Desktop.
router with cors
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/goware/cors"
)
func New() chi.Router {
cors := cors.New(cors.Options{
AllowedOrigins: []string{
"http://localhost",
"null",
},
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 := chi.NewRouter()
// global middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(cors.Handler)
// added health check for
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
json.Response(w, struct {
Status string `json:"status"`
}{Status: "OK"}, http.StatusOK)
})
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment