From f482e13fb7a8bf1237db2a6dd147115211ac840a Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 16 May 2020 03:22:45 +0300 Subject: [PATCH] cors example Former-commit-id: 31f25c5b3f0e02050bf9b120e903f56d1af35eeb --- .../cors/simple/client/index.html | 41 +++++++++++++++++++ .../cors/simple/client/main.go | 32 ++------------- .../experimental-handlers/cors/simple/main.go | 22 +++++----- 3 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 _examples/experimental-handlers/cors/simple/client/index.html diff --git a/_examples/experimental-handlers/cors/simple/client/index.html b/_examples/experimental-handlers/cors/simple/client/index.html new file mode 100644 index 00000000..f5ea7416 --- /dev/null +++ b/_examples/experimental-handlers/cors/simple/client/index.html @@ -0,0 +1,41 @@ + + + + + + + Example CORS + + + + + + + \ No newline at end of file diff --git a/_examples/experimental-handlers/cors/simple/client/main.go b/_examples/experimental-handlers/cors/simple/client/main.go index bf551f30..b0196965 100644 --- a/_examples/experimental-handlers/cors/simple/client/main.go +++ b/_examples/experimental-handlers/cors/simple/client/main.go @@ -1,38 +1,14 @@ package main -import ( - "github.com/kataras/iris/v12" -) - -// NOTE: THIS IS OPTIONALLY. -// It is just an example of communication between cors/simple/main.go and your app -// based on issues that beginners had with it. -// You should use your own favourite library for HTTP requests (any programming language ofc). -// -// Replace the '8fc93b1c.ngrok.io' with a domain which -// exposes the cors/simple/main.go server side. -const url = "http://8fc93b1c.ngrok.io/api/v1/mailer" - -var clientSide = []byte(``) +import "github.com/kataras/iris/v12" func main() { app := iris.New() app.Get("/", func(ctx iris.Context) { - ctx.Write(clientSide) + ctx.ServeFile("index.html") }) - // Start and navigate to http://localhost:8080 - // and go to the previous terminal of your running cors/simple/main.go server - // and see the logs. + // Read index.html comments, + // adn then start and navigate to http://localhost:8080. app.Listen(":8080") } diff --git a/_examples/experimental-handlers/cors/simple/main.go b/_examples/experimental-handlers/cors/simple/main.go index 61eba676..4f6667ff 100644 --- a/_examples/experimental-handlers/cors/simple/main.go +++ b/_examples/experimental-handlers/cors/simple/main.go @@ -1,8 +1,6 @@ package main -import ( - "github.com/kataras/iris/v12" -) +import "github.com/kataras/iris/v12" func main() { app := iris.New() @@ -10,7 +8,14 @@ func main() { crs := func(ctx iris.Context) { ctx.Header("Access-Control-Allow-Origin", "*") ctx.Header("Access-Control-Allow-Credentials", "true") - ctx.Header("Access-Control-Allow-Headers", "Access-Control-Allow-Origin,Content-Type") + + if ctx.Method() == iris.MethodOptions { + ctx.Header("Access-Control-Methods", "POST, PUT, PATCH, DELETE") + ctx.Header("Access-Control-Allow-Headers", "Access-Control-Allow-Origin,Content-Type") + ctx.Header("Access-Control-Max-Age", "86400") + ctx.StatusCode(iris.StatusNoContent) + return + } ctx.Next() } // or "github.com/iris-contrib/middleware/cors" @@ -25,6 +30,8 @@ func main() { return } ctx.Application().Logger().Infof("received %#+v", any) + + ctx.JSON(iris.Map{"message": "ok"}) }) v1.Get("/home", func(ctx iris.Context) { @@ -44,10 +51,5 @@ func main() { }) } - // iris.WithoutPathCorrectionRedirection | iris#Configuration.DisablePathCorrectionRedirection: - // CORS needs the allow origin headers in the redirect response as well, we have a solution for this: - // Add the iris.WithoutPathCorrectionRedirection option - // to directly fire the handler instead of redirection (which is the default behavior) - // on request paths like "/v1/mailer/" when "/v1/mailer" route handler is registered. - app.Listen(":80", iris.WithoutPathCorrectionRedirection) + app.Listen(":80", iris.WithTunneling) }