scaffold-goweb/internal/route/mount.go.gotpl

35 lines
697 B
Plaintext
Raw Normal View History

2020-02-19 22:13:06 +01:00
package route
import (
"{{ .ProjectNamespace }}/internal/config"
"github.com/go-chi/chi"
"github.com/gorilla/csrf"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/static"
"gitlab.com/wpetit/goweb/session/gorilla"
)
func Mount(r *chi.Mux, config *config.Config) error {
csrfSecret, err := gorilla.GenerateRandomBytes(32)
if err != nil {
return errors.Wrap(err, "could not generate CSRF secret")
}
csrfMiddleware := csrf.Protect(
csrfSecret,
csrf.Secure(false),
)
r.Group(func(r chi.Router) {
r.Use(csrfMiddleware)
r.Get("/", serveHomePage)
})
notFoundHandler := r.NotFoundHandler()
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler))
return nil
}