From e4e41a806563f7189b707ba4ce09dff631a8c957 Mon Sep 17 00:00:00 2001 From: "Bo-Yi.Wu" Date: Mon, 22 Aug 2022 21:12:39 +0800 Subject: [PATCH] chore(bots): re-structure grpc folder. Signed-off-by: Bo-Yi.Wu --- routers/api/bots/bots.go | 30 ++++++++++++----- routers/api/bots/grpc.go | 32 ------------------ routers/api/bots/grpc/grpc.go | 43 +++++++++++++++++++++++++ routers/api/bots/grpc/health.go | 19 +++++++++++ routers/api/bots/grpc/ping.go | 21 ++++++++++++ routers/api/bots/grpc/runner.go | 21 ++++++++++++ routers/api/bots/handler.go | 18 ----------- routers/api/bots/health.go | 29 ----------------- routers/api/bots/{ => ping}/ping.go | 20 ++---------- routers/api/bots/{ => runner}/runner.go | 22 +++---------- 10 files changed, 133 insertions(+), 122 deletions(-) delete mode 100644 routers/api/bots/grpc.go create mode 100644 routers/api/bots/grpc/grpc.go create mode 100644 routers/api/bots/grpc/health.go create mode 100644 routers/api/bots/grpc/ping.go create mode 100644 routers/api/bots/grpc/runner.go delete mode 100644 routers/api/bots/handler.go delete mode 100644 routers/api/bots/health.go rename routers/api/bots/{ => ping}/ping.go (64%) rename routers/api/bots/{ => runner}/runner.go (66%) diff --git a/routers/api/bots/bots.go b/routers/api/bots/bots.go index 62991f07bc..3c761eeffa 100644 --- a/routers/api/bots/bots.go +++ b/routers/api/bots/bots.go @@ -5,18 +5,32 @@ package bots import ( + "log" + "net/http" + "code.gitea.io/gitea/modules/web" + "code.gitea.io/gitea/routers/api/bots/grpc" ) +func grpcHandler(h http.Handler) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Println("protocol version:", r.Proto) + h.ServeHTTP(w, r) + }) +} + +func gRPCRouter(r *web.Route, fn grpc.RouteFn) { + p, h := fn() + r.Post(p+"{name}", grpcHandler(h)) +} + func Routes(r *web.Route) { // socket connection r.Get("/socket", socketServe) - // runner service - runnerServiceRoute(r) - // ping service - pingServiceRoute(r) - // health service - healthServiceRoute(r) - // grpcv1 and v1alpha service - grpcServiceRoute(r) + + gRPCRouter(r, grpc.V1Route) + gRPCRouter(r, grpc.V1AlphaRoute) + gRPCRouter(r, grpc.HealthRoute) + gRPCRouter(r, grpc.PingRoute) + gRPCRouter(r, grpc.RunnerRoute) } diff --git a/routers/api/bots/grpc.go b/routers/api/bots/grpc.go deleted file mode 100644 index 282aaa2980..0000000000 --- a/routers/api/bots/grpc.go +++ /dev/null @@ -1,32 +0,0 @@ -// 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 ( - "code.gitea.io/gitea/modules/web" - "gitea.com/gitea/proto-go/ping/v1/pingv1connect" - - "github.com/bufbuild/connect-go" - grpcreflect "github.com/bufbuild/connect-grpcreflect-go" -) - -func grpcServiceRoute(r *web.Route) { - compress1KB := connect.WithCompressMinBytes(1024) - - // grpcV1 - grpcPath, gHandler := grpcreflect.NewHandlerV1( - grpcreflect.NewStaticReflector(pingv1connect.PingServiceName), - compress1KB, - ) - - // grpcV1Alpha - grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha( - grpcreflect.NewStaticReflector(pingv1connect.PingServiceName), - compress1KB, - ) - - r.Post(grpcPath+"{name}", grpcHandler(gHandler)) - r.Post(grpcAlphaPath+"{name}", grpcHandler(gAlphaHandler)) -} diff --git a/routers/api/bots/grpc/grpc.go b/routers/api/bots/grpc/grpc.go new file mode 100644 index 0000000000..a59672f4f5 --- /dev/null +++ b/routers/api/bots/grpc/grpc.go @@ -0,0 +1,43 @@ +// 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 grpc + +import ( + "net/http" + + "gitea.com/gitea/proto-go/ping/v1/pingv1connect" + "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" + + "github.com/bufbuild/connect-go" + grpcreflect "github.com/bufbuild/connect-grpcreflect-go" + "google.golang.org/grpc/health/grpc_health_v1" +) + +// RouteFn gRPC route registration +type RouteFn func() (string, http.Handler) + +var compress1KB = connect.WithCompressMinBytes(1024) + +var allServices = []string{ + runnerv1connect.RunnerServiceName, + pingv1connect.PingServiceName, + grpc_health_v1.Health_ServiceDesc.ServiceName, +} + +func V1Route() (string, http.Handler) { + // grpcV1 + return grpcreflect.NewHandlerV1( + grpcreflect.NewStaticReflector(allServices...), + compress1KB, + ) +} + +func V1AlphaRoute() (string, http.Handler) { + // grpcV1Alpha + return grpcreflect.NewHandlerV1Alpha( + grpcreflect.NewStaticReflector(allServices...), + compress1KB, + ) +} diff --git a/routers/api/bots/grpc/health.go b/routers/api/bots/grpc/health.go new file mode 100644 index 0000000000..ec0dfc9d69 --- /dev/null +++ b/routers/api/bots/grpc/health.go @@ -0,0 +1,19 @@ +// 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 grpc + +import ( + "net/http" + + grpchealth "github.com/bufbuild/connect-grpchealth-go" +) + +func HealthRoute() (string, http.Handler) { + // grpcHealthCheck + return grpchealth.NewHandler( + grpchealth.NewStaticChecker(allServices...), + compress1KB, + ) +} diff --git a/routers/api/bots/grpc/ping.go b/routers/api/bots/grpc/ping.go new file mode 100644 index 0000000000..6e7b8886d8 --- /dev/null +++ b/routers/api/bots/grpc/ping.go @@ -0,0 +1,21 @@ +// 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 grpc + +import ( + "net/http" + + "code.gitea.io/gitea/routers/api/bots/ping" + "gitea.com/gitea/proto-go/ping/v1/pingv1connect" +) + +func PingRoute() (string, http.Handler) { + pingService := &ping.Service{} + + return pingv1connect.NewPingServiceHandler( + pingService, + compress1KB, + ) +} diff --git a/routers/api/bots/grpc/runner.go b/routers/api/bots/grpc/runner.go new file mode 100644 index 0000000000..496820f91a --- /dev/null +++ b/routers/api/bots/grpc/runner.go @@ -0,0 +1,21 @@ +// 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 grpc + +import ( + "net/http" + + "code.gitea.io/gitea/routers/api/bots/runner" + "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" +) + +func RunnerRoute() (string, http.Handler) { + runnerService := &runner.Service{} + + return runnerv1connect.NewRunnerServiceHandler( + runnerService, + compress1KB, + ) +} diff --git a/routers/api/bots/handler.go b/routers/api/bots/handler.go deleted file mode 100644 index 53016e5f34..0000000000 --- a/routers/api/bots/handler.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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 ( - "net/http" - - "code.gitea.io/gitea/modules/log" -) - -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) - }) -} diff --git a/routers/api/bots/health.go b/routers/api/bots/health.go deleted file mode 100644 index 46eb5c7eeb..0000000000 --- a/routers/api/bots/health.go +++ /dev/null @@ -1,29 +0,0 @@ -// 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 ( - "code.gitea.io/gitea/modules/web" - "gitea.com/gitea/proto-go/ping/v1/pingv1connect" - "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" - - "github.com/bufbuild/connect-go" - grpchealth "github.com/bufbuild/connect-grpchealth-go" -) - -func healthServiceRoute(r *web.Route) { - compress1KB := connect.WithCompressMinBytes(1024) - - // grpcHealthCheck - grpcHealthPath, gHealthHandler := grpchealth.NewHandler( - grpchealth.NewStaticChecker( - runnerv1connect.RunnerServiceName, - pingv1connect.PingServiceName, - ), - compress1KB, - ) - - r.Post(grpcHealthPath+"{name}", grpcHandler(gHealthHandler)) -} diff --git a/routers/api/bots/ping.go b/routers/api/bots/ping/ping.go similarity index 64% rename from routers/api/bots/ping.go rename to routers/api/bots/ping/ping.go index efbc416657..d8abe1f39a 100644 --- a/routers/api/bots/ping.go +++ b/routers/api/bots/ping/ping.go @@ -2,23 +2,21 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package bots +package ping import ( "context" "fmt" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/web" pingv1 "gitea.com/gitea/proto-go/ping/v1" - "gitea.com/gitea/proto-go/ping/v1/pingv1connect" "github.com/bufbuild/connect-go" ) -type PingService struct{} +type Service struct{} -func (s *PingService) Ping( +func (s *Service) Ping( ctx context.Context, req *connect.Request[pingv1.PingRequest], ) (*connect.Response[pingv1.PingResponse], error) { @@ -31,15 +29,3 @@ func (s *PingService) Ping( res.Header().Set("Gitea-Version", "v1") return res, nil } - -func pingServiceRoute(r *web.Route) { - compress1KB := connect.WithCompressMinBytes(1024) - - pingService := &PingService{} - connectPath, connecthandler := pingv1connect.NewPingServiceHandler( - pingService, - compress1KB, - ) - - r.Post(connectPath+"{name}", grpcHandler(connecthandler)) -} diff --git a/routers/api/bots/runner.go b/routers/api/bots/runner/runner.go similarity index 66% rename from routers/api/bots/runner.go rename to routers/api/bots/runner/runner.go index a6cc32fbda..e07b7ce67a 100644 --- a/routers/api/bots/runner.go +++ b/routers/api/bots/runner/runner.go @@ -2,22 +2,20 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package bots +package runner import ( "context" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/web" runnerv1 "gitea.com/gitea/proto-go/runner/v1" - "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" "github.com/bufbuild/connect-go" ) -type RunnerService struct{} +type Service struct{} -func (s *RunnerService) Connect( +func (s *Service) Connect( ctx context.Context, req *connect.Request[runnerv1.ConnectRequest], ) (*connect.Response[runnerv1.ConnectResponse], error) { @@ -32,7 +30,7 @@ func (s *RunnerService) Connect( return res, nil } -func (s *RunnerService) Accept( +func (s *Service) Accept( ctx context.Context, req *connect.Request[runnerv1.AcceptRequest], ) (*connect.Response[runnerv1.AcceptResponse], error) { @@ -43,15 +41,3 @@ func (s *RunnerService) Accept( res.Header().Set("Gitea-Version", "runnerv1") return res, nil } - -func runnerServiceRoute(r *web.Route) { - compress1KB := connect.WithCompressMinBytes(1024) - - runnerService := &RunnerService{} - connectPath, connecthandler := runnerv1connect.NewRunnerServiceHandler( - runnerService, - compress1KB, - ) - - r.Post(connectPath+"{name}", grpcHandler(connecthandler)) -}