diff --git a/go.mod b/go.mod index 2e6d50f7d2..33d7858bf7 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/routers/api/bots/bots.go b/routers/api/bots/bots.go index 205749a4a2..0584cb86bd 100644 --- a/routers/api/bots/bots.go +++ b/routers/api/bots/bots.go @@ -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) } diff --git a/routers/api/bots/ping.go b/routers/api/bots/ping.go new file mode 100644 index 0000000000..b162630eb5 --- /dev/null +++ b/routers/api/bots/ping.go @@ -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)) +} diff --git a/routers/api/bots/process.go b/routers/api/bots/process.go index 6abb1f4399..53016e5f34 100644 --- a/routers/api/bots/process.go +++ b/routers/api/bots/process.go @@ -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) diff --git a/routers/api/bots/runner.go b/routers/api/bots/runner.go new file mode 100644 index 0000000000..4b0a7a6c9b --- /dev/null +++ b/routers/api/bots/runner.go @@ -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)) +}