Skip to content

Commit

Permalink
Add one more example for custom router macro functions, relative to #918
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Mar 5, 2018
1 parent 828ad17 commit a7690c7
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions _examples/routing/dynamic-path/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"regexp"
"strconv"

"github.com/kataras/iris"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a7690c7

Please sign in to comment.