2021-12-03 03:11:01 -08:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-03 05:18:40 -08:00
|
|
|
"goweb/middleware"
|
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/labstack/echo-contrib/session"
|
|
|
|
"github.com/labstack/echo/v4"
|
2021-12-03 05:18:40 -08:00
|
|
|
echomw "github.com/labstack/echo/v4/middleware"
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
"goweb/container"
|
|
|
|
"goweb/controllers"
|
|
|
|
)
|
|
|
|
|
|
|
|
const StaticDir = "static"
|
|
|
|
|
|
|
|
func BuildRouter(c *container.Container) {
|
|
|
|
// Middleware
|
2021-12-03 05:18:40 -08:00
|
|
|
c.Web.Use(echomw.RemoveTrailingSlashWithConfig(echomw.TrailingSlashConfig{
|
2021-12-03 03:11:01 -08:00
|
|
|
RedirectCode: http.StatusMovedPermanently,
|
|
|
|
}))
|
2021-12-03 05:18:40 -08:00
|
|
|
c.Web.Use(echomw.RequestID())
|
|
|
|
c.Web.Use(echomw.Recover())
|
|
|
|
c.Web.Use(echomw.Gzip())
|
|
|
|
c.Web.Use(echomw.Logger())
|
2021-12-03 03:11:01 -08:00
|
|
|
c.Web.Use(session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))))
|
2021-12-03 05:18:40 -08:00
|
|
|
c.Web.Use(echomw.CSRFWithConfig(echomw.CSRFConfig{
|
2021-12-03 04:53:01 -08:00
|
|
|
TokenLookup: "form:csrf",
|
|
|
|
}))
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-03 05:06:06 -08:00
|
|
|
// Static files with proper cache control
|
|
|
|
// funcmap.File() should be used in templates to append a cache key to the URL in order to break cache
|
|
|
|
// after each server restart
|
2021-12-03 05:18:40 -08:00
|
|
|
c.Web.Group("", middleware.CacheControl(15552000)).
|
|
|
|
Static("/", StaticDir)
|
2021-12-03 05:06:06 -08:00
|
|
|
|
2021-12-03 03:11:01 -08:00
|
|
|
// Base controller
|
|
|
|
ctr := controllers.NewController(c)
|
|
|
|
|
|
|
|
// Error handler
|
|
|
|
err := controllers.Error{Controller: ctr}
|
|
|
|
c.Web.HTTPErrorHandler = err.Get
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
navRoutes(c.Web, ctr)
|
|
|
|
userRoutes(c.Web, ctr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func navRoutes(e *echo.Echo, ctr controllers.Controller) {
|
|
|
|
home := controllers.Home{Controller: ctr}
|
|
|
|
e.GET("/", home.Get).Name = "home"
|
|
|
|
|
|
|
|
about := controllers.About{Controller: ctr}
|
|
|
|
e.GET("/about", about.Get).Name = "about"
|
|
|
|
|
|
|
|
contact := controllers.Contact{Controller: ctr}
|
|
|
|
e.GET("/contact", contact.Get).Name = "contact"
|
|
|
|
e.POST("/contact", contact.Post).Name = "contact.post"
|
|
|
|
}
|
|
|
|
|
|
|
|
func userRoutes(e *echo.Echo, ctr controllers.Controller) {
|
|
|
|
// TODO
|
|
|
|
}
|