This is a template API implementation using Go and the Gin framework, with automatic Swagger documentation generation.
- Go - Programming language
- Gin - Web framework
- gin-swagger - Swagger/OpenAPI documentation generation
- RESTful API endpoints
- Automatic Swagger/OpenAPI documentation
- Automated Swagger 2.0 to OpenAPI 3.0.3 conversion (pure Go implementation)
- CORS support
- Health check endpoint
- Example integration with external API (JSONPlaceholder)
- Proper error handling and response formatting
- Go 1.21 or higher
- Internet connection (for external API calls)
-
Navigate to the API directory:
cd api -
Install dependencies:
go mod tidy
-
Install swag tool (for Swagger generation):
go install github.com/swaggo/swag/cmd/swag@latest
-
Ensure swag is in your PATH (add this to your shell profile if needed):
export PATH=$PATH:$(go env GOPATH)/bin
-
Generate OpenAPI 3.0.3 documentation:
# From repository root directory ./generate-openapi.sh -
Start the server:
go run main.go
The API will be available at http://localhost:8080
GET /api/v1/health- Health checkGET /api/v1/posts- Get all posts from JSONPlaceholderGET /api/v1/posts/{id}- Get a specific post by IDGET /swagger/index.html- Swagger UI documentation
The API automatically generates OpenAPI/Swagger documentation through the following workflow:
- Swagger 2.0 Generation:
swag initgenerates Swagger 2.0 format indocs/swagger.json - Conversion to OpenAPI 3.0.3: A Go converter transforms it to OpenAPI 3.0.3 format
- Output: Final specification is placed in
../openapi-specifications/api.swagger.json
The documentation is:
- Served at
/swagger/index.htmlwhen the server is running (Swagger 2.0 format) - Available as OpenAPI 3.0.3 in
../openapi-specifications/api.swagger.jsonfor tooling
To regenerate OpenAPI 3.0.3 documentation after making changes:
Complete workflow (Recommended):
# From repository root
./generate-openapi.shThis script will:
- Run
swag initto generate Swagger 2.0 documentation inapi/docs/ - Convert Swagger 2.0 to OpenAPI 3.0.3 format using a Go converter
- Place the result in
openapi-specifications/api.swagger.json - Test that
npm run gen-schemaandnpm run mockwork correctly
Manual steps:
# 1. Generate Swagger 2.0
cd api && swag init && cd ..
# 2. Convert to OpenAPI 3.0.3
go run convert-swagger.go
# 3. Test React Native tooling
cd mobile-app
npm run gen-schema # Generate TypeScript definitions
npm run mock # Start mock server on port 3001