Skip to content

Commit

Permalink
init swaggo/swag ; helps to make swagger docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bhattaraibishal50 committed Aug 29, 2020
1 parent 0ef8278 commit 71d8521
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 12 deletions.
69 changes: 67 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,78 @@ var doc = `{
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"contact": {},
"contact": {
"name": "readytowork",
"url": "http://readytowork.jp",
"email": "[email protected]"
},
"license": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {}
"paths": {
"/posts/create": {
"post": {
"description": "Add Posts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Add Posts",
"parameters": [
{
"type": "string",
"description": "name search by q",
"name": "q",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Post"
}
},
"headers": {
"Token": {
"type": "string",
"description": "qwerty"
}
}
}
}
}
}
},
"definitions": {
"model.Post": {
"type": "object",
"required": [
"Description",
"Title"
],
"properties": {
"Description": {
"type": "string"
},
"Title": {
"type": "string"
},
"createdAt": {
"type": "string"
},
"id": {
"type": "string"
}
}
}
}
}`

type swaggerInfo struct {
Expand Down
69 changes: 67 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@
{
"swagger": "2.0",
"info": {
"contact": {},
"contact": {
"name": "readytowork",
"url": "http://readytowork.jp",
"email": "[email protected]"
},
"license": {}
},
"paths": {}
"paths": {
"/posts/create": {
"post": {
"description": "Add Posts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Add Posts",
"parameters": [
{
"type": "string",
"description": "name search by q",
"name": "q",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Post"
}
},
"headers": {
"Token": {
"type": "string",
"description": "qwerty"
}
}
}
}
}
}
},
"definitions": {
"model.Post": {
"type": "object",
"required": [
"Description",
"Title"
],
"properties": {
"Description": {
"type": "string"
},
"Title": {
"type": "string"
},
"createdAt": {
"type": "string"
},
"id": {
"type": "string"
}
}
}
}
}
46 changes: 44 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
definitions:
model.Post:
properties:
Description:
type: string
Title:
type: string
createdAt:
type: string
id:
type: string
required:
- Description
- Title
type: object
info:
contact: {}
contact:
email: [email protected]
name: readytowork
url: http://readytowork.jp
license: {}
paths: {}
paths:
/posts/create:
post:
consumes:
- application/json
description: Add Posts
parameters:
- description: name search by q
in: query
name: q
type: string
produces:
- application/json
responses:
"200":
description: OK
headers:
Token:
description: qwerty
type: string
schema:
items:
$ref: '#/definitions/model.Post'
type: array
summary: Add Posts
swagger: "2.0"
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
cloud.google.com/go/firestore v1.3.0
firebase.google.com/go v3.13.0+incompatible
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/go-openapi/spec v0.19.9 // indirect
Expand All @@ -24,7 +25,8 @@ require (
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 // indirect
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.7 // indirect
github.com/swaggo/http-swagger v0.0.0-20200308142732-58ac5e232fba // indirect
github.com/swaggo/swag v1.6.7
github.com/thinkerou/favicon v0.1.0
github.com/urfave/cli/v2 v2.2.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tN
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -81,6 +82,7 @@ github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -170,6 +172,7 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
Expand Down Expand Up @@ -243,7 +246,10 @@ github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 h1:PyYN9JH5jY9j6av01S
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14/go.mod h1:gxQT6pBGRuIGunNf/+tSOB5OHvguWi8Tbt82WOkf35E=
github.com/swaggo/gin-swagger v1.2.0 h1:YskZXEiv51fjOMTsXrOetAjrMDfFaXD79PEoQBOe2W0=
github.com/swaggo/gin-swagger v1.2.0/go.mod h1:qlH2+W7zXGZkczuL+r2nEBR2JTT+/lX05Nn6vPhc7OI=
github.com/swaggo/http-swagger v0.0.0-20200308142732-58ac5e232fba h1:lUPlXKqgbqT2SVg2Y+eT9mu5wbqMnG+i/+Q9nK7C0Rs=
github.com/swaggo/http-swagger v0.0.0-20200308142732-58ac5e232fba/go.mod h1:O1lAbCgAAX/KZ80LM/OXwtWFI/5TvZlwxSg8Cq08PV0=
github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y=
github.com/swaggo/swag v1.6.3/go.mod h1:wcc83tB4Mb2aNiL/HP4MFeQdpHUrca+Rp/DRNgWAUio=
github.com/swaggo/swag v1.6.7 h1:e8GC2xDllJZr3omJkm9YfmK0Y56+rMO3cg0JBKNz09s=
github.com/swaggo/swag v1.6.7/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc=
github.com/thinkerou/favicon v0.1.0 h1:eWMISKTpHq2G8HOuKn7ydD55j5DDehx94b0C2y8ABMs=
Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@ package main
import (
"fmt"
"github/bhattaraibishal50/blog/common"
"github/bhattaraibishal50/blog/docs"
"github/bhattaraibishal50/blog/middleware"

"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/thinkerou/favicon"

ginSwagger "github.com/swaggo/gin-swagger"

// gin-swagger middleware
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)

// swagger embed files
// @contact.name readytowork
// @contact.url http://readytowork.jp
// @contact.email [email protected]

func main() {
//setting up the application
// programmatically set swagger info
docs.SwaggerInfo.Title = "Blog Application"
docs.SwaggerInfo.Description = "This is a sample server for Blog Application"
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Host = "localhost:8080"
docs.SwaggerInfo.BasePath = "/"
docs.SwaggerInfo.Schemes = []string{"http", "https"}

fmt.Println("UUID ::", common.GetNewUUID())
r := gin.Default()
r.Use(favicon.New("./public/favicon.ico")) // set favicon middleware

// set routes
middleware.SetRoutes(r)
PORT := ":8080"

r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run(PORT) //listen and serve on 0.0.0.0:8080 by default
}
10 changes: 9 additions & 1 deletion post/usecase/postUsecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ func GetPosts(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": &posts})
}

// AddPosts add the post
// AddPosts godoc
// @Summary Add Posts
// @Description Add Posts
// @Accept json
// @Produce json
// @Param q query string false "name search by q"
// @Success 200 {array} model.Post
// @Header 200 {string} Token "qwerty"
// @Router /posts/create [post]
func AddPosts(c *gin.Context) {
// for json type data we need to bind with the struct references https://mholt.github.io/json-to-go/
//JSON BIND
Expand Down
Binary file modified tmp/runner-build
Binary file not shown.

0 comments on commit 71d8521

Please sign in to comment.