2020-04-10 23:55:31 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := newApp()
|
|
|
|
// See main_test.go for usage.
|
|
|
|
app.Listen(":8080")
|
|
|
|
}
|
|
|
|
|
|
|
|
func newApp() *iris.Application {
|
|
|
|
app := iris.New()
|
2020-05-28 18:29:14 +02:00
|
|
|
// To automatically decompress using gzip:
|
|
|
|
// app.Use(iris.GzipReader)
|
|
|
|
|
2020-04-10 23:55:31 +02:00
|
|
|
app.Use(setAllowedResponses)
|
|
|
|
|
|
|
|
app.Post("/", readBody)
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func setAllowedResponses(ctx iris.Context) {
|
|
|
|
// Indicate that the Server can send JSON, XML, YAML and MessagePack for this request.
|
|
|
|
ctx.Negotiation().JSON().XML().YAML().MsgPack()
|
|
|
|
// Add more, allowed by the server format of responses, mime types here...
|
|
|
|
|
|
|
|
// If client is missing an "Accept: " header then default it to JSON.
|
|
|
|
ctx.Negotiation().Accept.JSON()
|
|
|
|
|
|
|
|
ctx.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
type payload struct {
|
|
|
|
Message string `json:"message" xml:"message" msgpack:"message" yaml:"Message" url:"message" form:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func readBody(ctx iris.Context) {
|
|
|
|
var p payload
|
|
|
|
|
|
|
|
// Bind request body to "p" depending on the content-type that client sends the data,
|
|
|
|
// e.g. JSON, XML, YAML, MessagePack, Form, URL Query.
|
|
|
|
err := ctx.ReadBody(&p)
|
|
|
|
if err != nil {
|
|
|
|
ctx.StopWithProblem(iris.StatusBadRequest,
|
|
|
|
iris.NewProblem().Title("Parser issue").Detail(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-16 00:00:51 +02:00
|
|
|
// For the sake of the example, log the received payload.
|
2020-04-10 23:55:31 +02:00
|
|
|
ctx.Application().Logger().Infof("Received: %#+v", p)
|
|
|
|
|
|
|
|
// Send back the payload depending on the accept content type and accept-encoding of the client,
|
|
|
|
// e.g. JSON, XML and so on.
|
|
|
|
ctx.Negotiate(p)
|
|
|
|
}
|