-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get a error when I using "github.com/iris-contrib/middleware/cors" #60
Labels
Comments
Hello @roderick-liu, this is happening because you've tried to install the stable version with the $ go mod init myapp
$ go get github.com/kataras/iris/v12@master
$ go get github.com/iris-contrib/middleware/cors@master Example Appserver package main
import (
"github.com/kataras/iris/v12"
"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,
})
/* OR just:
crs := func(ctx iris.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")
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-Max-Age", "86400")
ctx.StatusCode(iris.StatusNoContent)
return
}
ctx.Next()
}
*/
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.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.Application().Logger().Infof("received %#+v", any)
ctx.JSON(iris.Map{"message": "ok"})
})
v1.Get("/home", func(ctx iris.Context) {
ctx.WriteString("Hello from /home")
})
v1.Get("/about", func(ctx iris.Context) {
ctx.WriteString("Hello from /about")
})
v1.Post("/send", func(ctx iris.Context) {
ctx.WriteString("sent")
})
v1.Put("/send", func(ctx iris.Context) {
ctx.WriteString("updated")
})
v1.Delete("/send", func(ctx iris.Context) {
ctx.WriteString("deleted")
})
}
app.Listen(":8080", iris.WithTunneling)
} server/go.mod
client package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.ServeFile("index.html")
})
// Navigate to http://localhost:9090,
// this will act as a client for your server.
// Don't forget to EDIT the index.html's host variable
// to match the server's one.
app.Listen(":9090")
} <body>
<script type="text/javascript">
// Replace the "host" with your domain (you can use iris.WithTunneling).
const host = 'https://b6fbe7259b4f.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: "[email protected]"
})
.then(data => {
console.log(data);
document.write(data.message);
});
</script>
</body> output Notes
app.UseRouter(crs)
Thanks, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
go version go1.14.4 linux/amd64
programe:
package main
import (
"fmt"
"github.com/iris-contrib/middleware/cors"
"log"
"net"
"os"
)
func main() {
fmt.Println("Hello World!")
fmt.Println(os.Getuid())
f, err := os.Open("test.json")
if err != nil {
fmt.Printf("%s\n", err)
}
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
AllowCredentials: true,
})
}
error :
$ go build hello.go
github.com/iris-contrib/middleware/cors
gowork/src/github.com/iris-contrib/middleware/cors/cors.go:144:10: cannot use c.Serve (type func("github.com/kataras/iris/context".Context)) as type "github.com/kataras/iris/context".Handler in return argument
otherwise:
in go.mod:
there is a conflict between
github.com/iris-contrib/middleware v12.1.2+incompatible and import github.com/iris-contrib/middleware/cors
The text was updated successfully, but these errors were encountered: