From 9e9f630582505a505ac8a0267316407550b85fb4 Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 10:12:44 +0530 Subject: [PATCH 01/14] Update README.md --- README.md | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 39266e3..837305a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,34 @@

Welcome to Gin boilerplate 👋

-

API Gin boilerplate in progress

+

API Gin boilerplate

+ +# ⚡️ Fullstack Typescript Nest JS and Next JS ⚡️ + +## Features + +1. user Register +2. logrus logger + + +## Backend +- Golang +- Gin-gonic framework +- Mysql +- Logrus logger + +``` +git clone https://github.com/vinodnextcoder/golang-gin-boilerplate.git +cd golang-gin-boilerplate +create file .env copy from .env + +Install project dependencies +go mod download +Run run project + +go run main +``` + +# Api postman collection in root folder gin.postman_collection

@@ -12,4 +40,4 @@ -->

-It's an Golang API boilerplate project based on Gin framework. \ No newline at end of file +It's an Golang API boilerplate project based on Gin framework. From 32ef73c13321b7c660198a0b8b53d6012a6943a5 Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 10:13:08 +0530 Subject: [PATCH 02/14] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 837305a..1321e7d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@

Welcome to Gin boilerplate 👋

-

API Gin boilerplate

-# ⚡️ Fullstack Typescript Nest JS and Next JS ⚡️ +# ⚡️ API Gin boilerplate ⚡️ ## Features From 4cb613cc94bebc8ada8e1b16776f3005d232c9ce Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 10:13:28 +0530 Subject: [PATCH 03/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1321e7d..1196d32 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Install project dependencies go mod download Run run project -go run main +go run main.go ``` # Api postman collection in root folder gin.postman_collection From f165481c495cc0b6de5ba79119b35f709bee6a48 Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:16:27 +0530 Subject: [PATCH 04/14] Update README.md --- README.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1196d32..10f799c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,15 @@

Welcome to Gin boilerplate 👋

+

+ + + + + +

+ +It's an Golang API boilerplate project based on Gin framework. # ⚡️ API Gin boilerplate ⚡️ @@ -29,14 +40,4 @@ go run main.go # Api postman collection in root folder gin.postman_collection -

- - - - - -

-It's an Golang API boilerplate project based on Gin framework. From a4ed048848fd904b9c5f164e0028288aa90b8e08 Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:17:08 +0530 Subject: [PATCH 05/14] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 10f799c..40b7595 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,6 @@ -->

-It's an Golang API boilerplate project based on Gin framework. - # ⚡️ API Gin boilerplate ⚡️ From c48f6822df160150f7c5f93902a12fd63d3070d3 Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:17:56 +0530 Subject: [PATCH 06/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40b7595..f21d85e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -

Welcome to Gin boilerplate 👋

+

From ed0b2d65e89253bb5ea5aef2a49a104736d2f489 Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:18:34 +0530 Subject: [PATCH 07/14] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f21d85e..47c2c2f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ + +# ⚡️ API Gin boilerplate ⚡️

@@ -9,9 +11,6 @@ -->

- -# ⚡️ API Gin boilerplate ⚡️ - ## Features 1. user Register From e6515e816bd60cebf67e5a0957b62337a4598236 Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:21:06 +0530 Subject: [PATCH 08/14] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 47c2c2f..061bb19 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Run run project go run main.go ``` -# Api postman collection in root folder gin.postman_collection +**Api postman collection in root folder gin.postman_collection import in postman** + From e54c025f357233ac9f0f870d1285be39d6eccefd Mon Sep 17 00:00:00 2001 From: vinod <31404750+vinodnextcoder@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:21:50 +0530 Subject: [PATCH 09/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 061bb19..f7221b5 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Run run project go run main.go ``` - + **Testing api use postamn collection** **Api postman collection in root folder gin.postman_collection import in postman** From 41224391afd9e54cc02e7e253a631829eed4ee6a Mon Sep 17 00:00:00 2001 From: vinod Date: Thu, 15 Feb 2024 18:16:00 +0530 Subject: [PATCH 10/14] swagger docs --- controllers/health/health.go | 30 +++++++++----- docs/docs.go | 68 +++++++++++++++++++++++++++++++ docs/swagger.json | 44 ++++++++++++++++++++ docs/swagger.yaml | 30 ++++++++++++++ file-log02-15-2024.txt | 5 +++ go.mod | 30 +++++++++++--- go.sum | 78 ++++++++++++++++++++++++++++++++++++ main.go | 63 +++++++++++++++++++---------- 8 files changed, 312 insertions(+), 36 deletions(-) create mode 100644 docs/docs.go create mode 100644 docs/swagger.json create mode 100644 docs/swagger.yaml diff --git a/controllers/health/health.go b/controllers/health/health.go index aa5d93f..60cd414 100644 --- a/controllers/health/health.go +++ b/controllers/health/health.go @@ -1,17 +1,29 @@ package health import ( - "net/http" - "github.com/gin-gonic/gin" - "github.com/sirupsen/logrus" - "golang-gin-boilerplate/services/logservice" + "golang-gin-boilerplate/services/logservice" + "net/http" + + _ "golang-gin-boilerplate/docs" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" ) +// helloCall godoc +// @Summary hellow example +// @Schemes +// @Description Hello +// @Tags example +// @Accept json +// @Produce json +// @Success 200 {string} Hello, You created a Web App! +// @Router /health [get] func Health() gin.HandlerFunc { return func(c *gin.Context) { - logservice.Info("Health endpoint called", logrus.Fields{"status":"200","functionName":"Health", "controller":"healthcontroller"}) - c.JSON(http.StatusOK, gin.H{ - "message": "Ok", - }) + logservice.Info("Health endpoint called", logrus.Fields{"status": "200", "functionName": "Health", "controller": "healthcontroller"}) + c.JSON(http.StatusOK, gin.H{ + "message": "Ok", + }) } -} \ No newline at end of file +} diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..e43b1a7 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,68 @@ +// Package docs Code generated by swaggo/swag. DO NOT EDIT +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "contact": { + "name": "vinod", + "url": "http://www.swagger.io/support", + "email": "support@swagger.io" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/health": { + "get": { + "description": "Hello", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "example" + ], + "summary": "hellow example", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + } + }, + "externalDocs": { + "description": "OpenAPI", + "url": "https://swagger.io/resources/open-api/" + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "localhost:3001", + BasePath: "/", + Schemes: []string{}, + Title: "Swagger Example API", + Description: "This is a sample gin web server", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..9450745 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,44 @@ +{ + "swagger": "2.0", + "info": { + "description": "This is a sample gin web server", + "title": "Swagger Example API", + "contact": { + "name": "vinod", + "url": "http://www.swagger.io/support", + "email": "support@swagger.io" + }, + "version": "1.0" + }, + "host": "localhost:3001", + "basePath": "/", + "paths": { + "/health": { + "get": { + "description": "Hello", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "example" + ], + "summary": "hellow example", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + } + }, + "externalDocs": { + "description": "OpenAPI", + "url": "https://swagger.io/resources/open-api/" + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 0000000..91e77c5 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,30 @@ +basePath: / +externalDocs: + description: OpenAPI + url: https://swagger.io/resources/open-api/ +host: localhost:3001 +info: + contact: + email: support@swagger.io + name: vinod + url: http://www.swagger.io/support + description: This is a sample gin web server + title: Swagger Example API + version: "1.0" +paths: + /health: + get: + consumes: + - application/json + description: Hello + produces: + - application/json + responses: + "200": + description: OK + schema: + type: string + summary: hellow example + tags: + - example +swagger: "2.0" diff --git a/file-log02-15-2024.txt b/file-log02-15-2024.txt index 0057c36..33bd43e 100644 --- a/file-log02-15-2024.txt +++ b/file-log02-15-2024.txt @@ -1,2 +1,7 @@ {"level":"info","msg":"golang app started","time":"2024-02-15T09:57:52+05:30"} {"level":"info","msg":"golang app started","time":"2024-02-15T09:59:03+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-15T18:09:11+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-15T18:12:48+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-15T18:14:35+05:30"} +{"controller":"healthcontroller","functionName":"Health","level":"info","msg":"Health endpoint called","status":"200","time":"2024-02-15T18:14:52+05:30"} +{"controller":"healthcontroller","functionName":"Health","level":"info","msg":"Health endpoint called","status":"200","time":"2024-02-15T18:14:53+05:30"} diff --git a/go.mod b/go.mod index 77f5611..2fff428 100644 --- a/go.mod +++ b/go.mod @@ -3,38 +3,58 @@ module golang-gin-boilerplate go 1.21.5 require ( + github.com/KyleBanks/depth v1.2.1 // indirect + github.com/PuerkitoBio/purell v1.2.1 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/bytedance/sonic v1.10.2 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.9.1 // indirect + github.com/go-openapi/jsonpointer v0.20.2 // indirect + github.com/go-openapi/jsonreference v0.20.4 // indirect + github.com/go-openapi/spec v0.20.14 // indirect + github.com/go-openapi/swag v0.22.9 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.17.0 // indirect + github.com/go-playground/validator/v10 v10.18.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect - github.com/leodido/go-urn v1.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect + github.com/swaggo/files v1.0.1 // indirect + github.com/swaggo/gin-swagger v1.6.0 // indirect + github.com/swaggo/swag v1.16.3 // indirect github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect + github.com/urfave/cli/v2 v2.27.1 // indirect + github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect golang.org/x/arch v0.7.0 // indirect - golang.org/x/crypto v0.18.0 // indirect - golang.org/x/net v0.20.0 // indirect - golang.org/x/sys v0.16.0 // indirect + golang.org/x/crypto v0.19.0 // indirect + golang.org/x/net v0.21.0 // indirect + golang.org/x/sys v0.17.0 // indirect golang.org/x/text v0.14.0 // indirect + golang.org/x/tools v0.18.0 // indirect google.golang.org/protobuf v1.32.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect gorm.io/driver/mysql v1.5.2 // indirect gorm.io/gorm v1.25.6 // indirect + sigs.k8s.io/yaml v1.4.0 // indirect ) diff --git a/go.sum b/go.sum index 6e682d0..1476a99 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,9 @@ +github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= +github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/PuerkitoBio/purell v1.2.1 h1:QsZ4TjvwiMpat6gBCBxEQI0rcS9ehtkKtSpiUnd9N28= +github.com/PuerkitoBio/purell v1.2.1/go.mod h1:ZwHcC/82TOaovDi//J/804umJFFmbOHPngi8iYYv/Eo= +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/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= @@ -9,6 +15,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM= +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= @@ -17,23 +25,36 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= +github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= +github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdXSSgNeAhojU= +github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4= +github.com/go-openapi/spec v0.20.14 h1:7CBlRnw+mtjFGlPDRZmAMnq35cRzI91xj03HVyUi/Do= +github.com/go-openapi/spec v0.20.14/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw= +github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE= +github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74= github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtPsPm2S9NAZ5nl9U= +github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= @@ -41,10 +62,15 @@ github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/4 github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.3.0 h1:jX8FDLfW4ThVXctBNZ+3cIWnCSnrACDV73r76dy0aQQ= github.com/leodido/go-urn v1.3.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e h1:6b4YTtccT1y/3eSsDCVhB6boPPCh5bQwP1Pa863yH28= github.com/lpernett/godotenv v0.0.0-20230527005122-0de1d4c5ef5e/go.mod h1:K+inF/XYdmRn4sSP3IU4EM3KcOdGVJUJqZPmrQSxjGo= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -55,6 +81,10 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= @@ -69,31 +99,77 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= +github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= +github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M= +github.com/swaggo/gin-swagger v1.6.0/go.mod h1:BG00cCEy294xtVpyIAHG6+e2Qzj/xKlRdOqDkvq0uzo= +github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= +github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk= github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk= github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= +github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= +github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI= +github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= +golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= +golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= @@ -104,3 +180,5 @@ gorm.io/gorm v1.25.6 h1:V92+vVda1wEISSOMtodHVRcUIOPYa2tgQtyF+DfFx+A= gorm.io/gorm v1.25.6/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= +sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= +sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= diff --git a/main.go b/main.go index bd943ea..2dd587a 100644 --- a/main.go +++ b/main.go @@ -1,28 +1,47 @@ package main import ( - "os" - "log" - "github.com/lpernett/godotenv" - "github.com/gin-gonic/gin" - "golang-gin-boilerplate/routes" - "golang-gin-boilerplate/services/logservice" - // "github.com/sirupsen/logrus" - "golang-gin-boilerplate/database" + "golang-gin-boilerplate/routes" + "golang-gin-boilerplate/services/logservice" + "log" + "os" + + "github.com/gin-gonic/gin" + "github.com/lpernett/godotenv" + + // "github.com/sirupsen/logrus" + "golang-gin-boilerplate/database" + _ "golang-gin-boilerplate/docs" + + swaggerFiles "github.com/swaggo/files" + ginSwagger "github.com/swaggo/gin-swagger" ) +// @title Swagger Example API +// @version 1.0 +// @description This is a sample gin web server + +// @contact.name vinod +// @contact.url http://www.swagger.io/support +// @contact.email support@swagger.io + +// @host localhost:3001 +// @BasePath / + +// @externalDocs.description OpenAPI +// @externalDocs.url https://swagger.io/resources/open-api/ func main() { - logservice.InitLogger() - err := godotenv.Load() - if err != nil { - log.Fatal("Error loading .env file") - } - database.InitDb() - logservice.Info("golang app started") - port := os.Getenv("PORT") - router := gin.Default() - routes.HealthRoute(router) - routes.UserRoute(router) - - router.Run("0.0.0.0:" + port) -} \ No newline at end of file + logservice.InitLogger() + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } + database.InitDb() + logservice.Info("golang app started") + port := os.Getenv("PORT") + router := gin.Default() + routes.HealthRoute(router) + routes.UserRoute(router) + router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + router.Run("0.0.0.0:" + port) +} From 7fce62b0c8d667358e8ab41518b3901c228b72bc Mon Sep 17 00:00:00 2001 From: vinod Date: Tue, 20 Feb 2024 14:47:28 +0530 Subject: [PATCH 11/14] swagger docs updated --- controllers/users/users.go | 36 ++++++++++ docs/docs.go | 140 +++++++++++++++++++++++++++++++++++++ docs/swagger.json | 140 +++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 91 ++++++++++++++++++++++++ file-log02-20-2024.txt | 6 ++ 5 files changed, 413 insertions(+) create mode 100644 file-log02-20-2024.txt diff --git a/controllers/users/users.go b/controllers/users/users.go index f776679..4eee014 100644 --- a/controllers/users/users.go +++ b/controllers/users/users.go @@ -10,6 +10,16 @@ import ( "github.com/gin-gonic/gin" ) +// CreateUser create +// @Summary create user +// @Schemes +// @Description create user +// @Tags userCreate +// @Accept json +// @Param user body models.User true "User Data" +// @Produce json +// @Success 201 {string} Success +// @Router /users/v1/create [post] func CreateUser() gin.HandlerFunc { return func(c *gin.Context) { var input models.User @@ -31,6 +41,16 @@ func CreateUser() gin.HandlerFunc { } } +// @Summary update user +// @Schemes +// @Description update user +// @Tags userUpdate +// @Param id path int true "User Id" +// @Param user body models.User true "User Data" +// @Accept json +// @Produce json +// @Success 200 {string} Success +// @Router /users/v1/update/:id [put] func UpdateUser() gin.HandlerFunc { return func(c *gin.Context) { var user models.User @@ -52,6 +72,14 @@ func UpdateUser() gin.HandlerFunc { } } +// @Summary get users list +// @Schemes +// @Description get users list +// @Tags userList +// @Accept json +// @Produce json +// @Success 200 {string} Success +// @Router /users/v1/users [get] func GetUsers() gin.HandlerFunc { return func(c *gin.Context) { var user []models.User @@ -65,6 +93,14 @@ func GetUsers() gin.HandlerFunc { } } +// @Summary delete user +// @Schemes +// @Description delete user +// @Tags deleteUser +// @Accept json +// @Produce json +// @Success 200 {string} Success +// @Router /users/v1/delete/:id [delete] func DeleteUser() gin.HandlerFunc { return func(c *gin.Context) { var user models.User diff --git a/docs/docs.go b/docs/docs.go index e43b1a7..f58bae9 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -41,6 +41,146 @@ const docTemplate = `{ } } } + }, + "/users/v1/create": { + "post": { + "description": "create user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "userCreate" + ], + "summary": "create user", + "parameters": [ + { + "description": "User Data", + "name": "user", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.User" + } + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "type": "string" + } + } + } + } + }, + "/users/v1/delete/:id": { + "delete": { + "description": "delete user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "deleteUser" + ], + "summary": "delete user", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, + "/users/v1/update/:id": { + "put": { + "description": "update user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "userUpdate" + ], + "summary": "update user", + "parameters": [ + { + "type": "integer", + "description": "User Id", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "User Data", + "name": "user", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.User" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, + "/users/v1/users": { + "get": { + "description": "get users list", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "userList" + ], + "summary": "get users list", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + } + }, + "definitions": { + "models.User": { + "type": "object", + "properties": { + "Id": { + "type": "integer" + }, + "email": { + "type": "string" + }, + "name": { + "type": "string" + }, + "password": { + "type": "string" + } + } } }, "externalDocs": { diff --git a/docs/swagger.json b/docs/swagger.json index 9450745..963ec12 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -35,6 +35,146 @@ } } } + }, + "/users/v1/create": { + "post": { + "description": "create user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "userCreate" + ], + "summary": "create user", + "parameters": [ + { + "description": "User Data", + "name": "user", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.User" + } + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "type": "string" + } + } + } + } + }, + "/users/v1/delete/:id": { + "delete": { + "description": "delete user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "deleteUser" + ], + "summary": "delete user", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, + "/users/v1/update/:id": { + "put": { + "description": "update user", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "userUpdate" + ], + "summary": "update user", + "parameters": [ + { + "type": "integer", + "description": "User Id", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "User Data", + "name": "user", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.User" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, + "/users/v1/users": { + "get": { + "description": "get users list", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "userList" + ], + "summary": "get users list", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + } + }, + "definitions": { + "models.User": { + "type": "object", + "properties": { + "Id": { + "type": "integer" + }, + "email": { + "type": "string" + }, + "name": { + "type": "string" + }, + "password": { + "type": "string" + } + } } }, "externalDocs": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 91e77c5..d79fe30 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,4 +1,16 @@ basePath: / +definitions: + models.User: + properties: + Id: + type: integer + email: + type: string + name: + type: string + password: + type: string + type: object externalDocs: description: OpenAPI url: https://swagger.io/resources/open-api/ @@ -27,4 +39,83 @@ paths: summary: hellow example tags: - example + /users/v1/create: + post: + consumes: + - application/json + description: create user + parameters: + - description: User Data + in: body + name: user + required: true + schema: + $ref: '#/definitions/models.User' + produces: + - application/json + responses: + "201": + description: Created + schema: + type: string + summary: create user + tags: + - userCreate + /users/v1/delete/:id: + delete: + consumes: + - application/json + description: delete user + produces: + - application/json + responses: + "200": + description: OK + schema: + type: string + summary: delete user + tags: + - deleteUser + /users/v1/update/:id: + put: + consumes: + - application/json + description: update user + parameters: + - description: User Id + in: path + name: id + required: true + type: integer + - description: User Data + in: body + name: user + required: true + schema: + $ref: '#/definitions/models.User' + produces: + - application/json + responses: + "200": + description: OK + schema: + type: string + summary: update user + tags: + - userUpdate + /users/v1/users: + get: + consumes: + - application/json + description: get users list + produces: + - application/json + responses: + "200": + description: OK + schema: + type: string + summary: get users list + tags: + - userList swagger: "2.0" diff --git a/file-log02-20-2024.txt b/file-log02-20-2024.txt new file mode 100644 index 0000000..af8f27b --- /dev/null +++ b/file-log02-20-2024.txt @@ -0,0 +1,6 @@ +{"level":"info","msg":"golang app started","time":"2024-02-20T14:13:26+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-20T14:33:00+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-20T14:34:35+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-20T14:40:23+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-20T14:45:28+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-20T14:46:27+05:30"} From 82f32de31ac13f4d3c1a042553ec3d64c213e957 Mon Sep 17 00:00:00 2001 From: vinod Date: Wed, 21 Feb 2024 14:23:08 +0530 Subject: [PATCH 12/14] golang password --- controllers/auth/login.go | 29 +++++++++++++++++++++++++++++ controllers/users/users.go | 11 ++++++++++- file-log02-21-2024.txt | 4 ++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 controllers/auth/login.go create mode 100644 file-log02-21-2024.txt diff --git a/controllers/auth/login.go b/controllers/auth/login.go new file mode 100644 index 0000000..1a5ca2a --- /dev/null +++ b/controllers/auth/login.go @@ -0,0 +1,29 @@ +// package login + +// import ( +// "golang-gin-boilerplate/services/logservice" +// "net/http" + +// _ "golang-gin-boilerplate/docs" + +// "github.com/gin-gonic/gin" +// "github.com/sirupsen/logrus" +// ) + +// // helloCall godoc +// // @Summary hellow example +// // @Schemes +// // @Description Hello +// // @Tags example +// // @Accept json +// // @Produce json +// // @Success 200 {string} Hello, You created a Web App! +// // @Router /health [get] +// func Health() gin.HandlerFunc { +// return func(c *gin.Context) { +// logservice.Info("Health endpoint called", logrus.Fields{"status": "200", "functionName": "Health", "controller": "healthcontroller"}) +// c.JSON(http.StatusOK, gin.H{ +// "message": "Ok", +// }) +// } +// } diff --git a/controllers/users/users.go b/controllers/users/users.go index 4eee014..b142cfc 100644 --- a/controllers/users/users.go +++ b/controllers/users/users.go @@ -8,6 +8,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "golang.org/x/crypto/bcrypt" ) // CreateUser create @@ -27,8 +28,16 @@ func CreateUser() gin.HandlerFunc { c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: Invalid input", Data: ""}) return } - fmt.Println(input) + fmt.Println(input.Password) userCreate := models.User(input) + hash, err := bcrypt.GenerateFromPassword([]byte(input.Password), 14) + if err != nil { + fmt.Println("something went wrong in db query") + c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: Password error", Data: ""}) + panic(err) + } + fmt.Println("j", hash) + input.Password = string(hash) result := database.Db.Create(&userCreate) if result.Error != nil { fmt.Println("something went wrong in db query") diff --git a/file-log02-21-2024.txt b/file-log02-21-2024.txt new file mode 100644 index 0000000..812fea2 --- /dev/null +++ b/file-log02-21-2024.txt @@ -0,0 +1,4 @@ +{"level":"info","msg":"golang app started","time":"2024-02-21T14:08:57+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T14:09:39+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T14:16:03+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T14:21:26+05:30"} From c8e44cb5d41fa0769cc07fb8a48ae10698c99454 Mon Sep 17 00:00:00 2001 From: vinod Date: Wed, 21 Feb 2024 15:24:10 +0530 Subject: [PATCH 13/14] golang password --- controllers/auth/login.go | 74 ++++++++++++++++++++++++-------------- controllers/users/users.go | 2 +- file-log02-21-2024.txt | 17 +++++++++ main.go | 1 + models/user.go | 5 +++ routes/auth.go | 11 ++++++ 6 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 routes/auth.go diff --git a/controllers/auth/login.go b/controllers/auth/login.go index 1a5ca2a..32f07af 100644 --- a/controllers/auth/login.go +++ b/controllers/auth/login.go @@ -1,29 +1,51 @@ -// package login +package login -// import ( -// "golang-gin-boilerplate/services/logservice" -// "net/http" +import ( + "fmt" + "golang-gin-boilerplate/database" + _ "golang-gin-boilerplate/docs" + "golang-gin-boilerplate/models" + responses "golang-gin-boilerplate/services/apiresponse" + "golang-gin-boilerplate/services/logservice" + "net/http" -// _ "golang-gin-boilerplate/docs" + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "golang.org/x/crypto/bcrypt" +) -// "github.com/gin-gonic/gin" -// "github.com/sirupsen/logrus" -// ) - -// // helloCall godoc -// // @Summary hellow example -// // @Schemes -// // @Description Hello -// // @Tags example -// // @Accept json -// // @Produce json -// // @Success 200 {string} Hello, You created a Web App! -// // @Router /health [get] -// func Health() gin.HandlerFunc { -// return func(c *gin.Context) { -// logservice.Info("Health endpoint called", logrus.Fields{"status": "200", "functionName": "Health", "controller": "healthcontroller"}) -// c.JSON(http.StatusOK, gin.H{ -// "message": "Ok", -// }) -// } -// } +// Login godoc +// @Summary login +// @Schemes +// @Description Login +// @Tags Login +// @Accept json +// @Produce json +// @Success 200 {string} login api +// @Router /login [get] +func Login() gin.HandlerFunc { + return func(c *gin.Context) { + logservice.Info("Login endpoint called", logrus.Fields{"status": "200", "functionName": "Login", "controller": "logincontroller"}) + var input models.UserLogin + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: Invalid input", Data: ""}) + return + } + var email = input.Email + fmt.Println(input) + var existingUser models.User + if err := database.Db.Where("Email = ?", email).First(&existingUser).Error; err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: User not found", Data: ""}) + return + } + fmt.Println(existingUser) + err1 := bcrypt.CompareHashAndPassword([]byte(existingUser.Password), []byte(input.Password)) + if err1 != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: Invalid email or password", Data: ""}) + return + } + c.JSON(http.StatusOK, gin.H{ + "message": "Ok", + }) + } +} diff --git a/controllers/users/users.go b/controllers/users/users.go index b142cfc..da51ea3 100644 --- a/controllers/users/users.go +++ b/controllers/users/users.go @@ -37,7 +37,7 @@ func CreateUser() gin.HandlerFunc { panic(err) } fmt.Println("j", hash) - input.Password = string(hash) + userCreate.Password = string(hash) result := database.Db.Create(&userCreate) if result.Error != nil { fmt.Println("something went wrong in db query") diff --git a/file-log02-21-2024.txt b/file-log02-21-2024.txt index 812fea2..84d9c3e 100644 --- a/file-log02-21-2024.txt +++ b/file-log02-21-2024.txt @@ -2,3 +2,20 @@ {"level":"info","msg":"golang app started","time":"2024-02-21T14:09:39+05:30"} {"level":"info","msg":"golang app started","time":"2024-02-21T14:16:03+05:30"} {"level":"info","msg":"golang app started","time":"2024-02-21T14:21:26+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T14:46:44+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T14:47:59+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T14:48:05+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T15:13:04+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T15:13:12+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T15:17:05+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T15:17:10+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T15:18:03+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T15:18:51+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T15:19:13+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T15:20:10+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T15:20:15+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T15:21:29+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T15:21:34+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-21T15:23:20+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T15:23:24+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-21T15:23:45+05:30"} diff --git a/main.go b/main.go index 2dd587a..d1012bc 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ func main() { router := gin.Default() routes.HealthRoute(router) routes.UserRoute(router) + routes.AuthRoute(router) router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) router.Run("0.0.0.0:" + port) } diff --git a/models/user.go b/models/user.go index d7a1fae..180e570 100644 --- a/models/user.go +++ b/models/user.go @@ -13,3 +13,8 @@ type UserUpdate struct { Email string `json:"email"` Password string `json:"password"` } + +type UserLogin struct { + Email string `json:"email"` + Password string `json:"password"` +} diff --git a/routes/auth.go b/routes/auth.go new file mode 100644 index 0000000..8cd32f4 --- /dev/null +++ b/routes/auth.go @@ -0,0 +1,11 @@ +package routes + +import ( + login "golang-gin-boilerplate/controllers/auth" + + "github.com/gin-gonic/gin" +) + +func AuthRoute(router *gin.Engine) { + router.POST("/auth/login", login.Login()) +} From d8303da5e51ca69ea79ab371a3df44be2dfc9fbb Mon Sep 17 00:00:00 2001 From: vinod Date: Thu, 22 Feb 2024 14:36:12 +0530 Subject: [PATCH 14/14] jwt token --- controllers/auth/login.go | 41 +++++++++++++++++++++++++++++++++++++-- file-log02-22-2024.txt | 8 ++++++++ go.mod | 1 + go.sum | 2 ++ models/user.go | 5 +++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 file-log02-22-2024.txt diff --git a/controllers/auth/login.go b/controllers/auth/login.go index 32f07af..5faf144 100644 --- a/controllers/auth/login.go +++ b/controllers/auth/login.go @@ -8,7 +8,9 @@ import ( responses "golang-gin-boilerplate/services/apiresponse" "golang-gin-boilerplate/services/logservice" "net/http" + "time" + "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "golang.org/x/crypto/bcrypt" @@ -44,8 +46,43 @@ func Login() gin.HandlerFunc { c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: Invalid email or password", Data: ""}) return } - c.JSON(http.StatusOK, gin.H{ - "message": "Ok", + + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "user": existingUser.Email, + "id": existingUser.Id, + "sub": existingUser.Id, + "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), + }) + + tokenStr, err := token.SignedString([]byte("supersaucysecret")) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "Failed to create token", + }) + return + } + + Refreshtoken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "user": existingUser.Email, + "id": existingUser.Id, + "sub": existingUser.Id, + "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), }) + + refreshtoken, err := Refreshtoken.SignedString([]byte("supersaucysecret")) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "Failed to create token", + }) + return + } + var res models.SignedResponse + c.SetSameSite(http.SameSiteLaxMode) + c.SetCookie("access_token", tokenStr, 3600*24*30, "", "", false, true) + c.SetCookie("refresh_token", refreshtoken, 3600*24*30, "", "", false, true) + res.Access_token = tokenStr + res.Refresh_token = refreshtoken + c.JSON(http.StatusOK, responses.SuccesResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": res}}) + } } diff --git a/file-log02-22-2024.txt b/file-log02-22-2024.txt new file mode 100644 index 0000000..bf9111a --- /dev/null +++ b/file-log02-22-2024.txt @@ -0,0 +1,8 @@ +{"level":"info","msg":"golang app started","time":"2024-02-22T13:58:28+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T13:59:05+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-22T14:01:00+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T14:01:13+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-22T14:30:22+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T14:30:31+05:30"} +{"level":"info","msg":"golang app started","time":"2024-02-22T14:34:14+05:30"} +{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T14:34:23+05:30"} diff --git a/go.mod b/go.mod index 2fff428..c825e74 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect + github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.9.1 // indirect diff --git a/go.sum b/go.sum index 1476a99..3c17873 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0q github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= diff --git a/models/user.go b/models/user.go index 180e570..a9af4bd 100644 --- a/models/user.go +++ b/models/user.go @@ -18,3 +18,8 @@ type UserLogin struct { Email string `json:"email"` Password string `json:"password"` } + +type SignedResponse struct { + Access_token string `json:"access_token"` + Refresh_token string `json:"refresh_token"` +}