mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"app/protos"
|
||
|
|
||
|
"github.com/kataras/iris/v12"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
app := iris.New()
|
||
|
|
||
|
app.Get("/", send)
|
||
|
app.Get("/json", sendAsJSON)
|
||
|
app.Post("/read", read)
|
||
|
app.Post("/read_json", readFromJSON)
|
||
|
|
||
|
app.Listen(":8080")
|
||
|
}
|
||
|
|
||
|
func send(ctx iris.Context) {
|
||
|
response := &protos.HelloReply{Message: "Hello, World!"}
|
||
|
ctx.Protobuf(response)
|
||
|
}
|
||
|
|
||
|
func sendAsJSON(ctx iris.Context) {
|
||
|
response := &protos.HelloReply{Message: "Hello, World!"}
|
||
|
options := iris.JSON{
|
||
|
Proto: iris.ProtoMarshalOptions{
|
||
|
AllowPartial: true,
|
||
|
Multiline: true,
|
||
|
Indent: " ",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
ctx.JSON(response, options)
|
||
|
}
|
||
|
|
||
|
func read(ctx iris.Context) {
|
||
|
var request protos.HelloRequest
|
||
|
|
||
|
err := ctx.ReadProtobuf(&request)
|
||
|
if err != nil {
|
||
|
ctx.StopWithError(iris.StatusBadRequest, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ctx.Writef("HelloRequest.Name = %s", request.Name)
|
||
|
}
|
||
|
|
||
|
func readFromJSON(ctx iris.Context) {
|
||
|
var request protos.HelloRequest
|
||
|
|
||
|
err := ctx.ReadJSONProtobuf(&request)
|
||
|
if err != nil {
|
||
|
ctx.StopWithError(iris.StatusBadRequest, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ctx.Writef("HelloRequest.Name = %s", request.Name)
|
||
|
}
|