2017-11-05 03:12:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/kataras/iris"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2018-02-21 13:19:09 +01:00
|
|
|
|
2018-11-06 03:06:50 +01:00
|
|
|
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"
|
2017-11-05 03:12:18 +01:00
|
|
|
|
2018-03-10 13:22:56 +01:00
|
|
|
v1 := app.Party("/api/v1", crs).AllowMethods(iris.MethodOptions) // <- important for the preflight.
|
2017-11-05 03:12:18 +01:00
|
|
|
{
|
2018-11-06 03:06:50 +01:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2017-11-05 03:12:18 +01:00
|
|
|
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")
|
|
|
|
})
|
2018-02-21 10:27:01 +01:00
|
|
|
v1.Put("/send", func(ctx iris.Context) {
|
|
|
|
ctx.WriteString("updated")
|
|
|
|
})
|
|
|
|
v1.Delete("/send", func(ctx iris.Context) {
|
|
|
|
ctx.WriteString("deleted")
|
|
|
|
})
|
2017-11-05 03:12:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 03:06:50 +01:00
|
|
|
app.Run(iris.Addr(":80"))
|
2017-11-05 03:12:18 +01:00
|
|
|
}
|