This README provides instructions for setting up a Go project, running the application, generating Swagger documentation, and deploying it with ReDocly.
- Go (1.16+)
- MongoDB
- Git
- NPM
Clone your Go project repository:
git clone https://github.com/# Go Project Setup and Swagger Documentation Guide
This README provides instructions for setting up a Go project, running the application, generating Swagger documentation, and deploying it with ReDocly.
- Go (1.16+)
- MongoDB
- Git
- Docker (for ReDocly deployment)
Clone your Go project repository:
git clone https://github.com/yourusername/yourproject.git
cd yourprojectyourusername/yourproject.git
cd yourproject
Make sure you have Go installed and your GOPATH is set up. Then, install the required Go dependencies:
go mod tidy
Create a .env file in the root of your project and configure the necessary environment variables:
MONGO_USER=your_mongo_user
MONGO_PASS=your_mongo_password
MONGO_HOST=localhost
MONGO_PORT=27017
MONGO_DB=your_database_name
PORT=8080
Start the application:
go run main.go
The application will start and listen on port 8080 (or the port specified in your .env file).
You need to install the swaggo tools for generating Swagger documentation:
go install github.com/swaggo/swag/cmd/swag@latest
Ensure your Go code is properly annotated with Swagger comments. For example:
// @title My API
// @version 1.0
// @description This is a sample server.
// @host localhost:8080
// @BasePath /
Annotate your routes with Swagger comments:
// getItems retrieves items from MongoDB with pagination support
// @Summary Get items
// @Description Get all items from the database with pagination
// @Tags items
// @Accept json
// @Produce json
// @Param page query int false "Page number"
// @Param limit query int false "Number of items per page"
// @Success 200 {object} PaginatedItemsResponse "Successfully retrieved items"
// @Failure 400 {object} Response "Invalid request"
// @Failure 500 {object} Response "Internal server error"
// @Router /items [get]
func getItems(c *gin.Context) {
// Function implementation
}
Run the swag command to generate the Swagger documentation:
swag init
To view the Swagger UI, you need to add a route to serve it in your main.go:
import (
"github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
)
func main() {
// Other setup code...
r.GET("/swagger/*any", ginSwagger.WrapHandler(files.Handler))
// Run the server
r.Run(":" + port)
}
Sign up for a ReDocly account at ReDocly.
Install the ReDocly CLI tool:
npm install -g @redocly/cli
Navigate to your project directory and initialize ReDocly:
redocly init
Follow the prompts to set up your project.
Deploy your Swagger documentation to ReDocly:
redocly preview-docs
This command will preview your API documentation locally. To deploy it, use:
redocly push --file docs/swagger.yaml
This uploads your Swagger documentation to ReDocly, where you can manage and publish it.
- Swagger Annotations Not Showing: Ensure that your Swagger annotations are correct and re-run swag init.
- Environment Variables: Double-check that all required environment variables are set correctly in your .env file.
- [Go Documentation](https://golang.org/doc/)
- [Swaggo GitHub Repository](https://github.com/swaggo/swag)
- [ReDocly Documentation](https://redocly.com/docs)
This project is licensed under the MIT License. See the LICENSE file for details.
- Setup Instructions: Instructions to clone the repository, install dependencies, and run the Go application.
- Swagger Documentation: Steps for installing Swagger tools, annotating code, generating Swagger files, and serving Swagger UI.
- Deploying with ReDocly: Steps to sign up, install CLI tools, initialize, and deploy Swagger documentation with ReDocly.
- Troubleshooting: Common issues and their solutions.
- Additional Resources: Links to Go documentation, Swaggo, and ReDocly resources.
- License: Licensing information for the project.