Skip to content
Snippets Groups Projects
Commit 5949056b authored by ko_oler's avatar ko_oler
Browse files

Добавлено использование ConnectRPC для обеспечения взаимодействия через HTTP для сервиса "Лог"

parent b8b9109a
No related branches found
No related tags found
No related merge requests found
version: v2
plugins:
- local: protoc-gen-go
out: proto
opt: paths=source_relative
- local: protoc-gen-connect-go
out: proto
opt: paths=source_relative
- local: protoc-gen-go-grpc
out: proto
opt: paths=source_relative
buf.yaml 0 → 100644
# For details on buf.yaml configuration, visit https://buf.build/docs/configuration/v2/buf-yaml
# example command buf generate --path perxis-proto/proto/logs
version: v2
modules:
- path: perxis-proto/proto
lint:
use:
- STANDARD
breaking:
use:
- FILE
......@@ -3,6 +3,7 @@ module git.perx.ru/perxis/perxis-go
go 1.23.2
require (
connectrpc.com/connect v1.18.1
github.com/avast/retry-go/v4 v4.6.0
github.com/bep/gowebp v0.4.0
github.com/expr-lang/expr v1.16.9
......
......@@ -3,8 +3,10 @@ package logs
import (
"context"
"connectrpc.com/connect"
errorsgrpc "git.perx.ru/perxis/perxis-go/pkg/errors/grpc"
pb "git.perx.ru/perxis/perxis-go/proto/logs"
"git.perx.ru/perxis/perxis-go/proto/logs/logsconnect"
"google.golang.org/grpc"
)
......@@ -52,7 +54,7 @@ func (c *Client) Delete(ctx context.Context, filter *Filter) error {
if filter != nil {
request.Filter = &pb.Filter{Q: filter.Q}
}
response, err := c.client.Delete(ctx, &pb.DeleteRequest{Filter: &pb.Filter{Q: filter.Q}})
response, err := c.client.Delete(ctx, request)
if err != nil {
return err
}
......@@ -61,3 +63,60 @@ func (c *Client) Delete(ctx context.Context, filter *Filter) error {
}
return nil
}
type ConnectClient struct {
client logsconnect.LogsServiceClient
}
// ConnectRPC implementation.
var _ Service = &ConnectClient{}
func NewConnectClient(conn logsconnect.LogsServiceClient) *ConnectClient {
return &ConnectClient{
client: conn,
}
}
func (c *ConnectClient) Log(ctx context.Context, entries []*Entry) error {
var pbEntries []*pb.LogEntry
for _, e := range entries {
pbEntries = append(pbEntries, EntryToPB(e))
}
response, err := c.client.Log(ctx, connect.NewRequest[pb.LogRequest](&pb.LogRequest{Entries: pbEntries}))
if err != nil {
return err
}
if response.Msg.GetError() != nil {
return errorsgrpc.ErrorFromProto(nil, response.Msg.GetError())
}
return nil
}
func (c *ConnectClient) Find(ctx context.Context, filter *Filter, opts *FindOptions) (*FindResult, error) {
request := FindRequestToPB(&FindRequest{Filter: filter, Options: opts})
response, err := c.client.Find(ctx, connect.NewRequest[pb.FindRequest](request))
if err != nil {
return nil, err
}
if response.Msg.GetError() != nil {
return nil, errorsgrpc.ErrorFromProto(nil, response.Msg.GetError())
}
return FindResultFromPB(response.Msg.GetResult()), nil
}
func (c *ConnectClient) Delete(ctx context.Context, filter *Filter) error {
request := new(pb.DeleteRequest)
if filter != nil {
request.Filter = &pb.Filter{Q: filter.Q}
}
response, err := c.client.Delete(ctx, connect.NewRequest[pb.DeleteRequest](request))
if err != nil {
return err
}
if response.Msg.GetError() != nil {
return errorsgrpc.ErrorFromProto(nil, response.Msg.GetError())
}
return nil
}
// Code generated by protoc-gen-connect-go. DO NOT EDIT.
//
// Source: logs/log_service.proto
package logsconnect
import (
connect "connectrpc.com/connect"
context "context"
errors "errors"
logs "git.perx.ru/perxis/perxis-go/proto/logs"
http "net/http"
strings "strings"
)
// This is a compile-time assertion to ensure that this generated file and the connect package are
// compatible. If you get a compiler error that this constant is not defined, this code was
// generated with a version of connect newer than the one compiled into your binary. You can fix the
// problem by either regenerating this code with an older version of connect or updating the connect
// version compiled into your binary.
const _ = connect.IsAtLeastVersion1_13_0
const (
// LogsServiceName is the fully-qualified name of the LogsService service.
LogsServiceName = "logs.LogsService"
)
// These constants are the fully-qualified names of the RPCs defined in this package. They're
// exposed at runtime as Spec.Procedure and as the final two segments of the HTTP route.
//
// Note that these are different from the fully-qualified method names used by
// google.golang.org/protobuf/reflect/protoreflect. To convert from these constants to
// reflection-formatted method names, remove the leading slash and convert the remaining slash to a
// period.
const (
// LogsServiceLogProcedure is the fully-qualified name of the LogsService's Log RPC.
LogsServiceLogProcedure = "/logs.LogsService/Log"
// LogsServiceFindProcedure is the fully-qualified name of the LogsService's Find RPC.
LogsServiceFindProcedure = "/logs.LogsService/Find"
// LogsServiceDeleteProcedure is the fully-qualified name of the LogsService's Delete RPC.
LogsServiceDeleteProcedure = "/logs.LogsService/Delete"
)
// LogsServiceClient is a client for the logs.LogsService service.
type LogsServiceClient interface {
// Метод для записи логов
Log(context.Context, *connect.Request[logs.LogRequest]) (*connect.Response[logs.LogResponse], error)
// Метод для поиска логов по заданным параметрам
Find(context.Context, *connect.Request[logs.FindRequest]) (*connect.Response[logs.FindResponse], error)
// Метод для удаления логов по заданным параметрам
Delete(context.Context, *connect.Request[logs.DeleteRequest]) (*connect.Response[logs.DeleteResponse], error)
}
// NewLogsServiceClient constructs a client for the logs.LogsService service. By default, it uses
// the Connect protocol with the binary Protobuf Codec, asks for gzipped responses, and sends
// uncompressed requests. To use the gRPC or gRPC-Web protocols, supply the connect.WithGRPC() or
// connect.WithGRPCWeb() options.
//
// The URL supplied here should be the base URL for the Connect or gRPC server (for example,
// http://api.acme.com or https://acme.com/grpc).
func NewLogsServiceClient(httpClient connect.HTTPClient, baseURL string, opts ...connect.ClientOption) LogsServiceClient {
baseURL = strings.TrimRight(baseURL, "/")
logsServiceMethods := logs.File_logs_log_service_proto.Services().ByName("LogsService").Methods()
return &logsServiceClient{
log: connect.NewClient[logs.LogRequest, logs.LogResponse](
httpClient,
baseURL+LogsServiceLogProcedure,
connect.WithSchema(logsServiceMethods.ByName("Log")),
connect.WithClientOptions(opts...),
),
find: connect.NewClient[logs.FindRequest, logs.FindResponse](
httpClient,
baseURL+LogsServiceFindProcedure,
connect.WithSchema(logsServiceMethods.ByName("Find")),
connect.WithClientOptions(opts...),
),
delete: connect.NewClient[logs.DeleteRequest, logs.DeleteResponse](
httpClient,
baseURL+LogsServiceDeleteProcedure,
connect.WithSchema(logsServiceMethods.ByName("Delete")),
connect.WithClientOptions(opts...),
),
}
}
// logsServiceClient implements LogsServiceClient.
type logsServiceClient struct {
log *connect.Client[logs.LogRequest, logs.LogResponse]
find *connect.Client[logs.FindRequest, logs.FindResponse]
delete *connect.Client[logs.DeleteRequest, logs.DeleteResponse]
}
// Log calls logs.LogsService.Log.
func (c *logsServiceClient) Log(ctx context.Context, req *connect.Request[logs.LogRequest]) (*connect.Response[logs.LogResponse], error) {
return c.log.CallUnary(ctx, req)
}
// Find calls logs.LogsService.Find.
func (c *logsServiceClient) Find(ctx context.Context, req *connect.Request[logs.FindRequest]) (*connect.Response[logs.FindResponse], error) {
return c.find.CallUnary(ctx, req)
}
// Delete calls logs.LogsService.Delete.
func (c *logsServiceClient) Delete(ctx context.Context, req *connect.Request[logs.DeleteRequest]) (*connect.Response[logs.DeleteResponse], error) {
return c.delete.CallUnary(ctx, req)
}
// LogsServiceHandler is an implementation of the logs.LogsService service.
type LogsServiceHandler interface {
// Метод для записи логов
Log(context.Context, *connect.Request[logs.LogRequest]) (*connect.Response[logs.LogResponse], error)
// Метод для поиска логов по заданным параметрам
Find(context.Context, *connect.Request[logs.FindRequest]) (*connect.Response[logs.FindResponse], error)
// Метод для удаления логов по заданным параметрам
Delete(context.Context, *connect.Request[logs.DeleteRequest]) (*connect.Response[logs.DeleteResponse], error)
}
// NewLogsServiceHandler builds an HTTP handler from the service implementation. It returns the path
// on which to mount the handler and the handler itself.
//
// By default, handlers support the Connect, gRPC, and gRPC-Web protocols with the binary Protobuf
// and JSON codecs. They also support gzip compression.
func NewLogsServiceHandler(svc LogsServiceHandler, opts ...connect.HandlerOption) (string, http.Handler) {
logsServiceMethods := logs.File_logs_log_service_proto.Services().ByName("LogsService").Methods()
logsServiceLogHandler := connect.NewUnaryHandler(
LogsServiceLogProcedure,
svc.Log,
connect.WithSchema(logsServiceMethods.ByName("Log")),
connect.WithHandlerOptions(opts...),
)
logsServiceFindHandler := connect.NewUnaryHandler(
LogsServiceFindProcedure,
svc.Find,
connect.WithSchema(logsServiceMethods.ByName("Find")),
connect.WithHandlerOptions(opts...),
)
logsServiceDeleteHandler := connect.NewUnaryHandler(
LogsServiceDeleteProcedure,
svc.Delete,
connect.WithSchema(logsServiceMethods.ByName("Delete")),
connect.WithHandlerOptions(opts...),
)
return "/logs.LogsService/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case LogsServiceLogProcedure:
logsServiceLogHandler.ServeHTTP(w, r)
case LogsServiceFindProcedure:
logsServiceFindHandler.ServeHTTP(w, r)
case LogsServiceDeleteProcedure:
logsServiceDeleteHandler.ServeHTTP(w, r)
default:
http.NotFound(w, r)
}
})
}
// UnimplementedLogsServiceHandler returns CodeUnimplemented from all methods.
type UnimplementedLogsServiceHandler struct{}
func (UnimplementedLogsServiceHandler) Log(context.Context, *connect.Request[logs.LogRequest]) (*connect.Response[logs.LogResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("logs.LogsService.Log is not implemented"))
}
func (UnimplementedLogsServiceHandler) Find(context.Context, *connect.Request[logs.FindRequest]) (*connect.Response[logs.FindResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("logs.LogsService.Find is not implemented"))
}
func (UnimplementedLogsServiceHandler) Delete(context.Context, *connect.Request[logs.DeleteRequest]) (*connect.Response[logs.DeleteResponse], error) {
return nil, connect.NewError(connect.CodeUnimplemented, errors.New("logs.LogsService.Delete is not implemented"))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment