• buf connectgo 路由


    buf connect-go 的路由,只是利用了golang http handler 的特性

    参考使用

    package main
     
    import (
        "context"
        "log"
        "net/http"
     
        "github.com/bufbuild/connect-go"
        userloginv1 "github.com/rongfengliang/go-connect-app/pkg/userlogin/v1"
        userloginv1connect "github.com/rongfengliang/go-connect-app/pkg/userlogin/v1/userloginv1connect"
        "golang.org/x/net/http2"
        "golang.org/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{}),
        )
    }

    参考资料

    https://connect.build/docs/go/routing

  • 相关阅读:
    poj 1611 The Suspects
    POJ 2236 Wireless Network
    POJ 1703 Find them, Catch them
    poj 1733 Parity game
    hdu 1598 find the most comfortable road
    cmake的使用二:链接第三方静态库
    线段树基础
    迪杰斯特拉算法
    Floyd详解
    STL中的set使用方法详细
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/16705784.html
Copyright © 2020-2023  润新知