Start migration to echo
This commit is contained in:
@ -11,7 +11,8 @@ import (
|
||||
"git.javil.eu/jacob1123/budgeteer"
|
||||
"git.javil.eu/jacob1123/budgeteer/bcrypt"
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Handler handles incoming requests.
|
||||
@ -24,10 +25,10 @@ type Handler struct {
|
||||
|
||||
// Serve starts the http server.
|
||||
func (h *Handler) Serve() {
|
||||
router := gin.Default()
|
||||
router := echo.New()
|
||||
h.LoadRoutes(router)
|
||||
|
||||
if err := router.Run(":1323"); err != nil {
|
||||
if err := router.Start(":1323"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -41,9 +42,9 @@ type SuccessResponse struct {
|
||||
}
|
||||
|
||||
// LoadRoutes initializes all the routes.
|
||||
func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
func (h *Handler) LoadRoutes(router *echo.Echo) {
|
||||
router.Use(enableCachingForStaticFiles())
|
||||
router.NoRoute(h.ServeStatic)
|
||||
router.Use(h.ServeStatic)
|
||||
|
||||
api := router.Group("/api/v1")
|
||||
|
||||
@ -81,11 +82,14 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeStatic(c *gin.Context) {
|
||||
h.ServeStaticFile(c, c.Request.URL.Path)
|
||||
func (h *Handler) ServeStatic(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
h.ServeStaticFile(c, c.Path())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) ServeStaticFile(c *gin.Context, fullPath string) {
|
||||
func (h *Handler) ServeStaticFile(c echo.Context, fullPath string) {
|
||||
file, err := h.StaticFS.Open(fullPath)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
h.ServeStaticFile(c, path.Join("/", "/index.html"))
|
||||
@ -93,13 +97,13 @@ func (h *Handler) ServeStaticFile(c *gin.Context, fullPath string) {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
stat, err := file.Stat()
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -109,16 +113,20 @@ func (h *Handler) ServeStaticFile(c *gin.Context, fullPath string) {
|
||||
}
|
||||
|
||||
if file, ok := file.(io.ReadSeeker); ok {
|
||||
http.ServeContent(c.Writer, c.Request, stat.Name(), stat.ModTime(), file)
|
||||
http.ServeContent(c.Response().Writer, c.Request(), stat.Name(), stat.ModTime(), file)
|
||||
} else {
|
||||
panic("File does not implement ReadSeeker")
|
||||
}
|
||||
}
|
||||
|
||||
func enableCachingForStaticFiles() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.RequestURI, "/static/") {
|
||||
c.Header("Cache-Control", "max-age=86400")
|
||||
func enableCachingForStaticFiles() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if strings.HasPrefix(c.Path(), "/static/") {
|
||||
c.Response().Header().Set("Cache-Control", "max-age=86400")
|
||||
}
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user