Files
gitea/routers/api/bots/ping.go
Bo-Yi Wu ef3a74a1ba chore(proto): Add ping service
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2022-11-25 17:48:44 +08:00

68 lines
1.8 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package bots
import (
"context"
"fmt"
"log"
"code.gitea.io/gitea/modules/web"
v1 "gitea.com/gitea/proto/gen/proto/v1"
"gitea.com/gitea/proto/gen/proto/v1/v1connect"
"github.com/bufbuild/connect-go"
grpchealth "github.com/bufbuild/connect-grpchealth-go"
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
)
type PingService struct{}
func (s *PingService) Ping(
ctx context.Context,
req *connect.Request[v1.PingRequest],
) (*connect.Response[v1.PingResponse], error) {
log.Println("Content-Type: ", req.Header().Get("Content-Type"))
log.Println("User-Agent: ", req.Header().Get("User-Agent"))
res := connect.NewResponse(&v1.PingResponse{
Data: fmt.Sprintf("Hello, %s!", req.Msg.Data),
})
res.Header().Set("Gitea-Version", "v1")
return res, nil
}
func pingServiceRoute(r *web.Route) {
compress1KB := connect.WithCompressMinBytes(1024)
pingService := &PingService{}
connectPath, connecthandler := v1connect.NewPingServiceHandler(
pingService,
compress1KB,
)
// grpcV1
grpcPath, gHandler := grpcreflect.NewHandlerV1(
grpcreflect.NewStaticReflector(v1connect.PingServiceName),
compress1KB,
)
// grpcV1Alpha
grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha(
grpcreflect.NewStaticReflector(v1connect.PingServiceName),
compress1KB,
)
// grpcHealthCheck
grpcHealthPath, gHealthHandler := grpchealth.NewHandler(
grpchealth.NewStaticChecker(v1connect.PingServiceName),
compress1KB,
)
r.Post(connectPath+"{name}", grpcHandler(connecthandler))
r.Post(grpcPath+"{name}", grpcHandler(gHandler))
r.Post(grpcAlphaPath+"{name}", grpcHandler(gAlphaHandler))
r.Post(grpcHealthPath+"{name}", grpcHandler(gHealthHandler))
}