Skip to content

Commit

Permalink
Merge pull request #3 from josesalasdev/feature/swagger
Browse files Browse the repository at this point in the history
Swagger configuration
  • Loading branch information
josesalasdev authored May 20, 2021
2 parents cd25700 + 4e06ec1 commit ceed0ef
Show file tree
Hide file tree
Showing 10 changed files with 517 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ This is an api template in go with docker.
```bash
docker-compose up
```
or

```bash
go run src/main.go
```

## Docs

* http://localhost:8080/swagger/index.html
150 changes: 150 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag

package docs

import (
"bytes"
"encoding/json"
"strings"

"github.com/alecthomas/template"
"github.com/swaggo/swag"
)

var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "[email protected]"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/books": {
"get": {
"description": "Get all books.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Find Books.",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Book"
}
}
}
}
}
},
"/ping": {
"get": {
"description": "Health Api.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Health Api.",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/controllers.pingMessage"
}
}
}
}
}
},
"definitions": {
"controllers.pingMessage": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"models.Book": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"price": {
"type": "number"
},
"title": {
"type": "string"
}
}
}
}
}`

type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "",
Description: "",
}

type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}

return tpl.String()
}

func init() {
swag.Register(swag.Name, &s{})
}
83 changes: 83 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"swagger": "2.0",
"info": {
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "[email protected]"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"paths": {
"/books": {
"get": {
"description": "Get all books.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Find Books.",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Book"
}
}
}
}
}
},
"/ping": {
"get": {
"description": "Health Api.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Health Api.",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/controllers.pingMessage"
}
}
}
}
}
},
"definitions": {
"controllers.pingMessage": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"models.Book": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"price": {
"type": "number"
},
"title": {
"type": "string"
}
}
}
}
}
53 changes: 53 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
definitions:
controllers.pingMessage:
properties:
message:
type: string
type: object
models.Book:
properties:
id:
type: integer
price:
type: number
title:
type: string
type: object
info:
contact:
email: [email protected]
name: API Support
url: http://www.swagger.io/support
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
paths:
/books:
get:
consumes:
- application/json
description: Get all books.
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/models.Book'
type: array
summary: Find Books.
/ping:
get:
consumes:
- application/json
description: Health Api.
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/controllers.pingMessage'
summary: Health Api.
swagger: "2.0"
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ module github.com/josesalasdev/golang_api_template
go 1.16

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/gin-gonic/gin v1.7.1 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/validator/v10 v10.6.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 // indirect
github.com/swaggo/gin-swagger v1.3.0 // indirect
github.com/swaggo/swag v1.7.0 // indirect
github.com/ugorji/go v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
golang.org/x/tools v0.1.1 // indirect
gorm.io/driver/sqlite v1.1.4 // indirect
gorm.io/gorm v1.21.10 // indirect
)
Loading

0 comments on commit ceed0ef

Please sign in to comment.