Skip to content

bograh/Go-Backend-Generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

πŸš€ Go Backend Generator

A powerful CLI tool that generates modern Go backend projects with Gin, GORM, JWT authentication, and enterprise-grade project structure.

✨ Features

  • 🎯 Interactive CLI with beautiful terminal UI (using Bubble Tea)
  • πŸ—οΈ Modern Tech Stack: Gin web framework, GORM ORM, JWT auth
  • πŸ—ƒοΈ Multiple Databases: PostgreSQL, MySQL, SQLite, MongoDB support
  • πŸ” Authentication: JWT-based auth with bcrypt password hashing
  • πŸ“ Enterprise Structure: Clean architecture with services, handlers, middleware
  • 🐳 Docker Support: Dockerfile and docker-compose.yml generation
  • πŸ§ͺ Testing: Unit test templates included
  • πŸ”§ Auto-Migration: Database tables created automatically
  • 🌍 Environment Config: .env file support with godotenv

πŸ“‹ Prerequisites

  • Go 1.21+ installed on your system
  • Git (optional, for cloning)
  • Database (PostgreSQL/MySQL/SQLite/MongoDB) - optional, projects can run without DB

πŸš€ Quick Start

1. Clone & Build

# Clone the repository
git clone <repository-url>
cd go-backend-generator

# Install dependencies
go mod tidy

# Build the generator
go build -o go-backend-gen main.go

2. Run the Generator

# Run the interactive generator
./go-backend-gen

# Or run directly with Go
go run main.go

# Add to system path
sudo cp go-backend-gen /usr/local/bin/

# Run
go-backend-gen

3. Follow the Interactive Prompts

The generator will ask you:

  1. Project Name: my-awesome-api
  2. Go Module Name: github.com/username/my-awesome-api
  3. Database Choice: PostgreSQL, MySQL, SQLite, MongoDB, or None
  4. Features:
    • βœ… Authentication (JWT-based)
    • βœ… Docker Support
    • βœ… REST API (User CRUD)
    • βœ… Unit Tests

4. Generated Project Structure

my-awesome-api/
β”œβ”€β”€ cmd/server/main.go              # Application entry point
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ handlers/               # HTTP handlers (Gin)
β”‚   β”‚   β”‚   β”œβ”€β”€ health.go          # Health check endpoint
β”‚   β”‚   β”‚   └── user.go            # User CRUD endpoints
β”‚   β”‚   β”œβ”€β”€ middleware/            # Auth middleware
β”‚   β”‚   └── routes/                # Route setup
β”‚   β”œβ”€β”€ auth/                      # Authentication service
β”‚   β”œβ”€β”€ config/                    # Configuration management
β”‚   β”œβ”€β”€ database/                  # GORM database connection
β”‚   β”œβ”€β”€ models/                    # Data models with GORM tags
β”‚   β”œβ”€β”€ services/                  # Business logic layer
β”‚   └── utils/                     # Utility functions
β”œβ”€β”€ migrations/                    # Database migrations
β”œβ”€β”€ .env                          # Environment variables
β”œβ”€β”€ .env.example                  # Example environment file
β”œβ”€β”€ Dockerfile                    # Docker configuration
β”œβ”€β”€ docker-compose.yml            # Docker Compose setup
β”œβ”€β”€ go.mod                        # Go dependencies
└── README.md                     # Project documentation

πŸ› οΈ Generated Project Usage

Running the Generated Project

cd my-awesome-api

# Install dependencies
go mod tidy

# Set up environment (optional)
cp .env.example .env
# Edit .env with your database credentials

# Run the server
go run main.go

API Endpoints

The generated project includes these endpoints:

Method Endpoint Description
GET /health Health check
POST /api/v1/auth/register User registration
POST /api/v1/auth/login User login
GET /api/v1/users Get all users
POST /api/v1/users Create user
GET /api/v1/users/:id Get user by ID
PUT /api/v1/users/:id Update user
DELETE /api/v1/users/:id Delete user

Example API Usage

# Health check
curl http://localhost:8080/health

# Register a new user
curl -X POST http://localhost:8080/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "username": "johndoe",
    "password": "secretpassword",
    "first_name": "John",
    "last_name": "Doe"
  }'

# Login
curl -X POST http://localhost:8080/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "password": "secretpassword"
  }'

# Get users (requires authentication)
curl -X GET http://localhost:8080/api/v1/users \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

🐳 Docker Support

If you enabled Docker support, you can run the generated project with:

# Build and run with Docker Compose
docker-compose up --build

# Or build manually
docker build -t my-awesome-api .
docker run -p 8080:8080 my-awesome-api

πŸ—„οΈ Database Setup

PostgreSQL

createdb my-awesome-api
export DATABASE_URL="postgres://localhost/my-awesome-api?sslmode=disable"

MySQL

mysql -u root -p -e "CREATE DATABASE my_awesome_api;"
export DATABASE_URL="root:password@tcp(localhost:3306)/my_awesome_api"

SQLite

# No setup needed - file will be created automatically
export DATABASE_URL="my-awesome-api.db"

πŸ§ͺ Testing

Run tests in the generated project:

cd my-awesome-api
go test ./...

πŸ”§ Development

Generator Development

To modify the generator itself:

  1. Edit Templates: Update templates in main.go
  2. Add Features: Modify the ProjectConfig struct and generation logic
  3. Test Changes: Run go run main.go to test your changes

Generated Project Development

The generated projects follow these patterns:

  • Handlers: HTTP request/response logic in internal/api/handlers/
  • Services: Business logic in internal/services/
  • Models: Data structures in internal/models/
  • Database: GORM setup in internal/database/
  • Config: Environment configuration in internal/config/

πŸ›‘οΈ Security Features

  • βœ… Password Hashing: bcrypt with salt
  • βœ… JWT Authentication: Secure token-based auth
  • βœ… Input Validation: Struct validation tags
  • βœ… Database Security: GORM prevents SQL injection
  • βœ… CORS Ready: Easy to add CORS middleware

πŸ“¦ Dependencies

Generator Dependencies

  • github.com/charmbracelet/bubbletea - Terminal UI
  • github.com/charmbracelet/lipgloss - Terminal styling

Generated Project Dependencies

  • github.com/gin-gonic/gin - Web framework
  • gorm.io/gorm - ORM
  • github.com/golang-jwt/jwt/v5 - JWT authentication
  • golang.org/x/crypto - Password hashing
  • github.com/joho/godotenv - Environment variables
  • github.com/google/uuid - UUID generation

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

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

πŸ™ Acknowledgments


Happy Coding! πŸŽ‰

Generate modern Go backends in seconds!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages