chore: update grpc router path

This commit is contained in:
Jason Song 2022-11-22 18:24:01 +08:00
parent 48f5c152b9
commit e346581344
4 changed files with 24 additions and 23 deletions

View File

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

View File

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

View File

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

View File

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