/
ping.go
63 lines (52 loc) · 1.69 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"context"
"fmt"
"log"
"github.com/go-training/proto-connect-demo/gen/go/ping/v1"
"github.com/go-training/proto-connect-demo/gen/go/ping/v1/pingconnect"
"github.com/bufbuild/connect-go"
grpchealth "github.com/bufbuild/connect-grpchealth-go"
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
"github.com/gin-gonic/gin"
)
type PingService struct{}
func (s *PingService) Ping(
ctx context.Context,
req *connect.Request[ping.PingRequest],
) (*connect.Response[ping.PingResponse], error) {
log.Println("Content-Type: ", req.Header().Get("Content-Type"))
log.Println("User-Agent: ", req.Header().Get("User-Agent"))
res := connect.NewResponse(&ping.PingResponse{
Data: fmt.Sprintf("Hello, %s!", req.Msg.Data),
})
res.Header().Set("Gitea-Version", "v1")
return res, nil
}
func pingServiceRoute(r *gin.Engine) {
compress1KB := connect.WithCompressMinBytes(1024)
pingService := &PingService{}
connectPath, connecthandler := pingconnect.NewPingServiceHandler(
pingService,
compress1KB,
)
// grpcV1
grpcPath, gHandler := grpcreflect.NewHandlerV1(
grpcreflect.NewStaticReflector(pingconnect.PingServiceName),
compress1KB,
)
// grpcV1Alpha
grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha(
grpcreflect.NewStaticReflector(pingconnect.PingServiceName),
compress1KB,
)
// grpcHealthCheck
grpcHealthPath, gHealthHandler := grpchealth.NewHandler(
grpchealth.NewStaticChecker(pingconnect.PingServiceName),
compress1KB,
)
r.POST(connectPath+":name", grpcHandler(connecthandler))
r.POST(grpcPath+"Ping", grpcHandler(gHandler))
r.POST(grpcAlphaPath+"Ping", grpcHandler(gAlphaHandler))
r.POST(grpcHealthPath+"Ping", grpcHandler(gHealthHandler))
}