chore(proto): Add ping service

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2022-08-13 21:41:31 +08:00 committed by Jason Song
parent 0e79fc556a
commit ef3a74a1ba
5 changed files with 150 additions and 70 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.18
require (
code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b
code.gitea.io/sdk/gitea v0.15.1
gitea.com/gitea/proto v0.0.0-20220802024851-7ee5947f928a
gitea.com/gitea/proto v0.0.0-20220813120843-ce4b5dd68c1f
gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb
codeberg.org/gusted/mcaptcha v0.0.0-20220723083913-4f3072e1d570
gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681

View File

@ -6,47 +6,13 @@ package bots
import (
"code.gitea.io/gitea/modules/web"
"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"
)
func Routes(r *web.Route) {
compress1KB := connect.WithCompressMinBytes(1024)
service := &RunnerService{}
path, handler := v1connect.NewBuildServiceHandler(
service,
compress1KB,
)
// grpcV1
grpcPath, gHandler := grpcreflect.NewHandlerV1(
grpcreflect.NewStaticReflector(v1connect.BuildServiceName),
compress1KB,
)
// grpcV1Alpha
grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha(
grpcreflect.NewStaticReflector(v1connect.BuildServiceName),
compress1KB,
)
// grpcHealthCheck
grpcHealthPath, gHealthHandler := grpchealth.NewHandler(
grpchealth.NewStaticChecker(v1connect.BuildServiceName),
compress1KB,
)
// socket connection
r.Get("/socket", socketServe)
// restful connection
r.Post(path+"{name}", giteaHandler(handler))
// grpc connection
r.Post(grpcPath+"{name}", giteaHandler(gHandler))
r.Post(grpcAlphaPath+"{name}", giteaHandler(gAlphaHandler))
// healthy check connection
r.Post(grpcHealthPath+"{name}", giteaHandler(gHealthHandler))
// runner service
runnerServiceRoute(r)
// ping service
pingServiceRoute(r)
}

67
routers/api/bots/ping.go Normal file
View File

@ -0,0 +1,67 @@
// 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))
}

View File

@ -5,42 +5,12 @@
package bots
import (
"context"
"net/http"
"code.gitea.io/gitea/modules/log"
v1 "gitea.com/gitea/proto/gen/proto/v1"
"github.com/bufbuild/connect-go"
)
type RunnerService struct{}
func (s *RunnerService) Connect(
ctx context.Context,
req *connect.Request[v1.ConnectRequest],
) (*connect.Response[v1.ConnectResponse], error) {
log.Info("Request headers: %v", req.Header())
res := connect.NewResponse(&v1.ConnectResponse{
JobId: 100,
})
res.Header().Set("Gitea-Version", "v1")
return res, nil
}
func (s *RunnerService) Accept(
ctx context.Context,
req *connect.Request[v1.AcceptRequest],
) (*connect.Response[v1.AcceptResponse], error) {
log.Info("Request headers: %v", req.Header())
res := connect.NewResponse(&v1.AcceptResponse{
JobId: 100,
})
res.Header().Set("Gitea-Version", "v1")
return res, nil
}
func giteaHandler(h http.Handler) http.HandlerFunc {
func grpcHandler(h http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Info("Got connection: %v", r.Proto)
h.ServeHTTP(w, r)

View File

@ -0,0 +1,77 @@
// 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"
"code.gitea.io/gitea/modules/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 RunnerService struct{}
func (s *RunnerService) Connect(
ctx context.Context,
req *connect.Request[v1.ConnectRequest],
) (*connect.Response[v1.ConnectResponse], error) {
log.Info("Request headers: %v", req.Header())
res := connect.NewResponse(&v1.ConnectResponse{
JobId: 100,
})
res.Header().Set("Gitea-Version", "v1")
return res, nil
}
func (s *RunnerService) Accept(
ctx context.Context,
req *connect.Request[v1.AcceptRequest],
) (*connect.Response[v1.AcceptResponse], error) {
log.Info("Request headers: %v", req.Header())
res := connect.NewResponse(&v1.AcceptResponse{
JobId: 100,
})
res.Header().Set("Gitea-Version", "v1")
return res, nil
}
func runnerServiceRoute(r *web.Route) {
compress1KB := connect.WithCompressMinBytes(1024)
runnerService := &RunnerService{}
connectPath, connecthandler := v1connect.NewRunnerServiceHandler(
runnerService,
compress1KB,
)
// grpcV1
grpcPath, gHandler := grpcreflect.NewHandlerV1(
grpcreflect.NewStaticReflector(v1connect.RunnerServiceName),
compress1KB,
)
// grpcV1Alpha
grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha(
grpcreflect.NewStaticReflector(v1connect.RunnerServiceName),
compress1KB,
)
// grpcHealthCheck
grpcHealthPath, gHealthHandler := grpchealth.NewHandler(
grpchealth.NewStaticChecker(v1connect.RunnerServiceName),
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))
}