Skip to content

Commit

Permalink
Switch to connect rpc (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatgoel authored Sep 11, 2024
1 parent 5636575 commit dfa619f
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 256 deletions.
6 changes: 3 additions & 3 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: v2
plugins:
- remote: buf.build/protocolbuffers/go:v1.34.2
out: .
out: proto
opt: paths=source_relative
- remote: buf.build/grpc/go:v1.5.1
out: .
- remote: buf.build/connectrpc/go:v1.16.2
out: proto
opt: paths=source_relative
39 changes: 27 additions & 12 deletions cmd/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,52 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"log"
"net"
"log/slog"
"net/http"

"google.golang.org/grpc"
"connectrpc.com/grpchealth"
"connectrpc.com/grpcreflect"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"

"github.com/rajatgoel/gh-go/internal/frontend"
"github.com/rajatgoel/gh-go/internal/sqlbackend"
frontendpb "github.com/rajatgoel/gh-go/proto/frontend/v1"
frontendv1connect "github.com/rajatgoel/gh-go/proto/frontend/v1/v1connect"
)

func main() {
port := flag.Int("port", 50051, "The server port")
port := flag.Int("port", 5051, "The server port")
flag.Parse()

ctx := context.Background()
backend, err := sqlbackend.New(ctx)
if err != nil {
log.Fatalf("failed to create backend: %v", err)
}
handler := frontend.New(backend)
path, handler := frontendv1connect.NewFrontendServiceHandler(frontend.New(backend))

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
mux := http.NewServeMux()
mux.Handle(path, handler)

// health check
checker := grpchealth.NewStaticChecker(frontendv1connect.FrontendServiceName)
mux.Handle(grpchealth.NewHandler(checker))

// reflect
reflector := grpcreflect.NewStaticReflector(frontendv1connect.FrontendServiceName)
mux.Handle(grpcreflect.NewHandlerV1(reflector))

server := &http.Server{
Addr: fmt.Sprintf("127.0.0.1:%d", *port),
Handler: h2c.NewHandler(mux, &http2.Server{}),
}

s := grpc.NewServer()
frontendpb.RegisterFrontendServiceServer(s, handler)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
slog.Info("starting server", "port", *port, "path", path)
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("failed to start server: %v", err)
}
}
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module github.com/rajatgoel/gh-go
go 1.23.0

