Skip to content

Commit

Permalink
update buf and modules and move connect routes
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Sep 6, 2023
1 parent cd9b19e commit 7f0a594
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 100 deletions.
51 changes: 0 additions & 51 deletions backend/pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,20 @@
package api

import (
"context"
"errors"
"io/fs"
"math"

connect_go "connectrpc.com/connect"
"connectrpc.com/grpcreflect"
"github.com/bufbuild/protovalidate-go"
"github.com/cloudhut/common/logging"
"github.com/cloudhut/common/rest"
"go.uber.org/zap"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/protobuf/reflect/protoreflect"

"github.com/redpanda-data/console/backend/pkg/config"
"github.com/redpanda-data/console/backend/pkg/connect"
"github.com/redpanda-data/console/backend/pkg/console"
"github.com/redpanda-data/console/backend/pkg/embed"
"github.com/redpanda-data/console/backend/pkg/git"
"github.com/redpanda-data/console/backend/pkg/protogen/redpanda/api/console/v1alpha/consolev1alphaconnect"
"github.com/redpanda-data/console/backend/pkg/redpanda"
"github.com/redpanda-data/console/backend/pkg/version"
)
Expand Down Expand Up @@ -129,27 +122,6 @@ func (api *API) Start() {

mux := api.routes()

v, err := protovalidate.New()
if err != nil {
api.Logger.Fatal("failed to create proto validator", zap.Error(err))
}

interceptors := []connect_go.Interceptor{}

// we want the actual request validation after all authorization and permission checks
interceptors = append(interceptors, NewRequestValidationInterceptor(api.Logger, v))

// Connect service(s)
mux.Mount(consolev1alphaconnect.NewConsoleServiceHandler(
api,
connect_go.WithInterceptors(interceptors...),
))

// Connect reflection
reflector := grpcreflect.NewStaticReflector(consolev1alphaconnect.ConsoleServiceName)
mux.Mount(grpcreflect.NewHandlerV1(reflector))
mux.Mount(grpcreflect.NewHandlerV1Alpha(reflector))

// Server
api.server, err = rest.NewServer(&api.Cfg.REST.Config, api.Logger, mux)
if err != nil {
Expand All @@ -164,26 +136,3 @@ func (api *API) Start() {
api.Logger.Fatal("REST Server returned an error", zap.Error(err))
}
}

func NewRequestValidationInterceptor(logger *zap.Logger, validator *protovalidate.Validator) connect_go.UnaryInterceptorFunc {
interceptor := func(next connect_go.UnaryFunc) connect_go.UnaryFunc {
return connect_go.UnaryFunc(func(
ctx context.Context,
req connect_go.AnyRequest,
) (connect_go.AnyResponse, error) {
msg, ok := req.Any().(protoreflect.ProtoMessage)
if !ok {
return nil, connect_go.NewError(connect_go.CodeInvalidArgument, errors.New("request is not a protocol buffer message"))
}

err := validator.Validate(msg)
if err != nil {
return nil, err
}

return next(ctx, req)
})
}

return connect_go.UnaryInterceptorFunc(interceptor)
}
52 changes: 52 additions & 0 deletions backend/pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
package api

import (
"context"
"errors"
"net/http"

connect_go "connectrpc.com/connect"
"connectrpc.com/grpcreflect"
"github.com/bufbuild/protovalidate-go"
"github.com/cloudhut/common/middleware"
"github.com/cloudhut/common/rest"
"github.com/go-chi/chi/v5"
chimiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"google.golang.org/protobuf/reflect/protoreflect"

"github.com/redpanda-data/console/backend/pkg/protogen/redpanda/api/console/v1alpha/consolev1alphaconnect"
"github.com/redpanda-data/console/backend/pkg/version"
)

Expand Down Expand Up @@ -157,6 +164,28 @@ func (api *API) routes() *chi.Mux {
api.Hooks.Route.ConfigAPIRouterPostRegistration(r)
})

// Connect RPC
v, err := protovalidate.New()
if err != nil {
api.Logger.Fatal("failed to create proto validator", zap.Error(err))
}

interceptors := []connect_go.Interceptor{}

