diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 2dfda79c..3f474cf1 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -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) diff --git a/docs/api-used-stubs-list.md b/docs/api-used-stubs-list.md new file mode 100644 index 00000000..5afa1541 --- /dev/null +++ b/docs/api-used-stubs-list.md @@ -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! \ No newline at end of file diff --git a/internal/app/rest_server.go b/internal/app/rest_server.go index d22eb9b9..29c1baa5 100644 --- a/internal/app/rest_server.go +++ b/internal/app/rest_server.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "strings" + "sync/atomic" "github.com/bavix/gripmock/internal/domain/rest" "github.com/bavix/gripmock/pkg/clock" @@ -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) { @@ -57,7 +58,7 @@ type findStubPayload struct { } func (h *StubsServer) ServiceReady() { - h.ok = true + h.ok.Store(true) } func (h *StubsServer) Liveness(w http.ResponseWriter, _ *http.Request) { @@ -65,7 +66,7 @@ func (h *StubsServer) Liveness(w http.ResponseWriter, _ *http.Request) { } func (h *StubsServer) Readiness(w http.ResponseWriter, _ *http.Request) { - if !h.ok { + if !h.ok.Load() { w.WriteHeader(http.StatusServiceUnavailable) return