Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creates Initial OAPI Spec for Pacta Versions #3

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:resolve go github.com/RMI/pacta/openapi/pacta //openapi:pacta_generated
Expand All @@ -16,10 +15,3 @@ gazelle(
],
command = "update-repos",
)

go_library(
name = "pacta",
srcs = ["pacta.go"],
importpath = "github.com/RMI/pacta",
visibility = ["//visibility:public"],
)
12 changes: 6 additions & 6 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/RMI/pacta/cmd/server/pactasrv"
"github.com/RMI/pacta/db/sqldb"
"github.com/RMI/pacta/keyutil"
"github.com/RMI/pacta/openapi/pacta"
oapipacta "github.com/RMI/pacta/openapi/pacta"
gbdubs marked this conversation as resolved.
Show resolved Hide resolved
"github.com/Silicon-Ally/zaphttplog"
"github.com/go-chi/chi/v5"
"github.com/go-chi/httprate"
Expand Down Expand Up @@ -107,7 +107,7 @@ func run(args []string) error {
return fmt.Errorf("failed to init sqldb: %w", err)
}

pactaSwagger, err := pacta.GetSwagger()
pactaSwagger, err := oapipacta.GetSwagger()
if err != nil {
return fmt.Errorf("failed to load PACTA swagger spec: %w", err)
}
Expand All @@ -116,20 +116,20 @@ func run(args []string) error {
// that server names match. We don't know how this thing will be run.
pactaSwagger.Servers = nil

// Create an instance of our handler which satisfies the generated interface
pactaSrv := &pactasrv.Server{
// Create an instance of our handler which satisfies each generated interface
srv := &pactasrv.Server{
DB: db,
}

pactaStrictHandler := pacta.NewStrictHandlerWithOptions(pactaSrv, nil /* middleware */, pacta.StrictHTTPServerOptions{
pactaStrictHandler := oapipacta.NewStrictHandlerWithOptions(srv, nil /* middleware */, oapipacta.StrictHTTPServerOptions{
RequestErrorHandlerFunc: requestErrorHandlerFuncForService(logger, "pacta"),
ResponseErrorHandlerFunc: responseErrorHandlerFuncForService(logger, "pacta"),
})

r := chi.NewRouter()

// We now register our PACTA above as the handler for the interface
pacta.HandlerWithOptions(pactaStrictHandler, pacta.ChiServerOptions{
oapipacta.HandlerWithOptions(pactaStrictHandler, oapipacta.ChiServerOptions{
BaseRouter: r.With(
// The order of these is important. We run RequestID and RealIP first to
// populate relevant metadata for logging, and we run recovery immediately after
Expand Down
20 changes: 6 additions & 14 deletions cmd/server/pactasrv/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "pactasrv",
srcs = ["pactasrv.go"],
srcs = [
"pacta_version.go",
"pactasrv.go",
"user.go",
],
importpath = "github.com/RMI/pacta/cmd/server/pactasrv",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -11,15 +15,3 @@ go_library(
"//pacta",
],
)

go_test(
name = "pactasrv_test",
srcs = ["pactasrv_test.go"],
embed = [":pactasrv"],
deps = [
"//openapi:pacta_generated",
"@com_github_go_chi_jwtauth_v5//:jwtauth",
"@com_github_google_go_cmp//cmp",
"@com_github_lestrrat_go_jwx_v2//jwt",
],
)
38 changes: 38 additions & 0 deletions cmd/server/pactasrv/pacta_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pactasrv

import (
"context"
"fmt"

api "github.com/RMI/pacta/openapi/pacta"
)

// Returns a version of the PACTA model by ID
// (GET /pacta-version/{id})
func (s *Server) FindPactaVersionById(ctx context.Context, request api.FindPactaVersionByIdRequestObject) (api.FindPactaVersionByIdResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}

// Returns all versions of the PACTA model
// (GET /pacta-versions)
func (s *Server) ListPactaVersions(ctx context.Context, request api.ListPactaVersionsRequestObject) (api.ListPactaVersionsResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}

// Creates a PACTA version
// (POST /pacta-versions)
func (s *Server) CreatePactaVersion(ctx context.Context, request api.CreatePactaVersionRequestObject) (api.CreatePactaVersionResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}

// Updates a PACTA version
// (PATCH /pacta-version/{id})
func (s *Server) UpdatePactaVersion(ctx context.Context, request api.UpdatePactaVersionRequestObject) (api.UpdatePactaVersionResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}

// Deletes a pacta version by ID
// (DELETE /pacta-version/{id})
func (s *Server) DeletePactaVersion(ctx context.Context, request api.DeletePactaVersionRequestObject) (api.DeletePactaVersionResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}
95 changes: 1 addition & 94 deletions cmd/server/pactasrv/pactasrv.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// Package pactasrv currently just implements the Petstore API interface, the
// bog standard OpenAPI example. It implements pactaapi.StrictServerInterface,
// which is auto-generated from the OpenAPI 3 spec.
package pactasrv

import (
"context"
"fmt"
"net/http"

"github.com/RMI/pacta/db"

pactaapi "github.com/RMI/pacta/openapi/pacta"
"github.com/RMI/pacta/pacta"
)

Expand Down Expand Up @@ -66,92 +60,5 @@ type DB interface {
}

type Server struct {
DB DB
pets []pactaapi.Pet
idx int
}

// Returns all pets
// (GET /pets)
func (s *Server) FindPets(ctx context.Context, req pactaapi.FindPetsRequestObject) (pactaapi.FindPetsResponseObject, error) {
out := []pactaapi.Pet{}
for _, p := range s.pets {
if matchesTag(req.Params.Tags, p.Tag) {
out = append(out, p)
}
}
if req.Params.Limit != nil && *req.Params.Limit > 0 && int(*req.Params.Limit) < len(out) {
out = out[:*req.Params.Limit]
}

return pactaapi.FindPets200JSONResponse(out), nil
}

func matchesTag(tags *[]string, tag *string) bool {
if tags == nil || tag == nil || len(*tags) == 0 {
return true
}
for _, t := range *tags {
if t == *tag {
return true
}
}
return false
}

// Creates a new pet
// (POST /pets)
func (s *Server) AddPet(ctx context.Context, req pactaapi.AddPetRequestObject) (pactaapi.AddPetResponseObject, error) {
s.idx += 1
id := int64(s.idx)
s.pets = append(s.pets, pactaapi.Pet{
Id: id,
Name: req.Body.Name,
Tag: req.Body.Tag,
})
return pactaapi.AddPet200JSONResponse{
Id: id,
Name: req.Body.Name,
Tag: req.Body.Tag,
}, nil
}

// Deletes a pet by ID
// (DELETE /pets/{id})
func (s *Server) DeletePet(ctx context.Context, req pactaapi.DeletePetRequestObject) (pactaapi.DeletePetResponseObject, error) {
for i, p := range s.pets {
if p.Id == req.Id {
s.pets = append(s.pets[:i], s.pets[i+1:]...)
return pactaapi.DeletePet204Response{}, nil
}
}
return pactaapi.DeletePetdefaultJSONResponse{
Body: pactaapi.Error{
Code: 1,
Message: fmt.Sprintf("no pet with id %d", req.Id),
},
StatusCode: http.StatusNotFound,
}, nil
}

// Returns a pet by ID
// (GET /pets/{id})
func (s *Server) FindPetByID(ctx context.Context, req pactaapi.FindPetByIDRequestObject) (pactaapi.FindPetByIDResponseObject, error) {
for _, p := range s.pets {
if p.Id == req.Id {
return pactaapi.FindPetByID200JSONResponse{
Id: p.Id,
Name: p.Name,
Tag: p.Tag,
}, nil
}
}

return pactaapi.FindPetByIDdefaultJSONResponse{
Body: pactaapi.Error{
Code: 2,
Message: fmt.Sprintf("no pet with id %d", req.Id),
},
StatusCode: http.StatusNotFound,
}, nil
DB DB
}
82 changes: 0 additions & 82 deletions cmd/server/pactasrv/pactasrv_test.go

This file was deleted.

26 changes: 26 additions & 0 deletions cmd/server/pactasrv/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pactasrv

import (
"context"
"fmt"

api "github.com/RMI/pacta/openapi/pacta"
)

// Returns a user by ID
// (GET /user/{id})
func (s *Server) FindUserById(ctx context.Context, request api.FindUserByIdRequestObject) (api.FindUserByIdResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}

// Updates user properties
// (PATCH /user/{id})
func (s *Server) UpdateUser(ctx context.Context, request api.UpdateUserRequestObject) (api.UpdateUserResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}

// Deletes a user by ID
// (DELETE /user/{id})
func (s *Server) DeleteUser(ctx context.Context, request api.DeleteUserRequestObject) (api.DeleteUserResponseObject, error) {
return nil, fmt.Errorf("not implemented")
}
Loading