Add one more example for custom router macro functions, relative to https://github.com/kataras/iris/issues/918

Former-commit-id: 457c1a94dc8d93e614e9da70e1ec2482fe0c5765
This commit is contained in:
Gerasimos Maropoulos 2018-03-05 20:09:30 +02:00
parent 5c2edeebec
commit 1a4803307d

View File

@ -1,6 +1,7 @@
package main
import (
"regexp"
"strconv"
"github.com/kataras/iris"
@ -140,6 +141,24 @@ func main() {
ctx.Writef("Hello id: %d looking for friend id: ", id, friendid)
}) // this will throw e 504 error code instead of 404 if all route's macros not passed.
// Another example using a custom regexp and any custom logic.
latLonExpr := "^-?[0-9]{1,3}(?:\\.[0-9]{1,10})?$"
latLonRegex, err := regexp.Compile(latLonExpr)
if err != nil {
panic(err)
}
app.Macros().String.RegisterFunc("coordinate", func() func(paramName string) (ok bool) {
// MatchString is a type of func(string) bool, so we can return that as it's.
return latLonRegex.MatchString
})
app.Get("/coordinates/{lat:string coordinate() else 502}/{lon:string coordinate() else 502}", func(ctx iris.Context) {
ctx.Writef("Lat: %s | Lon: %s", ctx.Params().Get("lat"), ctx.Params().Get("lon"))
})
//
// http://localhost:8080/game/a-zA-Z/level/0-9
// remember, alphabetical is lowercase or uppercase letters only.
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx iris.Context) {