-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] how to get http.ResponseWriter in services with using middleware? #2953
Comments
可以尝试在ResponseEncoder这里处理 |
You can try to handle it here in ResponseEncoder |
ResponseEncoder 只是针对返回的内容进行 encode package main
import (
"context"
"github.com/go-kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/metadata"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/gorilla/websocket"
"log"
)
// go build -ldflags "-X main.Version=x.y.z"
var (
// Name is the name of the compiled software.
Name = "helloworld"
// Version is the version of the compiled software.
// Version = "v1.0.0"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
helloworld.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
hctx, ok := FromContext(ctx)
if !ok {
return nil, errors.BadRequest("custom_error", "not a http.Context")
}
req := hctx.Request()
resp := hctx.Response()
upgrader := websocket.Upgrader{}
c, _ := upgrader.Upgrade(resp, req, nil)
c.WriteMessage(websocket.TextMessage, []byte("websocket connect"))
return nil, nil
}
type HttpC struct{}
func FromContext(ctx context.Context) (http.Context, bool) {
h, ok := ctx.Value(HttpC{}).(http.Context)
return h, ok
}
func HttpContext(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
ctx = context.WithValue(ctx, HttpC{}, ctx)
return handler(ctx, req)
}
}
func main() {
s := &server{}
httpSrv := http.NewServer(
http.Address(":8000"),
http.Middleware(
recovery.Recovery(),
HttpContext,
metadata.Server(),
),
)
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
recovery.Recovery(),
),
)
helloworld.RegisterGreeterServer(grpcSrv, s)
helloworld.RegisterGreeterHTTPServer(httpSrv, s)
app := kratos.New(
kratos.Name(Name),
kratos.Server(
httpSrv,
grpcSrv,
),
)
if err := app.Run(); err != nil {
log.Fatal(err)
}
} |
Can you please give me the coding of RequestFromServerContext? |
func getIP(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
if httpCtx, ok := http.RequestFromServerContext(ctx); ok {
host, _, err := net.SplitHostPort(httpCtx.RemoteAddr)
if err != nil {
return nil, err
}
ctx = context.WithValue(ctx, "ip", host)
return handler(ctx, req)
}
return handler(ctx, req)
}
} |
Hi, @z760087139 I'm helping the Kratos team manage their backlog and am marking this issue as stale. It seems like you're seeking a solution to access http.ResponseWriter in services while using middleware that wraps the context, such as the metadata middleware. There have been comments from guihouchang, XiaoK29, and others providing suggestions and code examples to address the issue. Could you please confirm if this issue is still relevant to the latest version of the Kratos repository? If it is, please let the Kratos team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you! |
RequestFromServerContext returns request. Is there any way to get ResponseWriter? It is also used for websocket. |
|
use this method |
我想在 services 使用 http.ResponseWriter,但如果在 server 层增加中间件,且中间件进行了 context 封装,如例子中的 metadata 中间件,对 context 断言 http.Context 则会失败
有没有办法在使用中间件的同时能够获取 http.ResponseWriter?
有个类似的 #2429 question 但未考虑中间件对ctx 的封装问题
example
The text was updated successfully, but these errors were encountered: