Skip to content

Commit

Permalink
Merge pull request #14 from w-h-a/broker-endpoints
Browse files Browse the repository at this point in the history
tests: e2e pubsub
  • Loading branch information
w-h-a authored Sep 14, 2024
2 parents 76aea36 + bf2d8c2 commit 9144426
Show file tree
Hide file tree
Showing 22 changed files with 604 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmd/handlers/publish.go → cmd/http/publish.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handlers
package http

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion cmd/handlers/state.go → cmd/http/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handlers
package http

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion cmd/handlers/utils.go → cmd/http/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handlers
package http

import (
"encoding/json"
Expand Down
22 changes: 11 additions & 11 deletions cmd/controllers/publish.go → cmd/rpc/publish.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package rpc

import (
"context"
Expand All @@ -10,15 +10,15 @@ import (
"github.com/w-h-a/pkg/utils/errorutils"
)

type PublishController interface {
type PublishHandler interface {
Publish(ctx context.Context, req *pb.PublishRequest, rsp *pb.PublishResponse) error
}

type publishController struct {
type publishHandler struct {
service sidecar.Sidecar
}

func (c *publishController) Publish(ctx context.Context, req *pb.PublishRequest, rsp *pb.PublishResponse) error {
func (c *publishHandler) Publish(ctx context.Context, req *pb.PublishRequest, rsp *pb.PublishResponse) error {
if req.Event == nil {
return errorutils.BadRequest("sidecar", "event is required")
}
Expand All @@ -38,18 +38,18 @@ func (c *publishController) Publish(ctx context.Context, req *pb.PublishRequest,
return nil
}

func NewPublishController(s sidecar.Sidecar) PublishController {
return &publishController{s}
func NewPublishHandler(s sidecar.Sidecar) PublishHandler {
return &publishHandler{s}
}

type Publish struct {
PublishController
PublishHandler
}

func RegisterPublishController(s server.Server, controller PublishController, opts ...server.ControllerOption) error {
return s.RegisterController(
s.NewController(
&Publish{controller},
func RegisterPublishHandler(s server.Server, handler PublishHandler, opts ...server.HandlerOption) error {
return s.Handle(
s.NewHandler(
&Publish{handler},
opts...,
),
)
Expand Down
28 changes: 14 additions & 14 deletions cmd/controllers/state.go → cmd/rpc/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package rpc

import (
"context"
Expand All @@ -10,18 +10,18 @@ import (
"github.com/w-h-a/pkg/utils/errorutils"
)

type StateController interface {
type StateHandler interface {
Post(ctx context.Context, req *pb.PostStateRequest, rsp *pb.PostStateResponse) error
List(ctx context.Context, req *pb.ListStateRequest, rsp *pb.ListStateResponse) error
Get(ctx context.Context, req *pb.GetStateRequest, rsp *pb.GetStateResponse) error
Delete(ctx context.Context, req *pb.DeleteStateRequest, rsp *pb.DeleteStateResponse) error
}

type stateController struct {
type stateHandler struct {
service sidecar.Sidecar
}

func (c *stateController) Post(ctx context.Context, req *pb.PostStateRequest, rsp *pb.PostStateResponse) error {
func (c *stateHandler) Post(ctx context.Context, req *pb.PostStateRequest, rsp *pb.PostStateResponse) error {
state := &sidecar.State{
StoreId: req.StoreId,
Records: DeserializeRecords(req.Records),
Expand All @@ -34,7 +34,7 @@ func (c *stateController) Post(ctx context.Context, req *pb.PostStateRequest, rs
return nil
}

func (c *stateController) List(ctx context.Context, req *pb.ListStateRequest, rsp *pb.ListStateResponse) error {
func (c *stateHandler) List(ctx context.Context, req *pb.ListStateRequest, rsp *pb.ListStateResponse) error {
recs, err := c.service.ListStateFromStore(req.StoreId)
if err != nil {
return errorutils.InternalServerError("sidecar", "failed to retrive state from store %s: %v", req.StoreId, err)
Expand All @@ -45,7 +45,7 @@ func (c *stateController) List(ctx context.Context, req *pb.ListStateRequest, rs
return nil
}

func (c *stateController) Get(ctx context.Context, req *pb.GetStateRequest, rsp *pb.GetStateResponse) error {
func (c *stateHandler) Get(ctx context.Context, req *pb.GetStateRequest, rsp *pb.GetStateResponse) error {
recs, err := c.service.SingleStateFromStore(req.StoreId, req.Key)
if err != nil && err == store.ErrRecordNotFound {
return errorutils.NotFound("sidecar", "there is no such record at store %s and key %s", req.StoreId, req.Key)
Expand All @@ -58,26 +58,26 @@ func (c *stateController) Get(ctx context.Context, req *pb.GetStateRequest, rsp
return nil
}

func (c *stateController) Delete(ctx context.Context, req *pb.DeleteStateRequest, rsp *pb.DeleteStateResponse) error {
func (c *stateHandler) Delete(ctx context.Context, req *pb.DeleteStateRequest, rsp *pb.DeleteStateResponse) error {
if err := c.service.RemoveStateFromStore(req.StoreId, req.Key); err != nil {
return errorutils.InternalServerError("sidecar", "failed to remove state from store %s and key %s: %v", req.StoreId, req.Key, err)
}

return nil
}

func NewStateController(s sidecar.Sidecar) StateController {
return &stateController{s}
func NewStateHandler(s sidecar.Sidecar) StateHandler {
return &stateHandler{s}
}

type State struct {
StateController
StateHandler
}

func RegisterStateController(s server.Server, controller StateController, opts ...server.ControllerOption) error {
return s.RegisterController(
s.NewController(
&State{controller},
func RegisterStateHandler(s server.Server, handler StateHandler, opts ...server.HandlerOption) error {
return s.Handle(
s.NewHandler(
&State{handler},
opts...,
),
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/controllers/utils.go → cmd/rpc/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package rpc

import (
pb "github.com/w-h-a/pkg/proto/sidecar"
Expand Down
16 changes: 8 additions & 8 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/w-h-a/pkg/store"
"github.com/w-h-a/pkg/telemetry/log"
"github.com/w-h-a/sidecar/cmd/config"
"github.com/w-h-a/sidecar/cmd/controllers"
"github.com/w-h-a/sidecar/cmd/handlers"
"github.com/w-h-a/sidecar/cmd/http"
"github.com/w-h-a/sidecar/cmd/rpc"
)

func run(ctx *cli.Context) {
Expand Down Expand Up @@ -100,8 +100,8 @@ func run(ctx *cli.Context) {
// create http server
router := mux.NewRouter()

publish := handlers.NewPublishHandler(service)
state := handlers.NewStateHandler(service)
publish := http.NewPublishHandler(service)
state := http.NewStateHandler(service)

router.Methods("POST").Path("/publish").HandlerFunc(publish.Handle)
router.Methods("POST").Path("/state/{storeId}").HandlerFunc(state.HandlePost)
Expand Down Expand Up @@ -130,16 +130,16 @@ func run(ctx *cli.Context) {

grpcServer := grpcserver.NewServer(serverOpts...)

controllers.RegisterStateController(
rpc.RegisterStateHandler(
grpcServer,
controllers.NewStateController(
rpc.NewStateHandler(
service,
),
)

controllers.RegisterPublishController(
rpc.RegisterPublishHandler(
grpcServer,
controllers.NewPublishController(
rpc.NewPublishHandler(
service,
),
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.15
github.com/w-h-a/pkg v0.28.0
github.com/w-h-a/pkg v0.29.0
google.golang.org/protobuf v1.34.2
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli v1.22.15 h1:nuqt+pdC/KqswQKhETJjo7pvn/k4xMUxgW6liI7XpnM=
github.com/urfave/cli v1.22.15/go.mod h1:wSan1hmo5zeyLGBjRJbzRTNk8gwoYa2B9n4q9dmRIc0=
github.com/w-h-a/pkg v0.28.0 h1:oM3FEdyPBq+X5f9vzeWPZxou+HQSZjxYY7E5p3+DcLw=
github.com/w-h-a/pkg v0.28.0/go.mod h1:S/BTwm6C36WyXsi+cSYMhPDvwk3y4K3LgUGz2GTIF/4=
github.com/w-h-a/pkg v0.29.0 h1:AWSzQs1/Jt9O4VYgj9OJ9kh7OvJCHw3t0wu5Zpf1Ip8=
github.com/w-h-a/pkg v0.29.0/go.mod h1:VS8Rv3c6ucOFYOE+rIVXFtEcWPiblpG28HlQIo7Wg+A=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 h1:TT4fX+nBOA/+LUkobKGW1ydGcn+G3vRw9+g5HwCphpk=
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/helloworld/resources/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
HTTP_ADDRESS: ':3501'
RPC_ADDRESS: ':50001'
SERVICE_NAME: 'localhost'
SERVICE_PORT: '3001'
SERVICE_PORT: '3000'
SERVICE_PROTOCOL: 'http'
STORE: 'memory'
BROKER: 'memory'
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/helloworld/resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type TestCommandRequest struct {
Message string `json:"message,omitempty"`
}

func appRouter() *mux.Router {
func serviceRouter() *mux.Router {
router := mux.NewRouter()

router.HandleFunc("/", indexHandler).Methods("GET")
Expand Down Expand Up @@ -83,7 +83,7 @@ func green(commandRequest TestCommandRequest) (int, string) {
func main() {
log.Printf("hello world test service is listening on port %d", servicePort)

if err := http.ListenAndServe(fmt.Sprintf(":%d", servicePort), appRouter()); err != nil {
if err := http.ListenAndServe(fmt.Sprintf(":%d", servicePort), serviceRouter()); err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit 9144426

Please sign in to comment.