Skip to content

Commit

Permalink
Storage redone
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 7, 2023
1 parent adb4d6e commit fbfa812
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 63 deletions.
17 changes: 17 additions & 0 deletions api/openapi/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ paths:
responses:
'204':
description: Successful operation
/stubs/batchDelete:
post:
tags:
- stubs
summary: Deletes a pack by IDs
description: Takes IDs as input and deletes them
operationId: batchStubsDelete
responses:
'204':
description: Successful operation
requestBody:
description: Create a new pet in the store
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ListID'
'/stubs/{uuid}':
delete:
tags:
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/google/uuid v1.3.1
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-memdb v1.3.4
github.com/lithammer/fuzzysearch v1.1.8
github.com/oapi-codegen/runtime v1.0.0
github.com/stretchr/testify v1.8.4
Expand All @@ -23,6 +24,8 @@ require (
github.com/fatih/color v1.15.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrhxx+FVELeXpVPE=
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c=
github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg=
github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
36 changes: 31 additions & 5 deletions internal/app/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bavix/gripmock/pkg/clock"
"github.com/bavix/gripmock/pkg/storage"
"github.com/bavix/gripmock/pkg/yaml2json"
"github.com/google/uuid"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand All @@ -25,9 +26,14 @@ type StubsServer struct {
clock *clock.Clock
}

func NewRestServer(path string) *StubsServer {
func NewRestServer(path string) (*StubsServer, error) {
stubsStorage, err := storage.New()
if err != nil {
return nil, err
}

server := &StubsServer{
stubs: storage.New(),
stubs: stubsStorage,
convertor: yaml2json.New(),
clock: clock.New(),
caser: cases.Title(language.English, cases.NoLower),
Expand All @@ -37,7 +43,7 @@ func NewRestServer(path string) *StubsServer {
server.readStubs(path) // TODO: someday you will need to rewrite this code
}

return server
return server, nil
}

// deprecated code
Expand Down Expand Up @@ -97,9 +103,29 @@ func (h *StubsServer) AddStub(w http.ResponseWriter, r *http.Request) {
}
}

func (h *StubsServer) DeleteStubByID(w http.ResponseWriter, _ *http.Request, _ rest.ID) {
func (h *StubsServer) DeleteStubByID(w http.ResponseWriter, _ *http.Request, uuid rest.ID) {
w.Header().Set("Content-Type", "application/json")
h.stubs.Delete(uuid)
}

func (h *StubsServer) BatchStubsDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
panic("DeleteStubByID")

var inputs []uuid.UUID
decoder := json.NewDecoder(r.Body)
decoder.UseNumber()

defer r.Body.Close()

if err := decoder.Decode(&inputs); err != nil {
h.responseError(err, w)

return
}

if len(inputs) > 0 {
h.stubs.Delete(inputs...)
}
}

func (h *StubsServer) ListUnusedStubs(w http.ResponseWriter, r *http.Request) {
Expand Down
23 changes: 23 additions & 0 deletions internal/domain/rest/api.gen.go

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

131 changes: 131 additions & 0 deletions pkg/sdk/api.gen.go

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

Loading

0 comments on commit fbfa812

Please sign in to comment.