mirror of
https://github.com/kataras/iris.git
synced 2025-03-15 05:16:28 +01:00
cors example
Former-commit-id: 31f25c5b3f0e02050bf9b120e903f56d1af35eeb
This commit is contained in:
parent
44eafe739b
commit
f482e13fb7
|
@ -0,0 +1,41 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Example CORS</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<script type="text/javascript">
|
||||||
|
// Replace the "host" with your domain (you can use iris.WithTunneling).
|
||||||
|
const host = 'https://e1de7bc1.ngrok.io';
|
||||||
|
|
||||||
|
async function postData(url = '', data = {}) {
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
mode: 'cors',
|
||||||
|
cache: 'no-cache',
|
||||||
|
credentials: 'same-origin',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
redirect: 'follow',
|
||||||
|
referrerPolicy: 'no-referrer',
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
});
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
postData(host + '/api/v1/mailer', {
|
||||||
|
email: "me@test.com"
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
console.log(data);
|
||||||
|
document.write(data.message);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
|
@ -1,38 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris/v12"
|
||||||
"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(`<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() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
app.Get("/", func(ctx iris.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Write(clientSide)
|
ctx.ServeFile("index.html")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Start and navigate to http://localhost:8080
|
// Read index.html comments,
|
||||||
// and go to the previous terminal of your running cors/simple/main.go server
|
// adn then start and navigate to http://localhost:8080.
|
||||||
// and see the logs.
|
|
||||||
app.Listen(":8080")
|
app.Listen(":8080")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris/v12"
|
||||||
"github.com/kataras/iris/v12"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
@ -10,7 +8,14 @@ func main() {
|
||||||
crs := func(ctx iris.Context) {
|
crs := func(ctx iris.Context) {
|
||||||
ctx.Header("Access-Control-Allow-Origin", "*")
|
ctx.Header("Access-Control-Allow-Origin", "*")
|
||||||
ctx.Header("Access-Control-Allow-Credentials", "true")
|
ctx.Header("Access-Control-Allow-Credentials", "true")
|
||||||
|
|
||||||
|
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-Allow-Headers", "Access-Control-Allow-Origin,Content-Type")
|
||||||
|
ctx.Header("Access-Control-Max-Age", "86400")
|
||||||
|
ctx.StatusCode(iris.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
} // or "github.com/iris-contrib/middleware/cors"
|
} // or "github.com/iris-contrib/middleware/cors"
|
||||||
|
|
||||||
|
@ -25,6 +30,8 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Application().Logger().Infof("received %#+v", any)
|
ctx.Application().Logger().Infof("received %#+v", any)
|
||||||
|
|
||||||
|
ctx.JSON(iris.Map{"message": "ok"})
|
||||||
})
|
})
|
||||||
|
|
||||||
v1.Get("/home", func(ctx iris.Context) {
|
v1.Get("/home", func(ctx iris.Context) {
|
||||||
|
@ -44,10 +51,5 @@ func main() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// iris.WithoutPathCorrectionRedirection | iris#Configuration.DisablePathCorrectionRedirection:
|
app.Listen(":80", iris.WithTunneling)
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user