Skip to content

Commit

Permalink
Add swagger/controller
Browse files Browse the repository at this point in the history
  • Loading branch information
euiyounghwang committed Jan 2, 2024
1 parent 4f13848 commit bd16234
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ docs


2)

# https://github.com/swaggo/gin-swagger
# https://dadev.tistory.com/entry/GO-RESTAPI-%EB%AC%B8%EC%84%9C%ED%99%94-%EB%8F%84%EA%B5%AC-Swagger-Gin-Framework-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0

# http://localhost:9081/docs/index.html
Expand Down
60 changes: 57 additions & 3 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const docTemplate = `{
}
},
"/es/search": {
"get": {
"post": {
"description": "search engine api",
"consumes": [
"application/json"
Expand All @@ -48,12 +48,35 @@ const docTemplate = `{
"Search"
],
"summary": "search engine api",
"parameters": [
{
"description": "Search Info Body",
"name": "search",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/swagger_search.Search"
}
}
],
"responses": {
"200": {
"description": "OK"
"description": "OK",
"schema": {
"$ref": "#/definitions/swagger_search.Search"
}
},
"400": {
"description": "Bad Request"
"description": "We need ID!!",
"schema": {
"$ref": "#/definitions/swagger_search.APIError"
}
},
"404": {
"description": "Can not find ID",
"schema": {
"$ref": "#/definitions/swagger_search.APIError"
}
}
}
}
Expand Down Expand Up @@ -111,6 +134,37 @@ const docTemplate = `{
"example": "John"
}
}
},
"swagger_search.APIError": {
"type": "object",
"properties": {
"errorCode": {
"type": "integer"
},
"errorMessage": {
"type": "string"
}
}
},
"swagger_search.Search": {
"type": "object",
"properties": {
"age": {
"description": "Age",
"type": "integer",
"example": 10
},
"id": {
"description": "UserId",
"type": "integer",
"example": 1
},
"name": {
"description": "Name",
"type": "string",
"example": "John"
}
}
}
}
}`
Expand Down
60 changes: 57 additions & 3 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
},
"/es/search": {
"get": {
"post": {
"description": "search engine api",
"consumes": [
"application/json"
Expand All @@ -37,12 +37,35 @@
"Search"
],
"summary": "search engine api",
"parameters": [
{
"description": "Search Info Body",
"name": "search",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/swagger_search.Search"
}
}
],
"responses": {
"200": {
"description": "OK"
"description": "OK",
"schema": {
"$ref": "#/definitions/swagger_search.Search"
}
},
"400": {
"description": "Bad Request"
"description": "We need ID!!",
"schema": {
"$ref": "#/definitions/swagger_search.APIError"
}
},
"404": {
"description": "Can not find ID",
"schema": {
"$ref": "#/definitions/swagger_search.APIError"
}
}
}
}
Expand Down Expand Up @@ -100,6 +123,37 @@
"example": "John"
}
}
},
"swagger_search.APIError": {
"type": "object",
"properties": {
"errorCode": {
"type": "integer"
},
"errorMessage": {
"type": "string"
}
}
},
"swagger_search.Search": {
"type": "object",
"properties": {
"age": {
"description": "Age",
"type": "integer",
"example": 10
},
"id": {
"description": "UserId",
"type": "integer",
"example": 1
},
"name": {
"description": "Name",
"type": "string",
"example": "John"
}
}
}
}
}
41 changes: 39 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ definitions:
example: John
type: string
type: object
swagger_search.APIError:
properties:
errorCode:
type: integer
errorMessage:
type: string
type: object
swagger_search.Search:
properties:
age:
description: Age
example: 10
type: integer
id:
description: UserId
example: 1
type: integer
name:
description: Name
example: John
type: string
type: object
info:
contact: {}
paths:
Expand All @@ -31,17 +53,32 @@ paths:
description: Bad Request
summary: test swagger api
/es/search:
get:
post:
consumes:
- application/json
description: search engine api
parameters:
- description: Search Info Body
in: body
name: search
required: true
schema:
$ref: '#/definitions/swagger_search.Search'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/swagger_search.Search'
"400":
description: Bad Request
description: We need ID!!
schema:
$ref: '#/definitions/swagger_search.APIError'
"404":
description: Can not find ID
schema:
$ref: '#/definitions/swagger_search.APIError'
summary: search engine api
tags:
- Search
Expand Down
10 changes: 5 additions & 5 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"net/http"
"os"

swagger_search "go-search_engine/swagger_controller"

"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"

swagger_search "go-search_engine/swagger_controller"
)

/* 아래 항목이 swagger에 의해 문서화 된다. */
Expand All @@ -31,9 +31,9 @@ import (
func main() {
r := gin.Default()

//코드로 SwaggerInfo 속성을 지정해지만 doc.json 정상적으로 조회된다.
// https://github.com/swaggo/swag/blob/master/README.md
docs.SwaggerInfo.Title = "Golang Rest API"

// 127.0.0.1:8080/docs/index.html 주로 swagger로 생성된 문서를 확인 수 있다.
r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

Expand All @@ -46,7 +46,7 @@ func main() {

v1Search := r.Group("/")
{
v1Search.GET("/es/search", swagger_search.SearchHandler)
v1Search.POST("/es/search", swagger_search.SearchHandler)
}

httpPort := os.Getenv("PORT")
Expand Down
38 changes: 37 additions & 1 deletion swagger_controller/search_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import (
"github.com/gin-gonic/gin"
)

type Search struct {
Id int `json:"id" example:"1"` // UserId
Name string `json:"name" example:"John"` // Name
Age int `json:"age" example:"10"` // Age
}


type APIError struct {
ErrorCode int
ErrorMessage string
}

/*
// SearchHandler godoc
// @Summary search engine api
// @tags Search
Expand All @@ -18,4 +31,27 @@ import (
// @Failure 400
func SearchHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hello world!"})
}
}
*/

// SearchHandler godoc
// @Summary search engine api
// @tags Search
// @Description search engine api
// @Accept json
// @Produce json
// @Router /es/search [post]
// @Param search body Search true "Search Info Body"
// @content application/json
// @Success 200 {object} Search
// @Failure 400 {object} APIError "We need ID!!"
// @Failure 404 {object} APIError "Can not find ID"
func SearchHandler(c *gin.Context) {
var search Search
if err := c.BindJSON(&search); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// c.JSON(http.StatusOK, gin.H{"message": "success"})
c.JSON(http.StatusOK, gin.H{"message": search})
}

0 comments on commit bd16234

Please sign in to comment.