-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Play with grpc/connect and refactor echo server
- Loading branch information
Showing
53 changed files
with
2,786 additions
and
806 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.gen.go | ||
/testdata/ | ||
/scripts/ | ||
/tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
.build/ | ||
.DS_Store | ||
.idea/ | ||
.vscode/ | ||
*.dll | ||
*.dylib | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.out | ||
*.so | ||
*.dylib | ||
*.test | ||
*.out | ||
/vendor/ | ||
/Godeps/ | ||
.vscode/ | ||
/node_modules/ | ||
/coverage/ | ||
NOTES.md | ||
.build/ | ||
.DS_Store | ||
/node_modules/ | ||
/vendor/ | ||
NOTES.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,4 @@ | ||
--- | ||
run: | ||
modules-download-mode: vendor | ||
|
||
output: | ||
format: colored-line-number | ||
|
||
linters: | ||
disable-all: true | ||
enable: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
--- | ||
doublestar: true | ||
exclude: | ||
- "./vendor/**/*" | ||
formatter: | ||
include_document_start: true | ||
retain_line_breaks: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
version: v2 | ||
managed: | ||
enabled: true | ||
disable: | ||
- file_option: go_package | ||
module: buf.build/bufbuild/protovalidate | ||
plugins: | ||
# grpc | ||
- remote: buf.build/grpc/go | ||
out: proto | ||
opt: | ||
- paths=source_relative | ||
- require_unimplemented_servers=false | ||
- remote: buf.build/protocolbuffers/go | ||
out: proto | ||
opt: paths=source_relative | ||
# connect | ||
- remote: buf.build/connectrpc/go:v1.16.2 | ||
out: proto | ||
opt: | ||
- paths=source_relative |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Generated by buf. DO NOT EDIT. | ||
version: v2 | ||
deps: | ||
- name: buf.build/bufbuild/protovalidate | ||
commit: a3320276596649bcad929ac829d451f4 | ||
digest: b5:285a6d3a423b195a21f45aacc97ee222ac09cfb01a42f0d546aa51d92177b0b9d00eb9ae93e72dabbbefdc77f35a4c7a11f15d913cc08da764fcb6071f85d148 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
version: v2 | ||
modules: | ||
- path: proto | ||
deps: | ||
- buf.build/bufbuild/protovalidate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"connectrpc.com/connect" | ||
|
||
"github.com/pcriv/mancala/internal/mancala" | ||
"github.com/pcriv/mancala/internal/protomap" | ||
"github.com/pcriv/mancala/proto" | ||
"github.com/pcriv/mancala/proto/protoconnect" | ||
googleproto "google.golang.org/protobuf/proto" | ||
) | ||
|
||
var _ protoconnect.ServiceHandler = handler{} | ||
|
||
type ( | ||
requestValidator interface { | ||
Validate(msg googleproto.Message) error | ||
} | ||
|
||
handler struct { | ||
service mancala.Service | ||
validator requestValidator | ||
} | ||
) | ||
|
||
func (h handler) CreateGame(ctx context.Context, in *connect.Request[proto.CreateGameRequest]) (*connect.Response[proto.CreateGameResponse], error) { | ||
err := h.validator.Validate(in.Msg) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInvalidArgument, err) | ||
} | ||
|
||
g, err := h.service.CreateGame(ctx, in.Msg.Player1, in.Msg.Player2) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, err) | ||
} | ||
return connect.NewResponse(&proto.CreateGameResponse{ | ||
CreatedGame: protomap.Game(g), | ||
}), nil | ||
} | ||
|
||
func (h handler) FindGame(ctx context.Context, in *connect.Request[proto.FindGameRequest]) (*connect.Response[proto.FindGameResponse], error) { | ||
err := h.validator.Validate(in.Msg) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInvalidArgument, err) | ||
} | ||
|
||
g, err := h.service.FindGame(ctx, in.Msg.Id) | ||
if err != nil { | ||
switch { | ||
case errors.Is(err, mancala.ErrGameNotFound): | ||
return nil, connect.NewError(connect.CodeNotFound, err) | ||
default: | ||
return nil, connect.NewError(connect.CodeInternal, err) | ||
} | ||
} | ||
|
||
return connect.NewResponse(&proto.FindGameResponse{ | ||
Game: protomap.Game(g), | ||
}), nil | ||
} | ||
|
||
func (h handler) ExecutePlay(ctx context.Context, in *connect.Request[proto.ExecutePlayRequest]) (*connect.Response[proto.ExecutePlayResponse], error) { | ||
err := h.validator.Validate(in.Msg) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInvalidArgument, err) | ||
} | ||
|
||
g, err := h.service.ExecutePlay(ctx, in.Msg.GameId, in.Msg.PitIndex) | ||
if err != nil { | ||
switch { | ||
case errors.Is(err, mancala.ErrGameNotFound): | ||
return nil, connect.NewError(connect.CodeNotFound, err) | ||
case errors.Is(err, mancala.ErrInvalidPlay): | ||
return nil, connect.NewError(connect.CodeInvalidArgument, err) | ||
default: | ||
return nil, connect.NewError(connect.CodeInternal, err) | ||
} | ||
} | ||
|
||
return connect.NewResponse(&proto.ExecutePlayResponse{ | ||
PlayedPitIndex: in.Msg.PitIndex, | ||
Game: protomap.Game(g), | ||
}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/bufbuild/protovalidate-go" | ||
"github.com/caarlos0/env/v11" | ||
"github.com/pcriv/mancala/internal/mancala" | ||
"github.com/pcriv/mancala/proto/protoconnect" | ||
"github.com/redis/go-redis/v9" | ||
"golang.org/x/net/http2" | ||
"golang.org/x/net/http2/h2c" | ||
"golang.org/x/sync/errgroup" | ||
|
||
redisstore "github.com/pcriv/mancala/internal/store/redis" | ||
) | ||
|
||
type envConfig struct { | ||
Env string `env:"ENV" envDefault:"local"` | ||
LogLevel string `env:"LOG_LEVEL" envDefault:"debug"` | ||
RedisURL string `env:"REDIS_URL,required"` | ||
Address string `env:"ADDRESS" envDefault:":50051"` | ||
} | ||
|
||
func main() { | ||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) | ||
defer cancel() | ||
|
||
err := run(ctx) | ||
if err != nil && !errors.Is(err, context.Canceled) { | ||
slog.Error("error running application", slog.String("error", err.Error())) | ||
os.Exit(1) | ||
} | ||
|
||
slog.Info("closing server gracefully") | ||
} | ||
|
||
func run(ctx context.Context) error { | ||
cfg := envConfig{} | ||
if err := env.Parse(&cfg); err != nil { | ||
return fmt.Errorf("unable to parse config: %w", err) | ||
} | ||
|
||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil))) | ||
|
||
redisClient, err := newRedisClient(ctx, cfg.RedisURL) | ||
if err != nil { | ||
return err | ||
} | ||
gameStore := redisstore.NewGameStore(redisClient) | ||
|
||
validator, err := protovalidate.New() | ||
if err != nil { | ||
return fmt.Errorf("failed to create validator: %w", err) | ||
} | ||
|
||
path, handler := protoconnect.NewServiceHandler( | ||
handler{ | ||
service: mancala.NewService(gameStore), | ||
validator: validator, | ||
}, | ||
) | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle(path, handler) | ||
|
||
// Use h2c so we can serve HTTP/2 without TLS. | ||
srv := http.Server{ | ||
Addr: cfg.Address, | ||
Handler: h2c.NewHandler(mux, &http2.Server{}), | ||
} | ||
|
||
g, ctx := errgroup.WithContext(ctx) | ||
|
||
g.Go(func() error { | ||
slog.Info("starting connect server on address", slog.String("address", cfg.Address)) | ||
|
||
if err := srv.ListenAndServe(); err != nil { | ||
return fmt.Errorf("failed to listen and serve connect service: %w", err) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
g.Go(func() error { | ||
<-ctx.Done() | ||
|
||
if err := srv.Close(); err != nil { | ||
return fmt.Errorf("failed to close server: %w", err) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return g.Wait() | ||
} | ||
|
||
func newRedisClient(ctx context.Context, url string) (*redis.Client, error) { | ||
options, err := redis.ParseURL(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := redis.NewClient(options) | ||
_, err = client.Ping(ctx).Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return client, nil | ||
} |
Oops, something went wrong.