Skip to content

Commit 2bf1985

Browse files
Merge pull request #9 from vinodnextcoder/feature/swagger
swagger docs
2 parents e54c025 + 4122439 commit 2bf1985

File tree

8 files changed

+312
-36
lines changed

8 files changed

+312
-36
lines changed

controllers/health/health.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
package health
22

33
import (
4-
"net/http"
5-
"github.com/gin-gonic/gin"
6-
"github.com/sirupsen/logrus"
7-
"golang-gin-boilerplate/services/logservice"
4+
"golang-gin-boilerplate/services/logservice"
5+
"net/http"
6+
7+
_ "golang-gin-boilerplate/docs"
8+
9+
"github.com/gin-gonic/gin"
10+
"github.com/sirupsen/logrus"
811
)
912

13+
// helloCall godoc
14+
// @Summary hellow example
15+
// @Schemes
16+
// @Description Hello
17+
// @Tags example
18+
// @Accept json
19+
// @Produce json
20+
// @Success 200 {string} Hello, You created a Web App!
21+
// @Router /health [get]
1022
func Health() gin.HandlerFunc {
1123
return func(c *gin.Context) {
12-
logservice.Info("Health endpoint called", logrus.Fields{"status":"200","functionName":"Health", "controller":"healthcontroller"})
13-
c.JSON(http.StatusOK, gin.H{
14-
"message": "Ok",
15-
})
24+
logservice.Info("Health endpoint called", logrus.Fields{"status": "200", "functionName": "Health", "controller": "healthcontroller"})
25+
c.JSON(http.StatusOK, gin.H{
26+
"message": "Ok",
27+
})
1628
}
17-
}
29+
}

docs/docs.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Package docs Code generated by swaggo/swag. DO NOT EDIT
2+
package docs
3+
4+
import "github.com/swaggo/swag"
5+
6+
const docTemplate = `{
7+
"schemes": {{ marshal .Schemes }},
8+
"swagger": "2.0",
9+
"info": {
10+
"description": "{{escape .Description}}",
11+
"title": "{{.Title}}",
12+
"contact": {
13+
"name": "vinod",
14+
"url": "http://www.swagger.io/support",
15+
"email": "support@swagger.io"
16+
},
17+
"version": "{{.Version}}"
18+
},
19+
"host": "{{.Host}}",
20+
"basePath": "{{.BasePath}}",
21+
"paths": {
22+
"/health": {
23+
"get": {
24+
"description": "Hello",
25+
"consumes": [
26+
"application/json"
27+
],
28+
"produces": [
29+
"application/json"
30+
],
31+
"tags": [
32+
"example"
33+
],
34+
"summary": "hellow example",
35+
"responses": {
36+
"200": {
37+
"description": "OK",
38+
"schema": {
39+
"type": "string"
40+
}
41+
}
42+
}
43+
}
44+
}
45+
},
46+
"externalDocs": {
47+
"description": "OpenAPI",
48+
"url": "https://swagger.io/resources/open-api/"
49+
}
50+
}`
51+
52+
// SwaggerInfo holds exported Swagger Info so clients can modify it
53+
var SwaggerInfo = &swag.Spec{
54+
Version: "1.0",
55+
Host: "localhost:3001",
56+
BasePath: "/",
57+
Schemes: []string{},
58+
Title: "Swagger Example API",
59+
Description: "This is a sample gin web server",
60+
InfoInstanceName: "swagger",
61+
SwaggerTemplate: docTemplate,
62+
LeftDelim: "{{",
63+
RightDelim: "}}",
64+
}
65+
66+
func init() {
67+
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
68+
}

