packagemainimport (
"github.com/go-chi/chi""github.com/rs/cors""github.com/unrolled/secure""net/http""fmt")
funcmain() {
r:=chi.NewRouter()
// Handlers which are set with Use will proceed requests before processing of Handler functions are sets HandleFunc, Handle, Route.// In short, these are middlewares.r.Use(newCors().Handler)
r.Use(newSecure().Handler)
r.HandleFunc("/test", test_handle)
r.Route("/route", func(rchi.Router) {
r.Get("/test", test_route)
})
http.ListenAndServe(":8080", r)
}
functest_handle(whttp.ResponseWriter, r*http.Request) {
fmt.Fprint(w, "Hello from test_handle.")
return}
functest_route(whttp.ResponseWriter, r*http.Request) {
fmt.Fprint(w, "Hello from test_route.")
return}
funcnewCors() *cors.Cors {
returncors.New(cors.Options{
AllowCredentials: true,
// ... And more options you want })
}
funcnewSecure() *secure.Secure {
returnsecure.New(secure.Options{
BrowserXssFilter: true,
// ... And more options you want })
}