GoInit - Go Gin API Starter Template & Generator
A comprehensive Go API starter template built with Gin framework, featuring authentication, user management, real-time communication, and a CLI tool to generate n## π Quick Start
Once installed, here's how to get started quickly:projects from the template.
π― What's Included
β
Core Features
ποΈ Architecture
- Clean Architecture with separation of concerns
- Dependency Injection container
- Repository Pattern for data access
- Service Layer for business logic
- Middleware for cross-cutting concerns
- Automated Git Hooks for template synchronization
οΏ½ Installation
GoInit can be installed and used in several ways depending on your needs.
Prerequisites
- Go 1.21+ - Download here
- Git - For cloning the repository
- Optional: Docker for containerized development
Method 1: Install CLI Generator (Recommended)
From Source
# Clone the repository
git clone https://github.com/SOG-web/goinit/goinit.git
cd goinit
# Run the installation script
chmod +x install.sh
./install.sh
# Verify installation
goinit --version
From Pre-built Binaries
-
Go to Releases
-
Download the appropriate binary for your platform:
goinit-linux-amd64 (Linux)
goinit-darwin-amd64 (macOS Intel)
goinit-darwin-arm64 (macOS Apple Silicon)
goinit-windows-amd64.exe (Windows)
-
Make executable and install:
# Linux/macOS
chmod +x goinit-*
sudo mv goinit-* /usr/local/bin/goinit
# Windows: Move to a directory in your PATH
-
Verify installation:
goinit --version
Method 2: Use Template Directly
Clone and Setup
# Clone the repository
git clone https://github.com/SOG-web/goinit/goinit.git
cd goinit/gin
# Install Go dependencies
go mod tidy
# Copy environment configuration
cp .env.example .env
# Edit configuration (optional)
nano .env # or your preferred editor
Docker Installation
# Clone repository
git clone https://github.com/SOG-web/goinit/goinit.git
cd goinit/gin
# Start with Docker Compose
docker-compose up --build
# Or build manually
docker build -t goinit-api .
docker run -p 8080:8080 --env-file .env goinit-api
Method 3: Go Install (Recommended)
# Install CLI generator directly
go install github.com/SOG-web/goinit@latest
# Generate a new project
goinit
# Follow the interactive prompts to configure your project
π‘ Note: After installation, the CLI tool is available as goinit command.
Verification
After installation, verify everything works:
# Check CLI generator
goinit --help
# Test template (if using directly)
cd gin
go run cmd/api/main.go
# Should start server on http://localhost:8080
Post-Installation Setup
Environment Configuration
Edit .env file with your settings:
# Server
PORT=8080
PUBLIC_HOST=http://localhost:8080
# Database
DB_DRIVER=sqlite
DB_NAME=myapp.db
# Authentication
JWT_SECRET=your-super-secret-jwt-key-here
SESSION_SECRET=your-session-secret-here
# Email (optional)
USE_LOCAL_EMAIL=true # For development
Database Setup
For production databases:
# SQLite (default - no setup needed)
# Files are created automatically
# MySQL
mysql -u root -p
CREATE DATABASE myapp;
GRANT ALL PRIVILEGES ON myapp.* TO 'user'@'localhost' IDENTIFIED BY 'password';
# PostgreSQL
psql -U postgres
CREATE DATABASE myapp;
CREATE USER myuser WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE myapp TO myuser;
οΏ½π Quick Start
Generate New Project (Fastest)
# Generate a new project
goinit
# Follow the interactive prompts
# Your project will be ready in minutes!
Use Template Directly
# Navigate to template
cd gin
# Quick start with default settings
go run cmd/api/main.go
# Visit http://localhost:8080/docs for API documentation
Docker Quick Start
cd gin
docker-compose up --build
π‘ Tip: See the Installation section above for detailed setup instructions.
π Project Structure
Repository Structure
This repository contains both the template and the CLI generator:
goinit/
βββ gin/ # ποΈ API Template Source
β βββ cmd/api/ # Application entry point
β βββ config/ # Configuration management
β βββ internal/ # Internal application code
β βββ api/ # HTTP handlers and routes
β βββ docs/ # API documentation
β βββ docker/ # Docker configuration
βββ .github/workflows/ # οΏ½ GitHub Actions CI/CD
βββ main.go # CLI generator entry point
βββ go.mod # Go module for CLI generator
βββ install.sh # Installation script
βββ README.md # This file
Generated Project Structure
When you use the CLI generator, it creates a new project with this structure:
your-project/
βββ cmd/api/ # Application entry point
βββ config/ # Configuration management
βββ internal/
β βββ app/ # Application services
β βββ data/ # Data layer (repositories)
β βββ domain/ # Domain models
β βββ lib/ # Shared libraries
β β βββ email/ # Email service
β β βββ jwt/ # JWT utilities
β β βββ pwreset/ # Password reset
β β βββ storage/ # File storage
β βββ server/ # Server setup
βββ api/ # HTTP handlers and routes
β βββ common/
β β βββ dto/ # Data transfer objects
β β βββ middleware/ # HTTP middleware
β βββ protocol/
β βββ http/
β β βββ handler/ # HTTP handlers
β β βββ router/ # Route setup
β β βββ routes/ # Route definitions
β βββ sse/ # Server-Sent Events
β βββ ws/ # WebSocket handlers
βββ docs/ # API documentation
βββ docker/ # Docker configuration
βββ .env.example # Environment template
βββ go.mod # Go module file
π§ Configuration
The application uses environment variables for configuration. Key settings:
Server
PORT=8080
PUBLIC_HOST=http://localhost:8080
GIN_MODE=debug
Database
DB_DRIVER=sqlite # sqlite, mysql, postgres
DB_NAME=myapp
DB_USER=root
DB_PASSWORD=password
DB_HOST=localhost
DB_PORT=3306
Authentication
JWT_SECRET=your-jwt-secret
SESSION_SECRET=your-session-secret
USE_DATABASE_JWT=false # true for database, false for Redis
Email
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USERNAME=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
USE_LOCAL_EMAIL=true # true for development logging
Real-time Features
REDIS_ADDR=localhost:6379 # For JWT blacklist and sessions
π‘ API Endpoints
Authentication
POST /api/auth/register/ - User registration
POST /api/auth/login/ - User login
GET /api/auth/logout/ - User logout
POST /api/auth/change-password/ - Change password
POST /api/auth/password-reset/request/ - Request password reset
POST /api/auth/password-reset/confirm/ - Confirm password reset
User Management
GET /api/user/profile/ - Get user profile
PUT /api/user/profile/ - Update user profile
POST /api/user/profile/image/ - Upload profile image
Real-time
GET /api/sse/events - Server-Sent Events stream
GET /api/sse/notifications - Notification SSE stream
GET /api/ws/connect - WebSocket connection
Admin (requires admin role)
GET /api/admin/users/ - List all users
GET /api/admin/stats/ - User statistics
PUT /api/admin/users/:id/activate/ - Activate user
PUT /api/admin/users/:id/deactivate/ - Deactivate user
Health Check
GET /health - Server health check
π Authentication
The API supports multiple authentication methods:
JWT Bearer Token
curl -H "Authorization: Bearer <jwt-token>" \
http://localhost:8080/api/user/profile/
Session Cookies
Session cookies are automatically managed by the session middleware.
π§ Email Templates
The application includes email templates for:
- User registration verification
- Password reset
- Welcome emails
- Admin notifications
ποΈ Database Migrations
The application uses GORM's auto-migration feature. Models are automatically migrated on startup.
π§ͺ Testing
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific test
go test -run TestUserService ./internal/app/user/
οΏ½ GitHub Actions & Releases
This project includes automated CI/CD pipelines:
Automated Releases
- Trigger: Push a version tag (e.g.,
v1.0.0)
- Builds: Cross-platform binaries for Linux, macOS (Intel/Apple Silicon), and Windows
- Artifacts: Automatically uploaded to GitHub Releases
- Workflow:
.github/workflows/release.yml
Creating a Release
# Create and push a version tag
git tag v1.0.0
git push origin v1.0.0
# GitHub Actions will automatically:
# 1. Build binaries for all platforms
# 2. Create release archives
# 3. Upload to GitHub Releases
Development Workflow
- Pre-commit hooks: Automatically sync template files
- Pre-push hooks: Ensure template consistency before pushing
- Automated testing: Run tests on all platforms
- Code quality: Automated linting and formatting
οΏ½ Docker Support
Development
# From the template directory
cd gin
docker-compose up --build
Production
# From the template directory
cd gin
# Build production image
docker build -f Dockerfile -t my-gin-api .
# Run with environment
docker run -p 8080:8080 --env-file .env my-gin-api
οΏ½π API Documentation
When running the generated project, visit: http://localhost:8080/docs/
The documentation is auto-generated from code comments using Swagger.
π§ Development
Adding New Features
- Create domain models in
internal/domain/
- Implement repository in
internal/data/
- Add service logic in
internal/app/
- Create HTTP handler in
api/protocol/http/handler/
- Add routes in
api/protocol/http/routes/
- Register dependencies in
internal/di/container.go
Git Hooks
This project includes automated Git hooks for template synchronization:
- Pre-commit: Automatically copies updated template files before commits
- Pre-push: Ensures template consistency before pushing changes
- Location:
.git/hooks/ (automatically installed)
The hooks ensure that the CLI generator always uses the latest template files.
π€ Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Update documentation
- Submit a pull request
π License
This project is licensed under the MIT License.
π Acknowledgments
Repository: https://github.com/SOG-web/goinit/goinit
Happy coding! π
For questions or issues, please open a GitHub issue.