Skip to content

Commit

Permalink
adding swagger gin swaggo
Browse files Browse the repository at this point in the history
  • Loading branch information
bhattaraibishal50 committed Aug 29, 2020
1 parent b94be3d commit 0ef8278
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 8 deletions.
75 changes: 75 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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": {},
"license": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {}
}`

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{})
}
8 changes: 8 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"swagger": "2.0",
"info": {
"contact": {},
"license": {}
},
"paths": {}
}
5 changes: 5 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
info:
contact: {}
license: {}
paths: {}
swagger: "2.0"
26 changes: 26 additions & 0 deletions error/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package error

type responseError struct {
originalErr error
contextInfo errorContext
}

type errorContext struct {
Field string
Message string
}

// Error returns the message of a responseError
func (e responseError) Error() string {
return e.originalErr.Error()
}

// //New register the new error
// func New(err error, msg string) *responseError {
// return responseError{
// originalError: err,
// contextInfo: errorContext{
// Message: msg
// }
// }
// }
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ go 1.15
require (
cloud.google.com/go/firestore v1.3.0
firebase.google.com/go v3.13.0+incompatible
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
github.com/go-openapi/swag v0.19.9 // indirect
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/uuid v1.1.1
github.com/howeyc/fsnotify v0.9.0 // indirect
github.com/jinzhu/gorm v1.9.16
github.com/json-iterator/go v1.1.10 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
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/thinkerou/favicon v0.1.0
github.com/urfave/cli/v2 v2.2.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20200822124328-c89045814202
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 // indirect
golang.org/x/tools v0.0.0-20200828161849-5deb26317202 // indirect
google.golang.org/api v0.30.0
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
100 changes: 100 additions & 0 deletions go.sum

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ import (
"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"
)

// swagger embed files

func main() {
//setting up the application
fmt.Println("UUID ::", common.GetNewUUID())
Expand All @@ -19,5 +26,6 @@ func main() {
// 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
}
Binary file modified tmp/runner-build
Binary file not shown.
12 changes: 6 additions & 6 deletions user/model/userModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package model

// UserModel usermodel
type UserModel struct {
ID uint `gorm:"primary_key"`
Username string `gorm:"column:username"`
Email string `gorm:"column:email;unique_index"`
Bio string `gorm:"column:bio;size:1024"`
Image *string `gorm:"column:image"`
PasswordHash string `gorm:"column:password;not null"`
ID uint `gorm:"primary_key" json:"ID"`
Username string `gorm:"column:username" json:"Username" `
Email string `gorm:"column:email;unique_index" json:"Email"`
Bio string `gorm:"column:bio;size:1024" json:"Bio"`
Image string `gorm:"column:image" json:"Image"`
Password string `gorm:"column:password;not null" json:"Password"`
}
5 changes: 3 additions & 2 deletions user/route/userRoute.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package route

import (
"github/bhattaraibishal50/blog/user/usecase"
userUsecase "github/bhattaraibishal50/blog/user/usecase"

"github.com/gin-gonic/gin"
)

// UserRoute exports user routes
func UserRoute(routerGroup *gin.RouterGroup) {
routerGroup.GET("/", usecase.GetUsers)
routerGroup.GET("/", userUsecase.GetUsers)
routerGroup.POST("/signup", userUsecase.Signup)
}
10 changes: 10 additions & 0 deletions user/usecase/userUsecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ import (
func GetUsers(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "on users"})
}

// Login login the suer
func Login(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "on login"})
}

// Signup signup the user
func Signup(c *gin.Context) {
//username
}

0 comments on commit 0ef8278

Please sign in to comment.