diff --git a/Makefile b/Makefile index bb6591f..971b8d9 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/src/app/dto/common.dto.go b/src/app/dto/common.dto.go index 809897d..611c90e 100644 --- a/src/app/dto/common.dto.go +++ b/src/app/dto/common.dto.go @@ -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"` diff --git a/src/app/handler/auth/auth.handler.go b/src/app/handler/auth/auth.handler.go index 1cd063c..6a63325 100644 --- a/src/app/handler/auth/auth.handler.go +++ b/src/app/handler/auth/auth.handler.go @@ -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) diff --git a/src/docs/docs.go b/src/docs/docs.go new file mode 100644 index 0000000..49241f9 --- /dev/null +++ b/src/docs/docs.go @@ -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) +} diff --git a/src/docs/markdown/api.md b/src/docs/markdown/api.md new file mode 100644 index 0000000..b36f587 --- /dev/null +++ b/src/docs/markdown/api.md @@ -0,0 +1 @@ +# Johnjud API \ No newline at end of file diff --git a/src/docs/markdown/auth.md b/src/docs/markdown/auth.md new file mode 100644 index 0000000..918eaf4 --- /dev/null +++ b/src/docs/markdown/auth.md @@ -0,0 +1,2 @@ +# Auth Tag API Documentation +**Auth** functions goes here \ No newline at end of file diff --git a/src/docs/swagger.json b/src/docs/swagger.json new file mode 100644 index 0000000..418f356 --- /dev/null +++ b/src/docs/swagger.json @@ -0,0 +1,207 @@ +{ + "schemes": [ + "https", + "http" + ], + "swagger": "2.0", + "info": { + "description": "# Johnjud API", + "title": "JohnJud API", + "contact": { + "name": "ISD Team", + "email": "sd.team.sgcu@gmail.com" + }, + "version": "1.0" + }, + "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" + } + ] +} \ No newline at end of file diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml new file mode 100644 index 0000000..2bb7a3d --- /dev/null +++ b/src/docs/swagger.yaml @@ -0,0 +1,142 @@ +definitions: + dto.BadReqErrResponse: + properties: + failed_field: + type: string + message: + type: string + value: {} + type: object + dto.ResponseBadRequestErr: + properties: + data: + items: + $ref: '#/definitions/dto.BadReqErrResponse' + type: array + message: + example: Invalid request body + type: string + status_code: + example: 400 + type: integer + type: object + dto.ResponseConflictErr: + properties: + data: {} + message: + example: Conflict + type: string + status_code: + example: 409 + type: integer + type: object + dto.ResponseInternalErr: + properties: + data: {} + message: + example: Internal service error + type: string + status_code: + example: 500 + type: integer + type: object + dto.ResponseServiceDownErr: + properties: + data: {} + message: + example: Service is down + type: string + status_code: + example: 503 + type: integer + type: object + dto.SignupRequest: + properties: + email: + type: string + firstname: + type: string + lastname: + type: string + password: + maxLength: 30 + minLength: 6 + type: string + required: + - email + - firstname + - lastname + - password + type: object + dto.SignupResponse: + properties: + email: + type: string + firstname: + type: string + id: + type: string + lastname: + type: string + type: object +info: + contact: + email: sd.team.sgcu@gmail.com + name: ISD Team + description: '# Johnjud API' + title: JohnJud API + version: "1.0" +paths: + /v1/auth/signup: + post: + consumes: + - application/json + description: Return the data of user if successfully + parameters: + - description: signup request dto + in: body + name: signup + required: true + schema: + $ref: '#/definitions/dto.SignupRequest' + produces: + - application/json + 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' + summary: Signup user + tags: + - auth +schemes: +- https +- http +securityDefinitions: + AuthToken: + description: Description for what is this security definition being used + in: header + name: Authorization + type: apiKey +swagger: "2.0" +tags: +- description: |- + # Auth Tag API Documentation + **Auth** functions goes here + name: auth diff --git a/src/main.go b/src/main.go index b1ed616..86daab0 100644 --- a/src/main.go +++ b/src/main.go @@ -26,6 +26,23 @@ import ( "time" ) +// @title JohnJud API +// @version 1.0 +// @description.markdown + +// @contact.name ISD Team +// @contact.email sd.team.sgcu@gmail.com + +// @schemes https http + +// @securityDefinitions.apikey AuthToken +// @in header +// @name Authorization +// @description Description for what is this security definition being used + +// @tag.name auth +// @tag.description.markdown + func main() { conf, err := config.LoadConfig() if err != nil {