Skip to content

Commit

Permalink
refactor: use grpc-gateway to register http deck service
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Jun 5, 2022
1 parent db35cb3 commit 8f87289
Show file tree
Hide file tree
Showing 36 changed files with 2,208 additions and 2,317 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
-
name: Set up extra tools
run: |
go install github.com/swaggo/swag/cmd/swag@latest
go install github.com/magefile/mage@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/onsi/ginkgo/v2/ginkgo@latest
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

# Kreya
*.krproj
transport/grpc/DeckService
transport/grpc/DeckService/*
transport/DeckService
transport/DeckService/*
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ Deckr provides a REST/gRPC API to simulate a deck of cards.
- [Requirements](#requirements)
- [Mage Targets](#mage-targets)
- [Example](#example)
- [Generate docs](#generate-docs)
- [Generate stubs from proto files](#generate-stubs-from-proto-files)
- [Run tests with race detector](#run-tests-with-race-detector)
- [Build deckr for MacOS M1](#build-deckr-for-macos-m1)
- [Install](#install)
- [Usage](#usage)
- [REST/gRPC API server](#rest-api-server)
- [REST/gRPC API documentation](#rest-api-documentation)
- [REST/gRPC API server](#restgrpc-api-server)
- [REST API documentation](#rest-api-documentation)
- [License](#license)

## Requirements
Expand All @@ -44,12 +43,6 @@ Deckr provides a REST/gRPC API to simulate a deck of cards.
```shell
$ go install github.com/onsi/ginkgo/v2/ginkgo@latest
```

- [Swag](https://github.com/swaggo/swag) - Generate REST/gRPC API documentation.

```shell
$ go install github.com/swaggo/swag/cmd/swag@latest
```

- [Buf](https://github.com/bufbuild/buf) - A new way of working with Protocol Buffers.

Expand Down Expand Up @@ -89,7 +82,6 @@ Targets:
build:macOSAmd64 Builds for MacOS 64bit
build:macOSArm64 Builds for MacOS M1
build:winAmd64 Builds for Windows 64bit
docs Generates docs
lint Run golangci linters
proto Generate stubs from proto files
test Run tests
Expand All @@ -99,12 +91,6 @@ Targets:
### Example
#### Generate docs
```shell
$ mage -v docs
```

#### Generate stubs from proto files
```shell
Expand Down Expand Up @@ -187,14 +173,15 @@ USAGE:
OPTIONS:
--debug whether running in PROD or DEBUG mode (default: false) [$DECKR_DEBUG]
--server-uri value URI of server (default: "http://localhost:9000") [$DECKR_SERVER_URI]
--server-host value HOST of server (default: "localhost") [$DECKR_SERVER_HOST]
--server-port value PORT of server (default: 9000) [$DECKR_SERVER_PORT]
--storage-uri value URI of storage (default: "inmemory://") [$DECKR_STORAGE_URI]
--help, -h show help (default: false)
```

## REST/gRPC API documentation
## REST API documentation

The REST/gRPC API documentation is generated using Swag and is available at `/swagger/index.html`.
The REST API documentation is generated using Swag and is available at `/swagger/index.html`.

## License

Expand Down
13 changes: 10 additions & 3 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Documentation: https://docs.buf.build/configuration/v1/buf-gen-yaml
version: v1
plugins:
- name: go # Synonym with: protoc-gen-<name>
- name: go
out: .
opt: paths=source_relative
- name: go-grpc # Synonym with: protoc-gen-<name>
- name: go-grpc
out: .
opt: paths=source_relative
opt: paths=source_relative
- name: grpc-gateway
out: .
opt:
- paths=source_relative
- generate_unbound_methods=true
- name: openapiv2
out: .
11 changes: 11 additions & 0 deletions buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: cf209eca30404c41a80fec3caba9903f
- remote: buf.build
owner: grpc-ecosystem
repository: grpc-gateway
commit: 00116f302b12478b85deb33b734e026c
3 changes: 3 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version: v1
deps:
- buf.build/googleapis/googleapis
- buf.build/grpc-ecosystem/grpc-gateway
breaking:
use:
- FILE
Expand Down
50 changes: 37 additions & 13 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/mgjules/deckr/repo"
"github.com/mgjules/deckr/transport"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)

var serve = &cli.Command{
Expand All @@ -26,10 +27,16 @@ var serve = &cli.Command{
EnvVars: []string{"DECKR_DEBUG"},
},
&cli.StringFlag{
Name: "server-uri",
Value: "http://localhost:9000",
Usage: "URI of server",
EnvVars: []string{"DECKR_SERVER_URI"},
Name: "server-host",
Value: "localhost",
Usage: "HOST of server",
EnvVars: []string{"DECKR_SERVER_HOST"},
},
&cli.IntFlag{
Name: "server-port",
Value: 9000,
Usage: "PORT of server",
EnvVars: []string{"DECKR_SERVER_PORT"},
},
&cli.StringFlag{
Name: "storage-uri",
Expand Down Expand Up @@ -67,13 +74,20 @@ var serve = &cli.Command{
return fmt.Errorf("migrate repository: %w", err)
}

transporter, err := transport.NewTransporter(debug, c.String("server-uri"), log, info, repository)
if err != nil {
return fmt.Errorf("new transporter: %w", err)
}
host := c.String("server-host")
port := c.Int("server-port")
grpcServer := transport.NewGRPCServer(host, port, log, repository)
httpServer := transport.NewHTTPServer(debug, host, port+1, log, info)

go func() {
if err := transporter.Start(); err != nil {
log.Errorf("start transporter: %w", err)
if err = grpcServer.Start(); err != nil {
log.Errorf("start grpc server: %w", err)
}
}()

go func() {
if err = httpServer.Start(); err != nil {
log.Errorf("start http server: %w", err)
}
}()

Expand All @@ -84,10 +98,20 @@ var serve = &cli.Command{
ctx, cancel := context.WithTimeout(c.Context, 5*time.Second)
defer cancel()

if err := transporter.Stop(ctx); err != nil {
return fmt.Errorf("stop transporter: %w", err)
g, ctx := errgroup.WithContext(ctx)

g.Go(func() error {
return grpcServer.Stop(ctx)
})

g.Go(func() error {
return httpServer.Stop(ctx)
})

if err = g.Wait(); err != nil {
return fmt.Errorf("wait for servers to stop: %w", err)
}

return nil
return err
},
}
Loading

0 comments on commit 8f87289

Please sign in to comment.