mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-26 23:35:20 +02:00
chore(proto): Add ping service
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
0e79fc556a
commit
ef3a74a1ba
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.18
|
|||||||
require (
|
require (
|
||||||
code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b
|
code.gitea.io/gitea-vet v0.2.2-0.20220122151748-48ebc902541b
|
||||||
code.gitea.io/sdk/gitea v0.15.1
|
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
|
gitea.com/go-chi/binding v0.0.0-20220309004920-114340dabecb
|
||||||
codeberg.org/gusted/mcaptcha v0.0.0-20220723083913-4f3072e1d570
|
codeberg.org/gusted/mcaptcha v0.0.0-20220723083913-4f3072e1d570
|
||||||
gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681
|
gitea.com/go-chi/binding v0.0.0-20221013104517-b29891619681
|
||||||
|
@ -6,47 +6,13 @@ package bots
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"code.gitea.io/gitea/modules/web"
|
"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) {
|
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
|
// socket connection
|
||||||
r.Get("/socket", socketServe)
|
r.Get("/socket", socketServe)
|
||||||
// restful connection
|
// runner service
|
||||||
r.Post(path+"{name}", giteaHandler(handler))
|
runnerServiceRoute(r)
|
||||||
// grpc connection
|
// ping service
|
||||||
r.Post(grpcPath+"{name}", giteaHandler(gHandler))
|
pingServiceRoute(r)
|
||||||
r.Post(grpcAlphaPath+"{name}", giteaHandler(gAlphaHandler))
|
|
||||||
// healthy check connection
|
|
||||||
r.Post(grpcHealthPath+"{name}", giteaHandler(gHealthHandler))
|
|
||||||
}
|
}
|
||||||
|
67
routers/api/bots/ping.go
Normal file
67
routers/api/bots/ping.go
Normal file
@ -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))
|
||||||
|
}
|
@ -5,42 +5,12 @@
|
|||||||
package bots
|
package bots
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
v1 "gitea.com/gitea/proto/gen/proto/v1"
|
|
||||||
|
|
||||||
"github.com/bufbuild/connect-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type RunnerService struct{}
|
func grpcHandler(h http.Handler) http.HandlerFunc {
|
||||||
|
|
||||||
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 {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Info("Got connection: %v", r.Proto)
|
log.Info("Got connection: %v", r.Proto)
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
|
77
routers/api/bots/runner.go
Normal file
77
routers/api/bots/runner.go
Normal file
@ -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))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user