From f91269130ed30b72578334844d10c0d17dc42221 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 13 Aug 2022 09:07:00 +0300 Subject: [PATCH] add a more straightforward example for #1953 --- _examples/README.md | 1 + .../routing/dynamic-path/at-username/main.go | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 _examples/routing/dynamic-path/at-username/main.go diff --git a/_examples/README.md b/_examples/README.md index 4dbe6f81..55e5ddda 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -55,6 +55,7 @@ * [Not Found - Intelligence](routing/intelligence/main.go) * [Not Found - Suggest Closest Paths](routing/intelligence/manual/main.go) * [Dynamic Path](routing/dynamic-path/main.go) + * [At-username](routing/dynamic-path/at-username/main.go) * [Root Wildcard](routing/dynamic-path/root-wildcard/main.go) * [Implement a Parameter Type](routing/macros/main.go) * [Same Path Pattern but Func](routing/dynamic-path/same-pattern-different-func/main.go) diff --git a/_examples/routing/dynamic-path/at-username/main.go b/_examples/routing/dynamic-path/at-username/main.go new file mode 100644 index 00000000..36e88cb3 --- /dev/null +++ b/_examples/routing/dynamic-path/at-username/main.go @@ -0,0 +1,30 @@ +package main + +import "github.com/kataras/iris/v12" + +func main() { + app := iris.New() + + app.Get("/", func(c iris.Context) { + c.Writef("Hello %s", "world") + }) + + // This is an Iris-only feature across all web frameworks + // in every programming language for years. + // Dynamic Route Path Parameters Functions. + // Set min length characters to 2. + // Prefix of the username is '@' + // Otherwise 404. + // + // You can also use the regexp(...) function for more advanced expressions. + app.Get("/{username:string min(2) prefix(@)}", func(ctx iris.Context) { + username := ctx.Params().Get("username")[1:] + ctx.Writef("Username is %s", username) + }) + + // http://localhost:8080 -> FOUND (Hello world) + // http://localhost:8080/other -> NOT FOUND + // http://localhost/@ -> NOT FOUND + // http://localhost:8080/@kataras -> FOUND (username is kataras) + app.Listen(":8080") +}