From e34658134495aa0c650965dc2e2b8fa13298a950 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 22 Nov 2022 18:24:01 +0800 Subject: [PATCH] chore: update grpc router path --- routers/api/bots/bots.go | 33 +++++++++++++------------------ routers/api/bots/bots_test.go | 4 ++-- routers/api/bots/ping/testping.go | 3 ++- routers/init.go | 7 ++++++- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/routers/api/bots/bots.go b/routers/api/bots/bots.go index 4ccef4b339..0a7e857013 100644 --- a/routers/api/bots/bots.go +++ b/routers/api/bots/bots.go @@ -5,31 +5,26 @@ package bots import ( + "context" "net/http" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/bots/grpc" ) -func grpcHandler(h http.Handler) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - log.Trace("protocol version: %v", r.Proto) - h.ServeHTTP(w, r) +func Routes(_ context.Context, prefix string) *web.Route { + m := web.NewRoute() + + for _, fn := range []grpc.RouteFn{ + grpc.V1Route, + grpc.V1AlphaRoute, + grpc.HealthRoute, + grpc.PingRoute, + grpc.RunnerRoute, + } { + path, handler := fn() + m.Post(path+"*", http.StripPrefix(prefix, handler).ServeHTTP) } -} -func gRPCRouter(r *web.Route, fn grpc.RouteFn) { - p, h := fn() - r.Post(p+"{name}", grpcHandler(h)) -} - -func Routes(r *web.Route) *web.Route { - gRPCRouter(r, grpc.V1Route) - gRPCRouter(r, grpc.V1AlphaRoute) - gRPCRouter(r, grpc.HealthRoute) - gRPCRouter(r, grpc.PingRoute) - gRPCRouter(r, grpc.RunnerRoute) - - return r + return m } diff --git a/routers/api/bots/bots_test.go b/routers/api/bots/bots_test.go index 6ccb0ea3ec..de074cc050 100644 --- a/routers/api/bots/bots_test.go +++ b/routers/api/bots/bots_test.go @@ -5,12 +5,12 @@ package bots import ( + "context" "testing" - "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/bots/ping" ) func TestPingService(t *testing.T) { - ping.MainServiceTest(t, Routes(web.NewRoute())) + ping.MainServiceTest(t, Routes(context.Background(), "")) } diff --git a/routers/api/bots/ping/testping.go b/routers/api/bots/ping/testping.go index acfbf799a1..128306cffc 100644 --- a/routers/api/bots/ping/testping.go +++ b/routers/api/bots/ping/testping.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/bots-proto-go/ping/v1/pingv1connect" "github.com/bufbuild/connect-go" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func MainServiceTest(t *testing.T, h http.Handler) { @@ -46,7 +47,7 @@ func MainServiceTest(t *testing.T, h http.Handler) { result, err := client.Ping(context.Background(), connect.NewRequest(&pingv1.PingRequest{ Data: "foobar", })) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "Hello, foobar!", result.Msg.Data) } }) diff --git a/routers/init.go b/routers/init.go index 329be7d58a..0c9faa37e2 100644 --- a/routers/init.go +++ b/routers/init.go @@ -202,6 +202,11 @@ func NormalRoutes(ctx context.Context) *web.Route { // This implements the OCI API (Note this is not preceded by /api but is instead /v2) r.Mount("/v2", packages_router.ContainerRoutes(ctx)) } - _ = bots_router.Routes(r) + + if setting.Bots.Enabled { + prefix := "/api/bots" + r.Mount(prefix, bots_router.Routes(ctx, prefix)) + } + return r }