Skip to content

goapt/grpc-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

protoc-gen-go-http

从 protobuf 文件中生成使用 golang http handler

安装

go install github.com/goapt/grpc-http/cmd/protoc-gen-go-http@latest

生成

protoc --proto_path=. \
--proto_path=./third_party \
--go-http_out=paths=source_relative:. \
./example.proto

合约

你需要实现合约[contract](contract)中的接口 Codec: 用户解析请求和返回数据 ServeMux: 可以使用http.ServeMux

mux := http.NewServeMux()

apiv1.RegisterUserServiceHTTPServer(mux,codec,userService)

server := &http.Server{
    Addr:    ":8080", 
    Handler: mux, 
}

if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
    fmt.Printf("Server startup failed: %v\n", err)
}