Skip to content

Commit

Permalink
docs: add basic logic for openapi specs and add annotation for status…
Browse files Browse the repository at this point in the history
… endpoint
  • Loading branch information
jeamon committed May 4, 2024
1 parent c9eeb25 commit ffc1bfb
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ docker.down: ## Stop & Remove all services (app and redis) and network.
docker.clean: ## Stop & Remove all app containers (without the volume) and delete the images.
@docker rm -f $(docker ps -aq --filter "name=app.demo.redis")
@docker rmi -f $(docker images -aq --filter="reference=app.demo.redis:*")

.PHONY: swagger.generate
swagger.generate: ## Install swaggo/swag and generate openapi specs.
## use v1.8.12 due to https://github.com/swaggo/swag/issues/1568
go install github.com/swaggo/swag/cmd/[email protected]
swag init -g api.services.go
15 changes: 11 additions & 4 deletions api.handlers.book.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ func (api *APIHandler) Index(w http.ResponseWriter, r *http.Request, _ httproute
}

// Status provides basics details about the application to the public users.
// @Summary Get the app status
// @Description Get how long the application has been online.
// @ID get-status
// @Tags Books
// @Produce json
// @Success 200 {object} StatusResponse
// @Router /status [GET]
func (api *APIHandler) Status(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
requestID := GetValueFromContext(r.Context(), RequestIDContextKey)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := json.NewEncoder(w).Encode(
map[string]interface{}{
"requestid": requestID,
"status": fmt.Sprintf("up & running since %.0f mins", api.clock.Now().Sub(api.stats.started).Minutes()),
"message": "Hello. Books store api is available. Enjoy :)",
StatusResponse{
RequestID: requestID,
Status: fmt.Sprintf("up & running since %.0f mins", api.clock.Now().Sub(api.stats.started).Minutes()),
Message: "Hello. Books store api is available. Enjoy :)",
},
); err != nil {
api.logger.Error("failed to send status response", zap.String("request.id", requestID), zap.Error(err))
Expand Down
3 changes: 3 additions & 0 deletions api.router.core.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
_ "github.com/jeamon/demo-redis/docs"
"github.com/julienschmidt/httprouter"
httpswagger "github.com/swaggo/http-swagger/v2"
)

// SetupRoutes injects book and ops related endpoints if required.
Expand All @@ -12,5 +14,6 @@ func (api *APIHandler) SetupRoutes(router *httprouter.Router, m *MiddlewareMap)
if api.config.OpsEndpointsEnable {
api.SetupOpsRoutes(router, m)
}
router.GET("/swagger/*", m.public(api.OpsHandlerWrapper(httpswagger.WrapHandler)))
return router
}
15 changes: 15 additions & 0 deletions api.services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"go.uber.org/zap"
)

// @title Book Store API
// @version 1.0
// @description This provides a CRUD service on Book.

// @contact.name Jerome Amon
// @contact.url https://learn.cloudmentor-scale.com/contact

// @license.name MIT
// @license.url https://github.com/jeamon/demo-redis/blob/main/LICENSE

// @host localhost:8080
// @BasePath /v1

// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
type BookServiceProvider interface {
Add(ctx context.Context, id string, book Book) error
GetOne(ctx context.Context, id string) (Book, error)
Expand Down
84 changes: 84 additions & 0 deletions docs/docs.go

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

61 changes: 61 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"swagger": "2.0",
"info": {
"description": "This provides a CRUD service on Book.",
"title": "Book Store API",
"contact": {
"name": "Jerome Amon",
"url": "https://learn.cloudmentor-scale.com/contact"
},
"license": {
"name": "MIT",
"url": "https://github.com/jeamon/demo-redis/blob/main/LICENSE"
},
"version": "1.0"
},
"host": "localhost:8080",
"basePath": "/v1",
"paths": {
"/status": {
"get": {
"description": "Get how long the application has been online.",
"produces": [
"application/json"
],
"tags": [
"Books"
],
"summary": "Get the app status",
"operationId": "get-status",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/main.StatusResponse"
}
}
}
}
}
},
"definitions": {
"main.StatusResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"requestid": {
"type": "string"
},
"status": {
"type": "string"
}
}
}
},
"externalDocs": {
"description": "OpenAPI",
"url": "https://swagger.io/resources/open-api/"
}
}
41 changes: 41 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
basePath: /v1
definitions:
main.StatusResponse:
properties:
message:
type: string
requestid:
type: string
status:
type: string
type: object
externalDocs:
description: OpenAPI
url: https://swagger.io/resources/open-api/
host: localhost:8080
info:
contact:
name: Jerome Amon
url: https://learn.cloudmentor-scale.com/contact
description: This provides a CRUD service on Book.
license:
name: MIT
url: https://github.com/jeamon/demo-redis/blob/main/LICENSE
title: Book Store API
version: "1.0"
paths:
/status:
get:
description: Get how long the application has been online.
operationId: get-status
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/main.StatusResponse'
summary: Get the app status
tags:
- Books
swagger: "2.0"
15 changes: 14 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ require (
github.com/ory/dockertest/v3 v3.10.0
github.com/redis/go-redis/v9 v9.3.0
github.com/stretchr/testify v1.8.0
github.com/swaggo/swag v1.8.1
go.uber.org/zap v1.23.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
)

require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
Expand All @@ -24,12 +34,14 @@ require (
github.com/docker/cli v24.0.2+incompatible // indirect
github.com/docker/docker v24.0.2+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect; indirects
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand All @@ -51,6 +63,7 @@ require (
github.com/gofrs/uuid v4.3.0+incompatible
github.com/julienschmidt/httprouter v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/swaggo/http-swagger/v2 v2.0.2
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sync v0.3.0
Expand Down
Loading

0 comments on commit ffc1bfb

Please sign in to comment.