// we want the actual request validation after all authorization and permission checks
interceptors = append(interceptors, NewRequestValidationInterceptor(api.Logger, v))

// Connect service(s)
router.Mount(consolev1alphaconnect.NewConsoleServiceHandler(
api,
connect_go.WithInterceptors(interceptors...),
))

// Connect reflection
reflector := grpcreflect.NewStaticReflector(consolev1alphaconnect.ConsoleServiceName)
router.Mount(grpcreflect.NewHandlerV1(reflector))
router.Mount(grpcreflect.NewHandlerV1Alpha(reflector))

if api.Cfg.ServeFrontend {
// SPA Files
router.Group(func(r chi.Router) {
Expand All @@ -176,3 +205,26 @@ func (api *API) routes() *chi.Mux {

return baseRouter
}

func NewRequestValidationInterceptor(logger *zap.Logger, validator *protovalidate.Validator) connect_go.UnaryInterceptorFunc {
interceptor := func(next connect_go.UnaryFunc) connect_go.UnaryFunc {
return connect_go.UnaryFunc(func(
ctx context.Context,
req connect_go.AnyRequest,
) (connect_go.AnyResponse, error) {
msg, ok := req.Any().(protoreflect.ProtoMessage)
if !ok {
return nil, connect_go.NewError(connect_go.CodeInvalidArgument, errors.New("request is not a protocol buffer message"))
}

err := validator.Validate(msg)
if err != nil {
return nil, err
}

return next(ctx, req)
})
}

return connect_go.UnaryInterceptorFunc(interceptor)
}
5 changes: 3 additions & 2 deletions backend/pkg/kafka/testdata/proto/gen/index/v1/data.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/kafka/testdata/proto/gen/shop/v1/order.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/kafka/testdata/proto/gen/shop/v2/address.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/kafka/testdata/proto/gen/shop/v2/customer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/kafka/testdata/proto/gen/shop/v2/order.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/serde/testdata/proto/gen/index/v1/data.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/serde/testdata/proto/gen/shop/v1/order.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/serde/testdata/proto/gen/shop/v2/address.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/serde/testdata/proto/gen/shop/v2/customer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/pkg/serde/testdata/proto/gen/shop/v2/order.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ plugins:
opt: paths=source_relative
out: backend/pkg/protogen

- plugin: buf.build/connectrpc/go
- plugin: connect-go
opt:
- paths=source_relative
- require_unimplemented_servers=false
out: backend/pkg/protogen

# Typescript plugins
- plugin: buf.build/bufbuild/connect-es
- plugin: buf.build/bufbuild/es
opt:
- target=ts
- import_extension=
out: frontend/src/protogen

- plugin: buf.build/bufbuild/es
- plugin: buf.build/connectrpc/es
opt:
- target=ts
- import_extension=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-connect-es v0.13.0 with parameter "target=ts,import_extension="
// @generated by protoc-gen-connect-es v0.13.2 with parameter "target=ts,import_extension="
// @generated from file redpanda/api/console/v1alpha/list_messages.proto (package redpanda.api.console.v1alpha, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down
4 changes: 2 additions & 2 deletions proto/redpanda/api/console/v1alpha/list_messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ message KafkaRecordPayload {
bytes normalized_payload = 2; // Normilized user friendly representation of the payload.
PayloadEncoding encoding = 3; // Payload encoding if we have been able to detect.
int32 schema_id = 4; // Optionally, the schema ID used to deserialized the message.
int32 payload_size = 5; // Payload size in bytes.
int32 payload_size = 5; // Payload size in bytes.
bool is_payload_too_large = 6; // If payload is too large for deserialization.
repeated TroubleshootReport troubleshoot_report = 7; // Troubleshooting data for debugging.
}
Expand All @@ -98,7 +98,7 @@ message TroubleshootReport {
// KafkaRecordHeader is the record header.
message KafkaRecordHeader {
string key = 1; // Header key.
bytes value = 2; // Header value.
bytes value = 2; // Header value.
}

enum CompressionType {
Expand Down
Loading

0 comments on commit 7f0a594

Please sign in to comment.