1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00
kratos/examples/http/middlewares/handlers.go
Tony Chen eca0f35cb5
transport/http: add vars to http context (#1037)
* add vars to http context

* add query to http context

* remove http deps

* clean request decoder
2021-06-12 22:21:54 +08:00

28 lines
705 B
Go

package main
import (
"context"
"github.com/go-kratos/kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http"
)
func sayHelloHandler(ctx http.Context) error {
var in helloworld.HelloRequest
if err := ctx.Bind(&in); err != nil {
return err
}
// binding /hello/{name} to in.Name
if err := ctx.BindVars(&in); err != nil {
return err
}
transport.SetOperation(ctx, "/helloworld.Greeter/SayHello")
h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) {
return &helloworld.HelloReply{Message: "test:" + req.(*helloworld.HelloRequest).Name}, nil
})
return ctx.Returns(h(ctx, &in))
}