forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (48 loc) · 1.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"github.com/kataras/iris"
)
func main() {
app := iris.New()
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")
})
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")
})
}
// iris.WithoutPathCorrectionRedirection | iris#Configuration.DisablePathCorrectionRedirection:
// CORS needs the allow origin headers in the redirect response as well, we have a solution for this:
// If you use iris >= v11.0.4 then add the `app.Run(..., iris.WithoutPathCorrectionRedirection)`
// on the server side if you wish
// 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.Run(iris.Addr(":80"), iris.WithoutPathCorrectionRedirection)
}