Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Golang/Swagger] Apply Swagger to describe the information API on Golang

Posted on October 30, 2023December 29, 2023 By nim 2 Comments on [Golang/Swagger] Apply Swagger to describe the information API on Golang

Sau khi chúng ta thực hiện code xong 1 api thì chúng ta cần note lên 1 swagger để các teams khác integrate sẽ dễ dàng hơn

Với echo framework thì chúng ta sẽ sử dụng echo-swagger

Bạn sẽ cần chạy các command dưới đây, cùng vị trí với file main.go

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

tiếp đến bạn chạy câu lệnh swag init để nó generate ra các file docs của swagger.

swag init

Tiếp theo bạn cần thêm:
_ "github.com/swaggo/echo-swagger/example/docs" // docs is generated by Swag CLI, you have to import it.

Tiếp đến là bạn gõ:

go get -u github.com/swaggo/echo-swagger

Tiếp theo nếu bạn bị issues “no required module provides package github.com/alecthomas/template”

GOROOT=C:\Program Files\Go #gosetup
GOPATH=C:\Users\mrnim\go #gosetup
"C:\Program Files\Go\bin\go.exe" build -o C:\Users\mrnim\AppData\Local\JetBrains\GoLand2023.2\tmp\GoLand\___go_build_jougan.exe jougan #gosetup
docs\docs.go:11:2: no required module provides package github.com/alecthomas/template; to add it:
    go get github.com/alecthomas/template

Compilation finished with exit code 1

Bạn sẽ cần pull package xuống

PS C:\Users\mrnim\GolandProjects\jougan> go get -u github.com/alecthomas/template
go: added github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751

OK giờ bạn cần thêm một số thứ vào file main.go

// @title Jougan 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
e.GET("/swagger/*", echoSwagger.WrapHandler)

Tiếp đến bạn sửa từng Handler đang nhận request từ API

Chúng ta sẽ breakdown config bên dưới:

// SignUp godoc
// @Summary Test Donwnload File
// @Description	Measure Download File and Save to Disk
// @Tags inspect
// @Accept  json
// @Produce  json
// @Param data body model.DownloadFile true "measure"
// @Success 200 {object} model.Response
// @Failure 400 {object} model.Response
// @Router /inspect/download-url [post]
  • Summary: Short description of the endpoint, “Test Download File”.
  • Description: Longer description, “Measure Download File and Save to Disk”.
  • Tags: Categorize the endpoint under “inspect”.
  • Param: Annotation is used to describe a parameter for the API endpoint in Swagger documentation:
    • data: The name of the parameter.
    • body: The location of the parameter (in the request body).
    • model.DownloadFile: values are used to request
    • true: Indicates the parameter is required.
    • "measure": this value is used to respond.

and

Xong bạn chạy lại swag init
và truy cập: http://localhost:1994/swagger/index.html

Notice:

Nếu các bạn không muốn force link của swagger thì bạn cần bỏ 2 dòng này:

// @host petstore.swagger.io
// @BasePath /v2
Golang

Post navigation

Previous Post: [Oauth2-Proxy] Oauth2-Proxy encounters issue with Cognito
Next Post: [AWS] Optimizing Image Storage in Amazon ECR: Understanding Layer Reuse and Immutability.

More Related Articles

[Golang] Chuẩn bị 1 project golang. Coding
[Golang] Note mấy thứ hay ho về golang Coding
[Golang] How do you read each line inside a file? Golang
[Golang] Implement Job Queue inside GOlang. Golang
[Terminal/Huh] Build Terminal Form By golang. Golang
[Golang] Generate the Binary Files on Multi Architecture by Github Action Golang

Comments (2) on “[Golang/Swagger] Apply Swagger to describe the information API on Golang”

  1. Alvina Miller says:
    November 14, 2023 at 6:45 pm

    Hi,

    I intend to contribute a guest post to your website that will help you get good traffic as well as interest your readers.

    Shall I send you the topics then?

    Best,
    Alvina Miller

    Reply
    1. nim says:
      November 16, 2023 at 10:50 am

      Great!
      I just saw your comments in my post.
      If you want to share your knowledge on my page, I will be willing to create an account for you!

      Nimtechnology.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Laravel] Laravel Helpful June 26, 2025
  • [VScode] Hướng dẫn điều chỉnh font cho terminal June 20, 2025
  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.