require (
connectrpc.com/connect v1.16.2
connectrpc.com/grpchealth v1.3.0
connectrpc.com/grpcreflect v1.2.0
github.com/bufbuild/buf v1.36.0
github.com/golangci/golangci-lint v1.60.0
github.com/sqlc-dev/sqlc v1.26.0
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.28.0
golang.org/x/tools v0.24.0
golang.org/x/vuln v1.1.2
google.golang.org/grpc v1.64.1
google.golang.org/protobuf v1.34.2
modernc.org/sqlite v1.30.1
)
Expand All @@ -20,7 +23,6 @@ require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.2-20240717164558-a6c49f84cc0f.2 // indirect
buf.build/gen/go/bufbuild/registry/connectrpc/go v1.16.2-20240801134127-09fbc17f7c9e.1 // indirect
buf.build/gen/go/bufbuild/registry/protocolbuffers/go v1.34.2-20240801134127-09fbc17f7c9e.2 // indirect
connectrpc.com/connect v1.16.2 // indirect
connectrpc.com/otelconnect v0.7.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/4meepo/tagalign v1.3.4 // indirect
Expand Down Expand Up @@ -259,14 +261,14 @@ require (
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/telemetry v0.0.0-20240522233618-39ace7a40ae7 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240812133136-8ffd90a71988 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240812133136-8ffd90a71988 // indirect
google.golang.org/grpc v1.64.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ buf.build/gen/go/bufbuild/registry/protocolbuffers/go v1.34.2-20240801134127-09f
buf.build/gen/go/bufbuild/registry/protocolbuffers/go v1.34.2-20240801134127-09fbc17f7c9e.2/go.mod h1:psseUmlKRo9v5LZJtR/aTpdTLuyp9o3X7rnLT87SZEo=
connectrpc.com/connect v1.16.2 h1:ybd6y+ls7GOlb7Bh5C8+ghA6SvCBajHwxssO2CGFjqE=
connectrpc.com/connect v1.16.2/go.mod h1:n2kgwskMHXC+lVqb18wngEpF95ldBHXjZYJussz5FRc=
connectrpc.com/grpchealth v1.3.0 h1:FA3OIwAvuMokQIXQrY5LbIy8IenftksTP/lG4PbYN+E=
connectrpc.com/grpchealth v1.3.0/go.mod h1:3vpqmX25/ir0gVgW6RdnCPPZRcR6HvqtXX5RNPmDXHM=
connectrpc.com/grpcreflect v1.2.0 h1:Q6og1S7HinmtbEuBvARLNwYmTbhEGRpHDhqrPNlmK+U=
connectrpc.com/grpcreflect v1.2.0/go.mod h1:nwSOKmE8nU5u/CidgHtPYk1PFI3U9ignz7iDMxOYkSY=
connectrpc.com/otelconnect v0.7.1 h1:scO5pOb0i4yUE66CnNrHeK1x51yq0bE0ehPg6WvzXJY=
connectrpc.com/otelconnect v0.7.1/go.mod h1:dh3bFgHBTb2bkqGCeVVOtHJreSns7uu9wwL2Tbz17ms=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
Expand Down
23 changes: 13 additions & 10 deletions internal/frontend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@ package frontend
import (
"context"

"connectrpc.com/connect"

"github.com/rajatgoel/gh-go/internal/sqlbackend"
frontendpb "github.com/rajatgoel/gh-go/proto/frontend/v1"
frontendv1connect "github.com/rajatgoel/gh-go/proto/frontend/v1/v1connect"
)

type handler struct {
frontendpb.UnimplementedFrontendServiceServer
frontendv1connect.UnimplementedFrontendServiceHandler

backend sqlbackend.Backend
}

func (h *handler) Put(
ctx context.Context,
req *frontendpb.PutRequest,
) (*frontendpb.PutResponse, error) {
h.backend.Put(ctx, req.Key, req.Value)
return &frontendpb.PutResponse{}, nil
req *connect.Request[frontendpb.PutRequest],
) (*connect.Response[frontendpb.PutResponse], error) {
h.backend.Put(ctx, req.Msg.Key, req.Msg.Value)
return connect.NewResponse(&frontendpb.PutResponse{}), nil
}

func (h *handler) Get(
ctx context.Context,
req *frontendpb.GetRequest,
) (*frontendpb.GetResponse, error) {
value := h.backend.Get(ctx, req.Key)
return &frontendpb.GetResponse{Value: value}, nil
req *connect.Request[frontendpb.GetRequest],
) (*connect.Response[frontendpb.GetResponse], error) {
value := h.backend.Get(ctx, req.Msg.Key)
return connect.NewResponse(&frontendpb.GetResponse{Value: value}), nil
}

func New(backend sqlbackend.Backend) frontendpb.FrontendServiceServer {
func New(backend sqlbackend.Backend) frontendv1connect.FrontendServiceHandler {
return &handler{backend: backend}
}
28 changes: 22 additions & 6 deletions itest/frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@ package itest

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"connectrpc.com/connect"
"github.com/stretchr/testify/require"

"github.com/rajatgoel/gh-go/internal/frontend"
"github.com/rajatgoel/gh-go/internal/sqlbackend"
frontendpb "github.com/rajatgoel/gh-go/proto/frontend/v1"
frontendv1connect "github.com/rajatgoel/gh-go/proto/frontend/v1/v1connect"
)

func TestStub(t *testing.T) {
b, err := sqlbackend.New(context.Background())
require.NoError(t, err)
h := frontend.New(b)

mux := http.NewServeMux()
mux.Handle(frontendv1connect.NewFrontendServiceHandler(frontend.New(b)))
server := httptest.NewServer(mux)
t.Cleanup(func() { server.Close() })

key, value := int64(1), "value"
_, err = h.Put(context.Background(), &frontendpb.PutRequest{
client := frontendv1connect.NewFrontendServiceClient(http.DefaultClient, server.URL)

resp, err := client.Get(context.Background(), connect.NewRequest(&frontendpb.GetRequest{
Key: key,
}))
require.NoError(t, err)
require.Empty(t, resp.Msg.Value)

_, err = client.Put(context.Background(), connect.NewRequest(&frontendpb.PutRequest{
Key: key,
Value: value,
})
}))
require.NoError(t, err)

resp, err := h.Get(context.Background(), &frontendpb.GetRequest{
resp, err = client.Get(context.Background(), connect.NewRequest(&frontendpb.GetRequest{
Key: key,
})
}))
require.NoError(t, err)
require.Equal(t, value, resp.Value)
require.Equal(t, value, resp.Msg.Value)
}
Loading

0 comments on commit dfa619f

Please sign in to comment.