mirror of
https://github.com/kataras/iris.git
synced 2026-03-07 17:05:58 +00:00
examples: update the cors example to be easier for beginners
And update the pug view engine's vendor jade template engine to yesterday version 1 (tested and worked) to fix https://github.com/kataras/iris/issues/1125 Former-commit-id: 0ea7a8dd97ab13e5ecf984c379234cc671f5d84e
This commit is contained in:
38
_examples/experimental-handlers/cors/simple/client/main.go
Normal file
38
_examples/experimental-handlers/cors/simple/client/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// 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(`<script type="text/javascript">
|
||||
fetch("` + url + `", {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Access-Control-Allow-Origin": "*"
|
||||
},
|
||||
method: "POST",
|
||||
mode: "cors",
|
||||
body: JSON.stringify({ email: "mymail@mail.com" }),
|
||||
});
|
||||
</script>`)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Write(clientSide)
|
||||
})
|
||||
|
||||
// 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.
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -1,23 +1,32 @@
|
||||
package main
|
||||
|
||||
// go get -u github.com/iris-contrib/middleware/...
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
|
||||
"github.com/iris-contrib/middleware/cors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
crs := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
|
||||
AllowCredentials: true,
|
||||
})
|
||||
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")
|
||||
ctx.Next()
|
||||
} // or "github.com/iris-contrib/middleware/cors"
|
||||
|
||||
v1 := app.Party("/api/v1", crs).AllowMethods(iris.MethodOptions) // <- important for the preflight.
|
||||
{
|
||||
v1.Post("/mailer", func(ctx iris.Context) {
|
||||
var any iris.Map
|
||||
err := ctx.ReadJSON(&any)
|
||||
if err != nil {
|
||||
ctx.WriteString(err.Error())
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
ctx.Application().Logger().Infof("received %#+v", any)
|
||||
})
|
||||
|
||||
v1.Get("/home", func(ctx iris.Context) {
|
||||
ctx.WriteString("Hello from /home")
|
||||
})
|
||||
@@ -35,5 +44,5 @@ func main() {
|
||||
})
|
||||
}
|
||||
|
||||
app.Run(iris.Addr("localhost:8080"))
|
||||
app.Run(iris.Addr(":80"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user