mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
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:
parent
5c2edeebec
commit
1a4803307d
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
|
@ -140,6 +141,24 @@ func main() {
|
||||||
ctx.Writef("Hello id: %d looking for friend id: ", id, friendid)
|
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.
|
}) // 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
|
// http://localhost:8080/game/a-zA-Z/level/0-9
|
||||||
// remember, alphabetical is lowercase or uppercase letters only.
|
// remember, alphabetical is lowercase or uppercase letters only.
|
||||||
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx iris.Context) {
|
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx iris.Context) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user