buf connect-go 的路由,只是利用了golang http handler 的特性
参考使用
package main
import (
"context"
"log"
"net/http"
"/bufbuild/connect-go"
userloginv1 "/rongfengliang/go-connect-app/pkg/userlogin/v1"
userloginv1connect "/rongfengliang/go-connect-app/pkg/userlogin/v1/userloginv1connect"
"/x/net/http2"
"/x/net/http2/h2c"
)
type UserLogin struct{}
func (s *UserLogin) Login(
ctx context.Context,
req *connect.Request[userloginv1.LoginRequest],
) (*connect.Response[userloginv1.LoginResponse], error) {
log.Println("Request headers: ", req.Header())
log.Println("Request username: ", req.Msg.Username)
res := connect.NewResponse(&userloginv1.LoginResponse{
Token: "demoapp",
Exprise: 100,
})
res.Header().Set("userlogin-Version", "v1")
return res, nil
}
func main() {
userlogin := &UserLogin{}
api := http.NewServeMux()
api.Handle(userloginv1connect.NewUserLoginServiceHandler(userlogin))
mux := http.NewServeMux()
// 使用http.StripPrefix 进行处理
mux.Handle("/grpc/", http.StripPrefix("/grpc", api))
http.ListenAndServe(
"0.0.0.0:8080",
h2c.NewHandler(mux, &http2.Server{}),
)
}