### Example Router Setup with Dependency (Go) Source: https://github.com/diegoclair/goswag/blob/main/README.md Shows a typical router setup function that requires a database connection, illustrating why a separate setup is needed for dependency-free Swagger generation. ```Go func SetupRoutes(db *sql.DB) *echo.Echo { e := echo.New() handlers := handlers.New(db) e.GET("/hello", handlers.HandleHello) return e } ``` -------------------------------- ### Makefile Target for Swagger Generation Source: https://github.com/diegoclair/goswag/blob/main/README.md Provides a Makefile target (`docs`) to automate the installation of the `swag` tool and the execution of the Swagger documentation generation commands. ```Makefile .PHONY: docs docs: @go install github.com/swaggo/swag/cmd/swag@latest @cd goswag && \ go run main.go && \ cd .. && \ swag init --pdl=2 --parseInternal -g ./goswag/main.go -o ./docs && \ swag fmt -d ./goswag/ ``` -------------------------------- ### Recommended Swagger Generation Main Function (Go) Source: https://github.com/diegoclair/goswag/blob/main/README.md Demonstrates the recommended approach for generating Swagger documentation in a dedicated `main.go` file. It shows how to call the router setup function with a mock dependency (like `nil`) and then invoke `GenerateSwagger()`. Includes necessary package-level Swagger annotations. ```Go // @title GoSwag example API // @version 1.0 func main() { // Here you have already used goswag for your route setup, added annotations and change it return to goswag.Echo or Gin interfaces ge := server.SetupRoutes(nil) ge.GenerateSwagger() //will generate your swagger } ``` -------------------------------- ### Example Generated Swagger Annotations with Default Responses (Go) Source: https://github.com/diegoclair/goswag/blob/main/README.md Illustrates how the default responses configured during router instantiation appear in the generated Swagger annotations for individual route handlers. ```Go // @Summary Logout // @Description Logout the user // @Tags auth // @Param user-token header string true "User access token" // @Success 200 // @Failure 400 {object} YourStructOfError // @Failure 401 {object} YourStructOfError // @Router /auth/logout [post] func handleLogout() {} //nolint:unused // @Summary Login // @Description Login the user // @Tags auth // @Param user-token header string true "User access token" // @Success 200 // @Failure 400 {object} YourStructOfError // @Failure 401 {object} YourStructOfError // @Router /auth/logout [post] func handleLogin() {} //nolint:unused ``` -------------------------------- ### Adding Default Responses to Router (Go) Source: https://github.com/diegoclair/goswag/blob/main/README.md Shows how to define a slice of `models.ReturnType` representing default HTTP responses (e.g., 400, 401) and pass them to the router constructor (`NewEcho` or `NewGin`) to automatically apply these responses to all defined routes. ```Go defaultResponses := []models.ReturnType{ { StatusCode: http.StatusBadRequest, Body: YourStructOfError, }, { StatusCode: http.StatusUnauthorized, Body: YourStructOfError, }, } // pass the default responses to instance of your chosen framework e := NewEcho(defaultResponses) ``` -------------------------------- ### Defining a Return Type for Route Annotations - Go Source: https://github.com/diegoclair/goswag/blob/main/README.md This struct defines the structure used to specify the possible return types and their details for a route annotation in goswag. It includes the HTTP status code, the body structure, and an optional map to override field names/types in the generated documentation. ```Go type ReturnType struct { StatusCode int Body any // example: map[jsonFieldName]fieldType{} OverrideStructFields map[string]any } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.