mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
add a rs/cors full example
Former-commit-id: 9655e04b91145f60e69096cbd662fef08a91f988
This commit is contained in:
parent
350887d302
commit
05ae7792df
|
@ -142,6 +142,7 @@
|
|||
* [i18n](i18n/main.go)
|
||||
* Authentication, Authorization & Bot Detection
|
||||
* [Basic Authentication](auth/basicauth/main.go)
|
||||
* [CORS](auth/cors)
|
||||
* [JWT](auth/jwt/main.go)
|
||||
* [JWT (community edition)](https://github.com/iris-contrib/middleware/tree/v12/jwt/_example/main.go)
|
||||
* [OAUth2](auth/goth/main.go)
|
||||
|
|
63
_examples/auth/cors/main.go
Normal file
63
_examples/auth/cors/main.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
// Package main integrates the "rs/cors" net/http middleware into Iris.
|
||||
// That cors third-party middleware cannot be registered through `iris.FromStd`
|
||||
// as a common middleware because it should be injected before the Iris Router itself,
|
||||
// it allows/dissallows HTTP Methods too.
|
||||
//
|
||||
// This is just an example you can use to run something, based on custom logic,
|
||||
// before the Iris Router itself.
|
||||
//
|
||||
// In the "routing/custom-wrapper" example
|
||||
// we learn how we can acquire and release an Iris context to fire an Iris Handler
|
||||
// based on custom logic, before the Iris Router itself. In that example
|
||||
// we will fire a net/http handler (the "rs/cors" handler one) instead.
|
||||
//
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
c := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowCredentials: true,
|
||||
// Enable Debugging for testing, consider disabling in production
|
||||
Debug: true,
|
||||
})
|
||||
// app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
|
||||
// [custom logic...]
|
||||
// if shouldFireNetHTTPHandler {
|
||||
// ...ServeHTTP(w,r)
|
||||
// return
|
||||
// }
|
||||
// router(w,r)
|
||||
// })
|
||||
// In our case, the cors package has a ServeHTTP
|
||||
// of the same form of app.WrapRouter's accept input argument,
|
||||
// so we can just do:
|
||||
app.WrapRouter(c.ServeHTTP)
|
||||
|
||||
// Serve ./public/index.html, main.js.
|
||||
app.HandleDir("/", "./public")
|
||||
|
||||
// Register routes here...
|
||||
app.Get("/data", listData)
|
||||
|
||||
// http://localhost:8080 and click the "fetch data" button.
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
type item struct {
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
func listData(ctx iris.Context) {
|
||||
ctx.JSON([]item{
|
||||
{"Item 1"},
|
||||
{"Item 2"},
|
||||
{"Item 3"},
|
||||
})
|
||||
}
|
18
_examples/auth/cors/public/index.html
Normal file
18
_examples/auth/cors/public/index.html
Normal file
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Iris Cors Example</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<ul id="list">
|
||||
</ul>
|
||||
|
||||
<input type="button" value="Fetch Data" id="fetchBtn" />
|
||||
|
||||
<script src="main.js"></script>
|
||||
</body>
|
||||
</html>
|
43
_examples/auth/cors/public/main.js
Normal file
43
_examples/auth/cors/public/main.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
||||
async function doRequest(method = 'GET', url = '', data = {}) {
|
||||
// Default options are marked with *
|
||||
|
||||
const request = {
|
||||
method: method, // *GET, POST, PUT, DELETE, etc.
|
||||
mode: 'cors', // no-cors, *cors, same-origin
|
||||
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
|
||||
credentials: 'same-origin', // include, *same-origin, omit
|
||||
redirect: 'follow', // manual, *follow, error
|
||||
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
|
||||
};
|
||||
|
||||
if (data !== undefined && method !== 'GET' && method !== 'HEAD') {
|
||||
request.headers = {
|
||||
'Content-Type': 'application/json'
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded',
|
||||
};
|
||||
// body data type must match "Content-Type" header.
|
||||
request.body = JSON.stringify(data);
|
||||
}
|
||||
|
||||
const response = await fetch(url, request);
|
||||
return response.json(); // parses JSON response into native JavaScript objects.
|
||||
}
|
||||
|
||||
const ul = document.getElementById("list");
|
||||
|
||||
function fetchData() {
|
||||
console.log("sending request...")
|
||||
|
||||
doRequest('GET', '/data').then(data => {
|
||||
data.forEach(item => {
|
||||
var li = document.createElement("li");
|
||||
li.appendChild(document.createTextNode(item.title));
|
||||
ul.appendChild(li);
|
||||
});
|
||||
|
||||
console.log(data); // JSON data parsed by `response.json()` call.
|
||||
});
|
||||
}
|
||||
|
||||
document.getElementById("fetchBtn").onclick = fetchData;
|
Loading…
Reference in New Issue
Block a user