Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Small api refactor (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Pastro authored Aug 12, 2023
1 parent 458bc6c commit 576a058
Show file tree
Hide file tree
Showing 13 changed files with 850 additions and 531 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
# A simple TODO app

A simple TODO app that uses Buf Connect, JWTs for authentication, OTEL for
tracing, and sqlc and Postgres for storage.
A simple TODO app that uses [Buf Connect](https://connectrpc.com/), JWTs for
authentication, [OTEL](https://opentelemetry.io/) for tracing, and
[sqlc](https://sqlc.dev/) and Postgres for storage.

## Try it out

`docker compose up -d` will run Postgres, Jaeger and the TODO app.

## Usage
## Prerequisite

Create a JWT with the `sub` claim set to a user id, or just use
Create a JWT with the `sub` claim set to a user id and set the `JWT_SECRET` env
var, or just use

```
export TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtcl9yb2JvdG8ifQ.oUD_0r5Q1H_akjeJFWYAxbcr2fckBEb7M25wVJw432Y"
```

with the default secret.

## Usage

Create a post:

```
$ curl -XPOST http://localhost:8080/todoapp.v1.TodoAppService/Create \
-H "Authentication: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{"todo": "buy some carrots"}'
{"post":{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","todo":"buy some carrots","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:20:56.235695Z"}}
{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","todo":"buy some carrots","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:20:56.235695Z"}
```

Read a post:
Expand All @@ -34,7 +38,7 @@ $ curl -XPOST http://localhost:8080/todoapp.v1.TodoAppService/Read \
-H "Authentication: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{"todoId": "6086008b-4706-4245-8f4e-58ed3eba43d7"}'
{"post":{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","todo":"buy some carrots","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:20:56.235695Z"}}
{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","todo":"buy some carrots","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:20:56.235695Z"}
```

Read all posts:
Expand All @@ -44,7 +48,7 @@ $ curl -XPOST http://localhost:8080/todoapp.v1.TodoAppService/ReadAll \
-H "Authentication: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{}'
{"posts":[{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","todo":"buy some carrots","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:20:56.235695Z"}],"lastIndex":"1"}
{"todos":[{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","todo":"buy some carrots","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:20:56.235695Z"}],"lastIndex":"1"}
```

Update a post:
Expand All @@ -54,7 +58,7 @@ $ curl -XPOST http://localhost:8080/todoapp.v1.TodoAppService/Update \
-H "Authentication: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{"todoId": "6086008b-4706-4245-8f4e-58ed3eba43d7", "todo": "buy onions"}'
{"post":{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","data":"buy onions","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:22:18.689477Z"}}
{"userId":"mr_roboto","todoId":"6086008b-4706-4245-8f4e-58ed3eba43d7","data":"buy onions","createdAt":"2023-06-15T18:20:56.235695Z","updatedAt":"2023-06-15T18:22:18.689477Z"}
```

Delete a post:
Expand Down
14 changes: 7 additions & 7 deletions cmd/todoapp/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestAPI(t *testing.T) {
res, err := client.Create(context.Background(), req)
require.NoError(t, err)

todo := res.Msg.GetTodo()
todo := res.Msg

require.NotEmpty(t, todo.GetTodoId())
require.NotEmpty(t, todo.GetCreatedAt())
Expand All @@ -109,13 +109,13 @@ func TestAPI(t *testing.T) {
createRes, err := client.Create(context.Background(), createReq)
require.NoError(t, err)

readReq := createRequest(&pb.ReadRequest{TodoId: createRes.Msg.Todo.GetTodoId()})
readReq := createRequest(&pb.ReadRequest{TodoId: createRes.Msg.GetTodoId()})
readRes, err := client.Read(context.Background(), readReq)
require.NoError(t, err)

post := readRes.Msg.GetTodo()
todo := readRes.Msg.GetTodo()

require.Equal(t, post.GetTodo(), aTodo)
require.Equal(t, todo, aTodo)
})

t.Run("read not exist", func(t *testing.T) {
Expand All @@ -131,7 +131,7 @@ func TestAPI(t *testing.T) {
createRes, err := client.Create(ctx, createReq)
require.NoError(t, err)

createdTodo := createRes.Msg.GetTodo()
createdTodo := createRes.Msg
newTodo := "call parents"

upsertReq := createRequest(&pb.UpdateRequest{
Expand All @@ -149,7 +149,7 @@ func TestAPI(t *testing.T) {

todo := readRes.Msg.GetTodo()

require.Equal(t, todo.GetTodo(), newTodo)
require.Equal(t, todo, newTodo)
})

t.Run("delete", func(t *testing.T) {
Expand All @@ -159,7 +159,7 @@ func TestAPI(t *testing.T) {
createRes, err := client.Create(ctx, createReq)
require.NoError(t, err)

createdTodo := createRes.Msg.GetTodo()
createdTodo := createRes.Msg

deleteReq := createRequest(&pb.DeleteRequest{TodoId: createdTodo.GetTodoId()})
_, err = client.Delete(ctx, deleteReq)
Expand Down
1 change: 1 addition & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
ports:
- 8080:8080
environment:
- JWT_SECRET=PMBrjiOH5RMo6nQHidA62XctWGxDG0rw
- POSTGRES_CONN_STRING=postgres://authenticator:password@postgres:5432/postgres
- POSTGRES_AUTOMIGRATE=true
- POSTGRES_MIGRATE_CONN_STRING=postgres://postgres:password@postgres:5432/postgres
Expand Down
51 changes: 24 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,73 @@ module github.com/craigpastro/todoapp
go 1.20

require (
github.com/bufbuild/connect-go v1.9.0
github.com/bufbuild/connect-go v1.10.0
github.com/bufbuild/connect-grpcreflect-go v1.1.0
github.com/bufbuild/connect-opentelemetry-go v0.4.0
github.com/craigpastro/retrier v0.1.2
github.com/envoyproxy/protoc-gen-validate v1.0.1
github.com/envoyproxy/protoc-gen-validate v1.0.2
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/jackc/pgx/v5 v5.4.2
github.com/pressly/goose/v3 v3.11.2
github.com/jackc/pgx/v5 v5.4.3
github.com/pressly/goose/v3 v3.14.0
github.com/sethvargo/go-envconfig v0.9.0
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.21.0
github.com/testcontainers/testcontainers-go v0.22.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/net v0.11.0
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
golang.org/x/net v0.14.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.2 // indirect
github.com/containerd/containerd v1.7.3 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.2+incompatible // indirect
github.com/docker/docker v24.0.5+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.20.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.10.0 // indirect
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.4.0 // indirect
)
Loading

0 comments on commit 576a058

Please sign in to comment.