docs/swagger.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"description": "This is a sample gin web server",
5+
"title": "Swagger Example API",
6+
"contact": {
7+
"name": "vinod",
8+
"url": "http://www.swagger.io/support",
9+
"email": "support@swagger.io"
10+
},
11+
"version": "1.0"
12+
},
13+
"host": "localhost:3001",
14+
"basePath": "/",
15+
"paths": {
16+
"/health": {
17+
"get": {
18+
"description": "Hello",
19+
"consumes": [
20+
"application/json"
21+
],
22+
"produces": [
23+
"application/json"
24+
],
25+
"tags": [
26+
"example"
27+
],
28+
"summary": "hellow example",
29+
"responses": {
30+
"200": {
31+
"description": "OK",
32+
"schema": {
33+
"type": "string"
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"externalDocs": {
41+
"description": "OpenAPI",
42+
"url": "https://swagger.io/resources/open-api/"
43+
}
44+
}

docs/swagger.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
basePath: /
2+
externalDocs:
3+
description: OpenAPI
4+
url: https://swagger.io/resources/open-api/
5+
host: localhost:3001
6+
info:
7+
contact:
8+
email: support@swagger.io
9+
name: vinod
10+
url: http://www.swagger.io/support
11+
description: This is a sample gin web server
12+
title: Swagger Example API
13+
version: "1.0"
14+
paths:
15+
/health:
16+
get:
17+
consumes:
18+
- application/json
19+
description: Hello
20+
produces:
21+
- application/json
22+
responses:
23+
"200":
24+
description: OK
25+
schema:
26+
type: string
27+
summary: hellow example
28+
tags:
29+
- example
30+
swagger: "2.0"

file-log02-15-2024.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
{"level":"info","msg":"golang app started","time":"2024-02-15T09:57:52+05:30"}
22
{"level":"info","msg":"golang app started","time":"2024-02-15T09:59:03+05:30"}
3+
{"level":"info","msg":"golang app started","time":"2024-02-15T18:09:11+05:30"}
4+
{"level":"info","msg":"golang app started","time":"2024-02-15T18:12:48+05:30"}
5+
{"level":"info","msg":"golang app started","time":"2024-02-15T18:14:35+05:30"}
6+
{"controller":"healthcontroller","functionName":"Health","level":"info","msg":"Health endpoint called","status":"200","time":"2024-02-15T18:14:52+05:30"}
7+
{"controller":"healthcontroller","functionName":"Health","level":"info","msg":"Health endpoint called","status":"200","time":"2024-02-15T18:14:53+05:30"}

go.mod

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,58 @@ module golang-gin-boilerplate
33
go 1.21.5
44

55
require (
6+
github.com/KyleBanks/depth v1.2.1 // indirect
7+
github.com/PuerkitoBio/purell v1.2.1 // indirect
8+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
69
github.com/bytedance/sonic v1.10.2 // indirect
710
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
811
github.com/chenzhuoyu/iasm v0.9.1 // indirect
12+
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
913
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1014
github.com/gin-contrib/sse v0.1.0 // indirect
1115
github.com/gin-gonic/gin v1.9.1 // indirect
16+
github.com/go-openapi/jsonpointer v0.20.2 // indirect
17+
github.com/go-openapi/jsonreference v0.20.4 // indirect
18+
github.com/go-openapi/spec v0.20.14 // indirect
19+
github.com/go-openapi/swag v0.22.9 // indirect
1220
github.com/go-playground/locales v0.14.1 // indirect
1321
github.com/go-playground/universal-translator v0.18.1 // indirect
14-
github.com/go-playground/validator/v10 v10.17.0 // indirect
22+
github.com/go-playground/validator/v10 v10.18.0 // indirect
1523
github.com/go-sql-driver/mysql v1.7.0 // indirect
1624
github.com/goccy/go-json v0.10.2 // indirect
1725
github.com/jinzhu/inflection v1.0.0 // indirect
1826
github.com/jinzhu/now v1.1.5 // indirect
27+
github.com/josharian/intern v1.0.0 // indirect
1928
github.com/json-iterator/go v1.1.12 // indirect
2029
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
21-
github.com/leodido/go-urn v1.3.0 // indirect
30+
github.com/leodido/go-urn v1.4.0 // indirect
2231
github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e // indirect
32+
github.com/mailru/easyjson v0.7.7 // indirect
2333
github.com/mattn/go-isatty v0.0.20 // indirect
2434
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2535
github.com/modern-go/reflect2 v1.0.2 // indirect
2636
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
37+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
38+
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
2739
github.com/sirupsen/logrus v1.9.3 // indirect
40+
github.com/swaggo/files v1.0.1 // indirect
41+
github.com/swaggo/gin-swagger v1.6.0 // indirect
42+
github.com/swaggo/swag v1.16.3 // indirect
2843
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 // indirect
2944
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
3045
github.com/ugorji/go/codec v1.2.12 // indirect
46+
github.com/urfave/cli/v2 v2.27.1 // indirect
47+
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
3148
golang.org/x/arch v0.7.0 // indirect
32-
golang.org/x/crypto v0.18.0 // indirect
33-
golang.org/x/net v0.20.0 // indirect
34-
golang.org/x/sys v0.16.0 // indirect
49+
golang.org/x/crypto v0.19.0 // indirect
50+
golang.org/x/net v0.21.0 // indirect
51+
golang.org/x/sys v0.17.0 // indirect
3552
golang.org/x/text v0.14.0 // indirect
53+
golang.org/x/tools v0.18.0 // indirect
3654
google.golang.org/protobuf v1.32.0 // indirect
55+
gopkg.in/yaml.v2 v2.4.0 // indirect
3756
gopkg.in/yaml.v3 v3.0.1 // indirect
3857
gorm.io/driver/mysql v1.5.2 // indirect
3958
gorm.io/gorm v1.25.6 // indirect
59+
sigs.k8s.io/yaml v1.4.0 // indirect
4060
)

0 commit comments

Comments
 (0)