mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
remove the complicated fallback handlers, that didn't work and not approve the coblexity addition of the https://github.com/kataras/iris/pull/919, RouteExists accepts first argument the Context, add new AllowMethods per party and fix cors by 048e2be034 https://github.com/kataras/iris/issues/922, rel: https://github.com/iris-contrib/middleware/issues/36, https://github.com/iris-contrib/middleware/issues/34, https://github.com/iris-contrib/middleware/issues/32, https://github.com/iris-contrib/middleware/issues/30, https://github.com/kataras/iris/pull/909
Former-commit-id: 5576c44b64014fb00dd79e618b815b5f52b705e4
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
// $ go get github.com/rs/cors
|
||||
// $ go run main.go
|
||||
// go get -u github.com/iris-contrib/middleware/...
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
@@ -12,14 +11,12 @@ import (
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// `crs := cors.NewAllowAllPartyMiddleware()`, or:
|
||||
crs := cors.NewPartyMiddleware(cors.Options{
|
||||
crs := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
|
||||
AllowCredentials: true,
|
||||
})
|
||||
|
||||
v1 := app.Party("/api/v1")
|
||||
v1.ConfigureParty(crs)
|
||||
v1 := app.Party("/api/v1", crs).AllowMethods(iris.MethodOptions) // <- important for the preflight.
|
||||
{
|
||||
v1.Get("/home", func(ctx iris.Context) {
|
||||
ctx.WriteString("Hello from /home")
|
||||
@@ -38,13 +35,5 @@ func main() {
|
||||
})
|
||||
}
|
||||
|
||||
// or use that to wrap the entire router
|
||||
// even before the path and method matching
|
||||
// this should work better and with all cors' features.
|
||||
// Use that instead, if suits you.
|
||||
// app.WrapRouter(cors.WrapNext(cors.Options{
|
||||
// AllowedOrigins: []string{"*"},
|
||||
// AllowCredentials: true,
|
||||
// }))
|
||||
app.Run(iris.Addr("localhost:8080"))
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// add a fallback handler to process requests that would not be declared in the router.
|
||||
app.Fallback(fallbackHandler)
|
||||
|
||||
// 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"), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
}
|
||||
|
||||
func h(ctx iris.Context) {
|
||||
ctx.Writef("[%s] %s : Parameter = `%s`", ctx.Method(), ctx.Path(), ctx.Params().Get("p"))
|
||||
}
|
||||
|
||||
func fallbackHandler(ctx iris.Context) {
|
||||
if ctx.Method() == iris.MethodDelete {
|
||||
ctx.NextOrNotFound()
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("[%s] %s : From fallback handler", ctx.Method(), ctx.Path())
|
||||
}
|
||||
Reference in New Issue
Block a user