mirror of
https://github.com/kataras/iris.git
synced 2025-12-26 14:27:04 +00:00
Add fallback handlers
Former-commit-id: f7e9bd17076a10e1ed1702780d7ce9e89f00b592
This commit is contained in:
@@ -5,6 +5,7 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/core/router"
|
||||
|
||||
"github.com/iris-contrib/middleware/cors"
|
||||
)
|
||||
@@ -14,6 +15,7 @@ func main() {
|
||||
app := iris.New()
|
||||
crs := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
|
||||
AllowedMethods: router.AllMethods[:],
|
||||
AllowCredentials: true,
|
||||
})
|
||||
|
||||
@@ -29,6 +31,12 @@ func main() {
|
||||
v1.Post("/send", func(ctx iris.Context) {
|
||||
ctx.WriteString("sent")
|
||||
})
|
||||
v1.Put("/send", func(ctx iris.Context) {
|
||||
ctx.WriteString("updated")
|
||||
})
|
||||
v1.Delete("/send", func(ctx iris.Context) {
|
||||
ctx.WriteString("deleted")
|
||||
})
|
||||
}
|
||||
|
||||
// or use that to wrap the entire router
|
||||
|
||||
29
_examples/routing/fallback-handlers/main.go
Normal file
29
_examples/routing/fallback-handlers/main.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// this works as expected now,
|
||||
// will handle *all* expect DELETE requests, even if there is no routes
|
||||
app.Get("/action/{p}", h)
|
||||
|
||||
app.Run(iris.Addr(":8080"), ctx.Method(), ctx.Path(), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
}
|
||||
|
||||
func h(ctx iris.Context) {
|
||||
ctx.Writef("[%s] %s : Parameter = `%s`", ctx.Params().Get("p"))
|
||||
}
|
||||
|
||||
func fallbackHandler(ctx iris.Context) {
|
||||
if ctx.Method() == "DELETE" {
|
||||
ctx.Next()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("[%s] %s : From fallback handler", ctx.Method(), ctx.Path())
|
||||
}
|
||||
Reference in New Issue
Block a user