mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
fix wiki based on a user e-mail, correctly pointed, we only have IsOnline which reports true/false for online/offline respectfully
parent
04580179dc
commit
94de79999c
32
Routing.md
32
Routing.md
|
@ -113,11 +113,10 @@ func handler(ctx iris.Context){
|
||||||
|
|
||||||
### Offline Routes
|
### Offline Routes
|
||||||
|
|
||||||
There is one special method in Iris that you can use too. It's called `None` and you can use it to hide a route from outsiders but still able to call it from other route's handlers through the `Context.Exec` method. Each API Handle method returns the Route value back. A Route contains two methods to retrieve the state: `IsOffline()` and `IsOnline()`. You can change the **state** of the route from **offline** to **online** and visa-versa through its `Route.Method` field's value. Of course each change of the router at serve-time requires an `app.RefreshRouter()` call which is safe to use. Take a look below a more complete example:
|
There is one special method in Iris that you can use too. It's called `None` and you can use it to hide a route from outsiders but still able to call it from other route's handlers through the `Context.Exec` method. Each API Handle method returns the Route value back. A Route the `IsOnline` method which reports back the current state of that route. You can change the **state** of the route from **offline** to **online** and visa-versa through its `Route.Method` field's value. Of course each change of the router at serve-time requires an `app.RefreshRouter()` call which is safe to use. Take a look below a more complete example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// file: main.go
|
// file: main.go
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -127,7 +126,7 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
invisibleRoute := app.None("/invisible/{username}", func(ctx iris.Context) {
|
none := app.None("/invisible/{username}", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello %s with method: %s", ctx.Params().Get("username"), ctx.Method())
|
ctx.Writef("Hello %s with method: %s", ctx.Params().Get("username"), ctx.Method())
|
||||||
|
|
||||||
if from := ctx.Values().GetString("from"); from != "" {
|
if from := ctx.Values().GetString("from"); from != "" {
|
||||||
|
@ -137,30 +136,29 @@ func main() {
|
||||||
|
|
||||||
app.Get("/change", func(ctx iris.Context) {
|
app.Get("/change", func(ctx iris.Context) {
|
||||||
|
|
||||||
if invisibleRoute.IsOnline() {
|
if none.IsOnline() {
|
||||||
invisibleRoute.Method = iris.MethodNone
|
none.Method = iris.MethodNone
|
||||||
} else {
|
} else {
|
||||||
invisibleRoute.Method = iris.MethodGet
|
none.Method = iris.MethodGet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh re-builds the router at serve-time
|
// refresh re-builds the router at serve-time in order to be notified for its new routes.
|
||||||
// in order to be notified for its new routes.
|
|
||||||
app.RefreshRouter()
|
app.RefreshRouter()
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/execute", func(ctx iris.Context) {
|
app.Get("/execute", func(ctx iris.Context) {
|
||||||
// Same as navigating to "http://localhost:8080/invisible/iris"
|
if !none.IsOnline() {
|
||||||
// when /change has being invoked and route state changed
|
ctx.Values().Set("from", "/execute with offline access")
|
||||||
// from "offline" to "online"
|
|
||||||
// Values and session can be shared when calling Exec from a "foreign" context.
|
|
||||||
ctx.Values().Set("from", "/execute")
|
|
||||||
|
|
||||||
if invisibleRoute.IsOffline() {
|
|
||||||
ctx.Exec("NONE", "/invisible/iris")
|
ctx.Exec("NONE", "/invisible/iris")
|
||||||
}else{
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// same as navigating to "http://localhost:8080/invisible/iris" when /change has being invoked and route state changed
|
||||||
|
// from "offline" to "online"
|
||||||
|
ctx.Values().Set("from", "/execute") // values and session can be shared when calling Exec from a "foreign" context.
|
||||||
|
// ctx.Exec("NONE", "/invisible/iris")
|
||||||
// or after "/change":
|
// or after "/change":
|
||||||
ctx.Exec("GET", "/invisible/iris")
|
ctx.Exec("GET", "/invisible/iris")
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Run(iris.Addr(":8080"))
|
app.Run(iris.Addr(":8080"))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user