chore(bots): re-structure grpc folder.

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi.Wu 2022-08-22 21:12:39 +08:00 committed by Jason Song
parent 2cba8fafd8
commit e4e41a8065
10 changed files with 133 additions and 122 deletions

View File

@ -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)
}

View File

@ -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))
}

View File

@ -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,
)
}

View File

@ -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,
)
}

View File

@ -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,
)
}

View File

@ -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,
)
}

View File

@ -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)
})
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}