gopenapi

module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2025 License: MIT

README ΒΆ

GopenAPI

CI Release Go Report Card GoDoc

A modern OpenAPI code generator for Go with Gin framework support.

GopenAPI generates production-ready Go servers from OpenAPI 3.0 specifications with clean separation between generated and user code, ensuring your customizations are never overwritten.

✨ Features

  • πŸš€ Clean Code Generation - Separate generated and user code
  • πŸ”„ Safe Regeneration - Never overwrites your custom code
  • 🎯 Gin Framework - Built-in support for Gin HTTP router
  • πŸ“ Type Safety - Strong typing from OpenAPI schemas
  • πŸ›‘οΈ Production Ready - Graceful shutdown, middleware support
  • πŸ“š Auto Documentation - Generates comprehensive README
  • πŸ§ͺ Well Tested - 76%+ test coverage

πŸ“¦ Installation

go install github.com/shubhamku044/gopenapi/cmd/gopenapi@latest
Option 2: Download Binary

Download the latest binary from GitHub Releases:

# Linux/macOS
curl -L https://github.com/shubhamku044/gopenapi/releases/latest/download/gopenapi_linux_amd64.tar.gz | tar xz

# macOS (ARM)
curl -L https://github.com/shubhamku044/gopenapi/releases/latest/download/gopenapi_darwin_arm64.tar.gz | tar xz

# Windows
# Download gopenapi_windows_amd64.zip from releases page
Option 3: Homebrew (Coming Soon)
brew install shubhamku044/tap/gopenapi
Option 4: Build from Source
git clone https://github.com/shubhamku044/gopenapi.git
cd gopenapi
go build -o gopenapi ./cmd/gopenapi

πŸš€ Quick Start

1. Create an OpenAPI spec (api.yaml)
openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /users:
    get:
      operationId: listUsers
      summary: List users
      responses:
        '200':
          description: OK
    post:
      operationId: createUser  
      summary: Create user
      responses:
        '201':
          description: Created
  /users/{id}:
    get:
      operationId: getUser
      summary: Get user by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        email:
          type: string
          format: email
2. Generate your API server
gopenapi --spec=api.yaml --output=myapi --package=myapi
3. Implement your business logic

Edit myapi/handlers/api.go:

func (h *APIHandlers) ListUsers(c *gin.Context) {
    users := []models.User{
        {Id: "1", Name: "John Doe", Email: "john@example.com"},
        {Id: "2", Name: "Jane Smith", Email: "jane@example.com"},
    }
    c.JSON(http.StatusOK, users)
}

func (h *APIHandlers) CreateUser(c *gin.Context) {
    var user models.User
    if err := c.ShouldBindJSON(&user); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }
    
    // Save user to database...
    user.Id = "generated-id"
    
    c.JSON(http.StatusCreated, user)
}

func (h *APIHandlers) GetUser(c *gin.Context, id string) {
    // Fetch user from database...
    user := models.User{Id: id, Name: "John Doe", Email: "john@example.com"}
    c.JSON(http.StatusOK, user)
}
4. Run your server
cd myapi
go mod tidy
go run main.go

Your API server is now running on http://localhost:8080! πŸŽ‰

πŸ—οΈ Project Structure

GopenAPI creates a clean project structure that separates your code from generated code:

myapi/
β”œβ”€β”€ main.go              # ✏️  Your application entry point
β”œβ”€β”€ go.mod              # ✏️  Your module definition  
β”œβ”€β”€ handlers/           # ✏️  YOUR BUSINESS LOGIC
β”‚   └── api.go         #     Implement your handlers here
β”œβ”€β”€ generated/          # πŸ€– Generated code (safe to regenerate)
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── interfaces.go  # API interface definitions
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── models.go      # Data models from OpenAPI spec
β”‚   └── server/
β”‚       └── router.go      # HTTP server and routing
└── README.md           # πŸ“š Generated documentation

βœ… Safe files (never overwritten): main.go, go.mod, handlers/ πŸ”„ Generated files (safe to regenerate): Everything in generated/

πŸ“– Usage Examples

Generate with custom package name
gopenapi --spec=api.yaml --output=./my-service --package=myservice
Update existing project (safe regeneration)
gopenapi --spec=updated-api.yaml --output=. --package=myapi
Help and options
gopenapi --help

πŸ†š Comparison with Other Tools

Feature GopenAPI oapi-codegen go-swagger
Safe Regeneration βœ… ❌ ❌
Gin Support βœ… βœ… ❌
Clean Separation βœ… ❌ ❌
Auto Documentation βœ… ❌ βœ…
Type Safety βœ… βœ… βœ…
Production Ready βœ… ⚠️ βœ…

🀝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Setup
git clone https://github.com/shubhamku044/gopenapi.git
cd gopenapi
go mod download

# Run tests
make test

# Run linter  
make lint

# Build binary
make build

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Inspired by oapi-codegen
  • Built with Gin web framework
  • OpenAPI specification support

πŸ“ž Support


Made with ❀️ by Shubham Kumar

Directories ΒΆ

Path Synopsis
cmd
gopenapi command
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL