Skip to content

Commit

Permalink
🐛 chore: wagger
Browse files Browse the repository at this point in the history
  • Loading branch information
perebaj committed Sep 27, 2023
1 parent b539de5 commit c40a5c8
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The testcase variable could be used to run a specific test
`heroku logs --tail -a contractus`

## API documentation

[API Docs](api/docs/)

## Attention points
- For a while, the integration-tests just ran locally not in CI, this increased the time to ship code 🚀
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions docs/api.yml → api/docs/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ info:
version: 0.0.0
servers:
- url: http://localhost:8080
description: Local server
- url: https://contractus-25fea2a1cfb3.herokuapp.com
description: Production server

paths:
/upload:
post:
Expand Down
14 changes: 14 additions & 0 deletions api/transactionhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"embed"
"fmt"
"net/http"
"strconv"
Expand All @@ -11,6 +12,7 @@ import (
"log/slog"

"github.com/go-chi/chi/v5"
"github.com/go-openapi/runtime/middleware"
"github.com/perebaj/contractus"
"github.com/perebaj/contractus/postgres"
)
Expand All @@ -25,6 +27,9 @@ type transactionHandler struct {
storage transactionStorage
}

//go:embed docs/api.yml
var swagger embed.FS

// RegisterHandler gather all the handlers for the API.
func RegisterHandler(r chi.Router, storage transactionStorage) {
h := transactionHandler{
Expand All @@ -42,6 +47,15 @@ func RegisterHandler(r chi.Router, storage transactionStorage) {

const transactions = "/transactions"
r.Method(http.MethodGet, transactions, http.HandlerFunc(h.transactions))

// Register swagger docs.
opts := middleware.SwaggerUIOpts{SpecURL: "docs/api.yml",
Path: "/",
Title: "Contractus API",
}
sh := middleware.SwaggerUI(opts, nil)
r.Handle("/", sh)
r.Handle("/docs/api.yml", http.FileServer(http.FS(swagger)))
}

func (s transactionHandler) producerBalance(w http.ResponseWriter, r *http.Request) {
Expand Down
23 changes: 23 additions & 0 deletions api/transactionhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,26 @@ func TestTransactionHandlerTransactions(t *testing.T) {
t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.Code)
}
}

func TestSwagger(t *testing.T) {
m := &mockTransactionStorage{}
r := chi.NewRouter()
RegisterHandler(r, m)

req := httptest.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()

r.ServeHTTP(resp, req)

if resp.Code != http.StatusOK {
t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.Code)
}

req = httptest.NewRequest(http.MethodGet, "/docs/api.yml", nil)
resp = httptest.NewRecorder()

r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.Code)
}
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/alexkohler/nakedret/v2 v2.0.2 // indirect
github.com/alexkohler/prealloc v1.0.0 // indirect
github.com/alingse/asasalint v0.0.11 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/ashanbrown/forbidigo v1.6.0 // indirect
github.com/ashanbrown/makezero v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand All @@ -52,6 +53,16 @@ require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/go-critic/go-critic v0.9.0 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/runtime v0.26.0 // indirect
github.com/go-openapi/spec v0.20.8 // indirect
github.com/go-openapi/strfmt v0.21.7 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/validate v0.22.1 // indirect
github.com/go-toolsmith/astcast v1.1.0 // indirect
github.com/go-toolsmith/astcopy v1.1.0 // indirect
github.com/go-toolsmith/astequal v1.1.0 // indirect
Expand Down Expand Up @@ -87,6 +98,7 @@ require (
github.com/jgautheron/goconst v1.5.1 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/julz/importas v0.1.0 // indirect
github.com/kisielk/errcheck v1.6.3 // indirect
github.com/kisielk/gotool v1.0.0 // indirect
Expand All @@ -100,6 +112,7 @@ require (
github.com/lib/pq v1.10.9 // indirect
github.com/lufeee/execinquery v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/maratori/testableexamples v1.0.0 // indirect
github.com/maratori/testpackage v1.1.1 // indirect
github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 // indirect
Expand All @@ -116,6 +129,7 @@ require (
github.com/nishanths/exhaustive v0.11.0 // indirect
github.com/nishanths/predeclared v0.2.2 // indirect
github.com/nunnatsa/ginkgolinter v0.13.5 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
Expand Down Expand Up @@ -168,6 +182,7 @@ require (
github.com/yeya24/promlinter v0.2.0 // indirect
github.com/ykadowak/zerologlint v0.1.3 // indirect
gitlab.com/bosi/decorder v0.4.0 // indirect
go.mongodb.org/mongo-driver v1.11.3 // indirect
go.tmz.dev/musttag v0.7.2 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
Expand Down
Loading

0 comments on commit c40a5c8

Please sign in to comment.