Swagger API Documentation
This project demonstrates how to integrate Swagger for API documentation in a Go application.
Prerequisites
Ensure you have the following installed:
- Golang
- Swag for generating Swagger docs
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git
cd recipes/swagger -
Install dependencies:
go get -u github.com/swaggo/swag/cmd/swag
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
Generating Swagger Docs
- Generate the Swagger documentation:
swag init
Running the Application
-
Start the application:
go run main.go
-
Access the Swagger UI: Open your browser and navigate to
http://localhost:8080/swagger/index.html
Example
Here is an example of how to document an API endpoint using Swag:
// @Summary Show an account
// @Description get string by ID
// @ID get-string-by-int
// @Accept json
// @Produce json
// @Param id path int true "Account ID"
// @Success 200 {object} model.Account
// @Failure 400 {object} http.Response
// @Failure 404 {object} http.Response
// @Router /accounts/{id} [get]
func GetAccount(c *gin.Context) {
// Your code here
}