goinit

command module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 9 Imported by: 0

README ΒΆ

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
  • Authentication & Authorization

    • JWT-based authentication with refresh tokens
    • Session management with cookies
    • Password reset functionality
    • Role-based access control (admin/user)
  • User Management

    • User registration and login
    • Profile management
    • User roles and permissions
    • Admin user controls
  • Real-time Communication

    • Server-Sent Events (SSE) for notifications
    • WebSocket support for bidirectional communication
    • Real-time event streaming
  • Database Support

    • SQLite (default, file-based)
    • MySQL
    • PostgreSQL
    • GORM ORM with auto-migration
  • Email Integration

    • SMTP email sending
    • Local email logging for development
    • HTML email templates
  • Storage Solutions

    • Local file storage
    • S3-compatible storage (AWS S3, MinIO, etc.)
  • API Documentation

    • Swagger/OpenAPI 3.0 documentation
    • Auto-generated API docs
    • Interactive API testing
  • Project Generator CLI

    • Interactive project setup
    • Customizable configuration
    • Automated dependency management
    • Cross-platform builds
πŸ—οΈ 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
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
  1. Go to Releases

  2. 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)
  3. Make executable and install:

    # Linux/macOS
    chmod +x goinit-*
    sudo mv goinit-* /usr/local/bin/goinit
    
    # Windows: Move to a directory in your PATH
    
  4. 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
# 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
  1. Create domain models in internal/domain/
  2. Implement repository in internal/data/
  3. Add service logic in internal/app/
  4. Create HTTP handler in api/protocol/http/handler/
  5. Add routes in api/protocol/http/routes/
  6. 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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Update documentation
  6. 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.

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
gin
cmd/api
Package main bootstraps the House of Rou API server.
Package main bootstraps the House of Rou API server.
docs
Package docs Code generated by swaggo/swag.
Package docs Code generated by swaggo/swag.
internal/di
Package di provides dependency injection setup for the application.
Package di provides dependency injection setup for the application.

Jump to

Keyboard shortcuts

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