Skip to content

Commit

Permalink
fix readFromPath
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Oct 4, 2024
1 parent 0c1a26e commit 8f1c1fc
Show file tree
Hide file tree
Showing 32 changed files with 78 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.23-alpine3.20 as protoc-builder
FROM golang:1.23-alpine3.20 AS protoc-builder

ENV PROTOC_VERSION=28.2
ARG TARGETARCH
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/bavix/features v1.0.0
github.com/bavix/gripmock-sdk-go v1.0.5
github.com/bavix/gripmock-ui v1.0.0-alpha9
github.com/bavix/gripmock/protogen v0.0.0-20240924151019-bd43d64ad313
github.com/bavix/gripmock/protogen v0.0.0-20241004180513-0c1a26e578c2
github.com/cristalhq/base64 v0.1.2
github.com/goccy/go-yaml v1.12.0
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -43,7 +43,7 @@ require (
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/bavix/gripmock-sdk-go v1.0.5 h1:DFZIz5aobjBIsMmBptHkuSdhGzOPlqp3Gn1qH
github.com/bavix/gripmock-sdk-go v1.0.5/go.mod h1:/+9vHE4ccPeV+5RecNGpy9NMwccDglH3IeXsrLWVvaY=
github.com/bavix/gripmock-ui v1.0.0-alpha9 h1:7HLtboi9XMwXSvoGmrDaaf6mBOA/wreGMkLa1DwQhwk=
github.com/bavix/gripmock-ui v1.0.0-alpha9/go.mod h1:XEH4YYEKL+wEDtONntoWm6JxjbVWzl7XtDYztUTBfeA=
github.com/bavix/gripmock/protogen v0.0.0-20240924151019-bd43d64ad313 h1:fv+WRudvdsya7Vtd5IVxhLPPgdUi3Gm7hsvfMsQrEpk=
github.com/bavix/gripmock/protogen v0.0.0-20240924151019-bd43d64ad313/go.mod h1:CZq5XycSXCrHDtmhtECqpOT2dSrw5wxzBIIIxL7GiVE=
github.com/bavix/gripmock/protogen v0.0.0-20241004180513-0c1a26e578c2 h1:w54NvfIugfqo8fe1n8IzoncH9l2AtEIdgaBcQ0QViWA=
github.com/bavix/gripmock/protogen v0.0.0-20241004180513-0c1a26e578c2/go.mod h1:lBMh0QN+Rw86t9flN7oeJKT+V+VaU6JhDI66aWgRjuE=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw=
github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c=
Expand Down Expand Up @@ -103,8 +103,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 h1:1wqE9dj9NpSm04INVsJhhEUzhuDVjbcyKH91sVyPATw=
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
Expand Down
3 changes: 2 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v8
golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc=
golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
Expand Down Expand Up @@ -352,6 +351,8 @@ golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
Expand Down
2 changes: 1 addition & 1 deletion internal/app/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (h *RestServer) SearchStubs(w http.ResponseWriter, r *http.Request) {

if result.Found() == nil {
w.WriteHeader(http.StatusNotFound)
h.writeResponseError(stubNotFoundError2(query, result), w)
h.writeResponseError(stubNotFoundError(query, result), w)

return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gripmock/stuber"
)

func stubNotFoundError2(expect stuber.Query, result *stuber.Result) error {
func stubNotFoundError(expect stuber.Query, result *stuber.Result) error {
template := fmt.Sprintf("Can't find stub \n\nService: %s \n\nMethod: %s \n\nInput\n\n", expect.Service, expect.Method)

expectString, err := json.MarshalIndent(expect.Data, "", "\t")
Expand Down
41 changes: 14 additions & 27 deletions internal/domain/rest/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 28 additions & 5 deletions internal/infra/storage/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"strings"
"time"

"github.com/gripmock/stuber"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -36,15 +37,32 @@ func (s *Extender) Wait() {
}

func (s *Extender) ReadFromPath(ctx context.Context, pathDir string) {
defer close(s.ch)
s.readFromPath(ctx, pathDir)
s.readFromPath(ctx, pathDir, false)
close(s.ch)

go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

for {
select {
case <-ticker.C:
s.readFromPath(ctx, pathDir, true)
case <-ctx.Done():
return
}
}
}()
}

// readFromPath reads all the stubs from the given directory and its subdirectories,
// and adds them to the server's stub store.
// The stub files can be in yaml or json format.
// If a file is in yaml format, it will be converted to json format.
func (s *Extender) readFromPath(ctx context.Context, pathDir string) {
//
// If `update` is true, the stubs will be updated in the server's stub store.
// Otherwise, the stubs will be added to the server's stub store.
func (s *Extender) readFromPath(ctx context.Context, pathDir string, update bool) {
files, err := os.ReadDir(pathDir)
if err != nil {
zerolog.Ctx(ctx).
Expand All @@ -57,7 +75,7 @@ func (s *Extender) readFromPath(ctx context.Context, pathDir string) {
for _, file := range files {
// If the file is a directory, recursively read its stubs.
if file.IsDir() {
s.readFromPath(ctx, path.Join(pathDir, file.Name()))
s.readFromPath(ctx, path.Join(pathDir, file.Name()), update)

continue
}
Expand All @@ -74,7 +92,12 @@ func (s *Extender) readFromPath(ctx context.Context, pathDir string) {
continue
}

s.storage.PutMany(stubs...)
// Update or add the stubs to the server's stub store.
if update {
s.storage.UpdateMany(stubs...)
} else {
s.storage.PutMany(stubs...)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-gripmock/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.2

require (
golang.org/x/text v0.19.0
golang.org/x/tools v0.25.0
golang.org/x/tools v0.26.0
google.golang.org/protobuf v1.34.2
)

Expand Down
4 changes: 2 additions & 2 deletions protoc-gen-gripmock/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
2 changes: 1 addition & 1 deletion protogen/example/ms/ms.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protogen/example/ms/ms_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protogen/example/multi-files/file1.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protogen/example/multi-files/file1_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8f1c1fc

Please sign in to comment.