Skip to content

Commit

Permalink
Merge pull request #91 from bavix/atomic-bool
Browse files Browse the repository at this point in the history
update docs
  • Loading branch information
rez1dent3 authored Dec 14, 2023
2 parents 3c27c0f + 220536d commit 8e69559
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Rest API
- [List stubs](api-stubs-list)
- [List unused stubs](api-unused-stubs-list)
- [List used stubs](api-used-stubs-list)
- [Add stub](api-stubs-add)
- [Delete stub](api-stubs-delete)
- [Search stub](api-stubs-search)
Expand Down
77 changes: 77 additions & 0 deletions docs/api-used-stubs-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
## Rest API. Stubs Used List

Stubs Used List — endpoint returns a list of used stubs (all stubs that were found through the search).
The method inverts the logic of unused operation.

Let's imagine that our contract `simple.proto` looks something like this:
```protobuf
syntax = "proto3";
option go_package = "github.com/bavix/gripmock/protogen/example/simple";
package simple;
service Gripmock {
rpc SayHello (Request) returns (Reply);
}
message Request {
string name = 1;
}
message Reply {
string message = 1;
int32 return_code = 2;
}
```

Enough to knock on the handle `GET /api/stubs/used`:
```bash
curl http://127.0.0.1:4771/api/stubs/used
```

Response:
```json
[]
```

Find stub by ID. Enough to knock on the handle `POST /api/stubs/search`:
```bash
curl -X POST -d '{ \
"id": "6c85b0fa-caaf-4640-a672-f56b7dd8074d", \
"service": "Gripmock", \
"method": "SayHello", \
"data":{} \
}' http://127.0.0.1:4771/api/stubs/search
```

Now the stub is marked as used. Let's try to get a list of used stubs.
```bash
curl http://127.0.0.1:4771/api/stubs/used
```

Response:
```json
[
{
"id": "6c85b0fa-caaf-4640-a672-f56b7dd8074d",
"service": "Gripmock",
"method": "SayHello",
"input": {
"equals": {
"name": "gripmock"
},
"contains": null,
"matches": null
},
"output": {
"data": {
"message": "Hello GripMock",
"return_code": 42
},
"error": ""
}
}
]
```

It worked!
7 changes: 4 additions & 3 deletions internal/app/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strings"
"sync/atomic"

"github.com/bavix/gripmock/internal/domain/rest"
"github.com/bavix/gripmock/pkg/clock"
Expand All @@ -24,7 +25,7 @@ type StubsServer struct {
convertor *yaml2json.Convertor
caser cases.Caser
clock *clock.Clock
ok bool
ok atomic.Bool
}

func NewRestServer(path string) (*StubsServer, error) {
Expand Down Expand Up @@ -57,15 +58,15 @@ type findStubPayload struct {
}

func (h *StubsServer) ServiceReady() {
h.ok = true
h.ok.Store(true)
}

func (h *StubsServer) Liveness(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(rest.MessageOK{Message: "ok", Time: h.clock.Now()})
}

func (h *StubsServer) Readiness(w http.ResponseWriter, _ *http.Request) {
if !h.ok {
if !h.ok.Load() {
w.WriteHeader(http.StatusServiceUnavailable)

return
Expand Down

0 comments on commit 8e69559

Please sign in to comment.