Skip to content

Commit

Permalink
chore: create swagger doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitiwat-owen committed Dec 26, 2023
1 parent 2174678 commit 1c9bc49
Show file tree
Hide file tree
Showing 9 changed files with 621 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ test:
server:
go run ./src/.

mockGen:
mock-gen:
mockgen -source ./src/pkg/service/auth/auth.service.go -destination ./src/mocks/service/auth/auth.mock.go
mockgen -source ./src/pkg/service/user/user.service.go -destination ./src/mocks/service/user/user.mock.go
mockgen -source ./src/app/validator/validator.go -destination ./src/mocks/validator/validator.mock.go
mockgen -source ./src/app/router/context.go -destination ./src/mocks/router/context.mock.go

create-doc:
swag init -d ./src -o ./src/docs -md ./src/docs/markdown
6 changes: 6 additions & 0 deletions src/app/dto/common.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ type ResponseNotfoundErr struct {
Data interface{} `json:"data"`
}

type ResponseConflictErr struct {
StatusCode int `json:"status_code" example:"409"`
Message string `json:"message" example:"Conflict"`
Data interface{} `json:"data"`
}

type ResponseInternalErr struct {
StatusCode int `json:"status_code" example:"500"`
Message string `json:"message" example:"Internal service error"`
Expand Down
13 changes: 13 additions & 0 deletions src/app/handler/auth/auth.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ func (h *Handler) RefreshToken(c router.IContext) {

}

// Signup is a function that create user in database
// @Summary Signup user
// @Description Return the data of user if successfully
// @Param signup body dto.SignupRequest true "signup request dto"
// @Tags auth
// @Accept json
// @Produce json
// @Success 201 {object} dto.SignupResponse
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body"
// @Failure 409 {object} dto.ResponseConflictErr "Duplicate email"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/auth/signup [post]
func (h *Handler) Signup(c router.IContext) {
request := &dto.SignupRequest{}
err := c.Bind(request)
Expand Down
229 changes: 229 additions & 0 deletions src/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
// 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": "ISD Team",
"email": "sd.team.sgcu@gmail.com"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/v1/auth/signup": {
"post": {
"description": "Return the data of user if successfully",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"auth"
],
"summary": "Signup user",
"parameters": [
{
"description": "signup request dto",
"name": "signup",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.SignupRequest"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/dto.SignupResponse"
}
},
"400": {
"description": "Invalid request body",
"schema": {
"$ref": "#/definitions/dto.ResponseBadRequestErr"
}
},
"409": {
"description": "Duplicate email",
"schema": {
"$ref": "#/definitions/dto.ResponseConflictErr"
}
},
"500": {
"description": "Internal service error",
"schema": {
"$ref": "#/definitions/dto.ResponseInternalErr"
}
},
"503": {
"description": "Service is down",
"schema": {
"$ref": "#/definitions/dto.ResponseServiceDownErr"
}
}
}
}
}
},
"definitions": {
"dto.BadReqErrResponse": {
"type": "object",
"properties": {
"failed_field": {
"type": "string"
},
"message": {
"type": "string"
},
"value": {}
}
},
"dto.ResponseBadRequestErr": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/dto.BadReqErrResponse"
}
},
"message": {
"type": "string",
"example": "Invalid request body"
},
"status_code": {
"type": "integer",
"example": 400
}
}
},
"dto.ResponseConflictErr": {
"type": "object",
"properties": {
"data": {},
"message": {
"type": "string",
"example": "Conflict"
},
"status_code": {
"type": "integer",
"example": 409
}
}
},
"dto.ResponseInternalErr": {
"type": "object",
"properties": {
"data": {},
"message": {
"type": "string",
"example": "Internal service error"
},
"status_code": {
"type": "integer",
"example": 500
}
}
},
"dto.ResponseServiceDownErr": {
"type": "object",
"properties": {
"data": {},
"message": {
"type": "string",
"example": "Service is down"
},
"status_code": {
"type": "integer",
"example": 503
}
}
},
"dto.SignupRequest": {
"type": "object",
"required": [
"email",
"firstname",
"lastname",
"password"
],
"properties": {
"email": {
"type": "string"
},
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
},
"password": {
"type": "string",
"maxLength": 30,
"minLength": 6
}
}
},
"dto.SignupResponse": {
"type": "object",
"properties": {
"email": {
"type": "string"
},
"firstname": {
"type": "string"
},
"id": {
"type": "string"
},
"lastname": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"AuthToken": {
"description": "Description for what is this security definition being used",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
},
"tags": [
{
"description": "# Auth Tag API Documentation\n**Auth** functions goes here",
"name": "auth"
}
]
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "",
BasePath: "",
Schemes: []string{"https", "http"},
Title: "JohnJud API",
Description: "# Johnjud API",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
1 change: 1 addition & 0 deletions src/docs/markdown/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Johnjud API
2 changes: 2 additions & 0 deletions src/docs/markdown/auth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auth Tag API Documentation
**Auth** functions goes here
Loading

0 comments on commit 1c9bc49

Please sign in to comment.