diff --git a/api.go b/api.go index fdbc286..3406893 100644 --- a/api.go +++ b/api.go @@ -7,10 +7,12 @@ import ( "fmt" "github.com/google/uuid" "github.com/labstack/echo/v4" + "github.com/samber/mo" "gorm.io/gorm" "io" "net/http" "regexp" + "strconv" "strings" "time" ) @@ -40,9 +42,23 @@ func (app *App) HandleAPIError(err error, c *echo.Context) error { if e, ok := err.(*echo.HTTPError); ok { code = e.Code + if m, ok := e.Message.(string); ok { message = m } + + if code == http.StatusNotFound { + path_ := (*c).Request().URL.Path + if version, ok := IsDeprecatedAPIPath(path_).Get(); ok { + switch version { + case 1: + message = "Version 1 of this API was deprecated in release 3.0.0." + default: + message = fmt.Sprintf("Version %d of this API is deprecated.", version) + } + } + } + log = false } @@ -73,6 +89,27 @@ func IsAPIPath(path_ string) bool { return false } +func IsDeprecatedAPIPath(path_ string) mo.Option[int] { + if path_ == "/" { + return mo.None[int]() + } + + split := strings.Split(path_, "/") + if len(split) >= 3 && split[1] == "drasl" && split[2] == "api" { + re := regexp.MustCompile(`v(\d+)`) + match := re.FindStringSubmatch(split[3]) + if len(match) == 2 { + version, err := strconv.Atoi(match[1]) + if err != nil { + return mo.None[int]() + } + return mo.Some(version) + } + } + + return mo.None[int]() +} + func (app *App) withAPIToken(f func(c echo.Context, user *User) error) func(c echo.Context) error { bearerExp := regexp.MustCompile("^Bearer (.*)$") diff --git a/flake.nix b/flake.nix index 8052058..88308e0 100644 --- a/flake.nix +++ b/flake.nix @@ -48,7 +48,7 @@ ]; # Update whenever Go dependencies change - vendorHash = "sha256-bkmi3yvE/JPQvTjBzOHZ07PBVXp8lQEv2l0q6GTC94k="; + vendorHash = "sha256-W0XCv9pUaRyExSU6kuDU/iNzQkgDPBthhiRrgHWK6P8="; outputs = ["out"]; diff --git a/go.mod b/go.mod index 1ccca10..c11ff09 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/samber/mo v1.13.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/net v0.23.0 // indirect diff --git a/go.sum b/go.sum index a6cd838..288ab4a 100644 --- a/go.sum +++ b/go.sum @@ -61,6 +61,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/samber/mo v1.13.0 h1:LB1OwfJMju3a6FjghH+AIvzMG0ZPOzgTWj1qaHs1IQ4= +github.com/samber/mo v1.13.0/go.mod h1:BfkrCPuYzVG3ZljnZB783WIJIGk1mcZr9c9CPf8tAxs= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=