Return 501 Not Implemented on unknown API routes

This commit is contained in:
Jan Bader 2022-09-11 21:01:54 +00:00
parent 0567619408
commit a06d0df142

View File

@ -1,11 +1,8 @@
package server
import (
"errors"
"io"
"io/fs"
"fmt"
"net/http"
"path"
"strings"
"git.javil.eu/jacob1123/budgeteer"
@ -51,10 +48,10 @@ func (h *Handler) LoadRoutes(router *echo.Echo) {
authenticated := api.Group("")
{
authenticated.Use(h.verifyLoginWithForbidden)
authenticated.GET("/account/:accountid/transactions", h.transactionsForAccount)
authenticated.POST("/account/:accountid/reconcile", h.reconcileTransactions)
authenticated.POST("/account/:accountid", h.editAccount)
authenticated.GET("/admin/clear-database", h.clearDatabase)
account := authenticated.Group("/account")
account.GET("/:accountid/transactions", h.transactionsForAccount)
account.POST("/:accountid/reconcile", h.reconcileTransactions)
account.POST("/:accountid", h.editAccount)
budget := authenticated.Group("/budget")
budget.POST("/new", h.newBudget)
@ -75,44 +72,16 @@ func (h *Handler) LoadRoutes(router *echo.Echo) {
transaction := authenticated.Group("/transaction")
transaction.POST("/new", h.newTransaction)
transaction.POST("/:transactionid", h.updateTransaction)
}
authenticated.GET("/admin/clear-database", h.clearDatabase)
}
func (h *Handler) ServeStatic(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
h.ServeStaticFile(c, c.Path())
return nil
}
api.Any("/*", h.notFound)
}
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"))
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 (h *Handler) notFound(c echo.Context) error {
fmt.Println("not found?")
return echo.NewHTTPError(http.StatusNotImplemented, "not found")
}
func enableCachingForStaticFiles() echo.MiddlewareFunc {