Golang

[Golang] Gin-Swagger

GenieLove! 2022. 3. 30. 00:01
728x90
반응형

1. swagger 설치

go get -u github.com/swaggo/swag/cmd/swag

# main.go가 있는 위치에서
swag

swag 입력 시 이미지처럼 나와야 잘 설치 된 것

go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files

2.swagger 작성

(1)main.go

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host 3.39.24.154:8000
// @BasePath /api/v1
func main() {
	...
}

(2)login.go

//swagger.json에 쓰기 위한 설명들
// login godoc
// @Summary 요약
// @Description 상세 요약
// @name login
// @Accept json
// @Produce json
// @Param user body loginUser true "User ID and password"
// @Success 200 {object} models.User
// @Failure 400 {object} models.User
// @Failure 404 {object} models.User
// @Failure 500 {object} models.User
// @Router /login [post]
func Login(c *gin.Context) {
	...
}


//설명
// @Param {Param Name} {Param Type} {Data Type} {isMandatory} {Description}
// parameter type - query, path, header, body, formData
// Data type - string, integer, number, boolean, struct

3. swagger 적용

swag init

- 적용 안 되고 error 시 swag init --parseDependency --parseInternal

 

4.적용 예시

728x90
반응형

'Golang' 카테고리의 다른 글

[Golang] 채널, 컨텍스트  (0) 2022.04.18
[Golang] 고루틴과 동시성  (0) 2022.04.17
[Golang] 에러 핸들링  (0) 2022.03.24
[Golang] 자료구조  (0) 2022.03.23
[Golang] 함수  (1) 2022.03.21