Return 501 Not Implemented on unknown API routes
This commit is contained in:
parent
0567619408
commit
a06d0df142
@ -1,11 +1,8 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.javil.eu/jacob1123/budgeteer"
|
"git.javil.eu/jacob1123/budgeteer"
|
||||||
@ -51,10 +48,10 @@ func (h *Handler) LoadRoutes(router *echo.Echo) {
|
|||||||
authenticated := api.Group("")
|
authenticated := api.Group("")
|
||||||
{
|
{
|
||||||
authenticated.Use(h.verifyLoginWithForbidden)
|
authenticated.Use(h.verifyLoginWithForbidden)
|
||||||
authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount)
|
account := authenticated.Group("/account")
|
||||||
authenticated.POST("/account/:accountid/reconcile", h.reconcileTransactions)
|
account.GET("/:accountid/transactions", h.transactionsForAccount)
|
||||||
authenticated.POST("/account/:accountid", h.editAccount)
|
account.POST("/:accountid/reconcile", h.reconcileTransactions)
|
||||||
authenticated.GET("/admin/clear-database", h.clearDatabase)
|
account.POST("/:accountid", h.editAccount)
|
||||||
|
|
||||||
budget := authenticated.Group("/budget")
|
budget := authenticated.Group("/budget")
|
||||||
budget.POST("/new", h.newBudget)
|
budget.POST("/new", h.newBudget)
|
||||||
@ -75,44 +72,16 @@ func (h *Handler) LoadRoutes(router *echo.Echo) {
|
|||||||
transaction := authenticated.Group("/transaction")
|
transaction := authenticated.Group("/transaction")
|
||||||
transaction.POST("/new", h.newTransaction)
|
transaction.POST("/new", h.newTransaction)
|
||||||
transaction.POST("/:transactionid", h.updateTransaction)
|
transaction.POST("/:transactionid", h.updateTransaction)
|
||||||
}
|
|
||||||
|
authenticated.GET("/admin/clear-database", h.clearDatabase)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) ServeStatic(next echo.HandlerFunc) echo.HandlerFunc {
|
api.Any("/*", h.notFound)
|
||||||
return func(c echo.Context) error {
|
|
||||||
h.ServeStaticFile(c, c.Path())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) ServeStaticFile(c echo.Context, fullPath string) {
|
func (h *Handler) notFound(c echo.Context) error {
|
||||||
file, err := h.StaticFS.Open(fullPath)
|
fmt.Println("not found?")
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
return echo.NewHTTPError(http.StatusNotImplemented, "not found")
|
||||||
h.ServeStaticFile(c, path.Join("/", "/index.html"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
stat, err := file.Stat()
|
|
||||||
if err != nil {
|
|
||||||
c.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if stat.IsDir() {
|
|
||||||
h.ServeStaticFile(c, path.Join(fullPath, "index.html"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if file, ok := file.(io.ReadSeeker); ok {
|
|
||||||
http.ServeContent(c.Response().Writer, c.Request(), stat.Name(), stat.ModTime(), file)
|
|
||||||
} else {
|
|
||||||
panic("File does not implement ReadSeeker")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func enableCachingForStaticFiles() echo.MiddlewareFunc {
|
func enableCachingForStaticFiles() echo.MiddlewareFunc {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user