Getting Started with switctl
This tutorial walks you through using switctl to create your first microservice and explores the key features of the CLI tool.
Prerequisites
- Go 1.23.12 or later
- Git
- Basic understanding of microservices
Installation
1. Build from Source
# Clone the Swit framework
git clone https://github.com/innovationmech/swit.git
cd swit
# Build switctl CLI
make build
# Verify installation
./bin/switctl --version2. Add to PATH
# Add to your shell profile (~/.bashrc, ~/.zshrc)
export PATH="$PATH:/path/to/swit/bin"
# Or create a symlink
sudo ln -s /path/to/swit/bin/switctl /usr/local/bin/switctl3. Verify Installation
switctl --help
# Should show the CLI help with all available commandsYour First Service with switctl
Step 1: Create a New Service
# Create a user management service
switctl new service user-serviceThe CLI will prompt you for options:
✓ Service template: http-grpc
✓ Database type: postgresql
✓ Authentication: jwt
✓ Include Docker files: yes
✓ Include CI/CD files: yesStep 2: Explore Generated Structure
cd user-service
treeGenerated structure:
user-service/
├── cmd/user-service/
│ └── main.go # Service entry point
├── internal/
│ ├── handler/
│ │ ├── http/ # HTTP handlers
│ │ └── grpc/ # gRPC handlers
│ ├── service/ # Business logic
│ └── config/ # Configuration
├── api/
│ └── proto/ # Protocol buffers
├── configs/
│ ├── development.yaml # Development config
│ └── production.yaml # Production config
├── Dockerfile # Container configuration
├── Makefile # Build automation
├── docker-compose.yml # Local development
├── .switctl.yaml # CLI configuration
├── go.mod # Go modules
└── README.md # Service documentationStep 3: Build and Run
# Initialize Go modules
go mod tidy
# Build the service
make build
# Run in development mode
make run
# Or run directly
go run cmd/user-service/main.goYour service is now running on:
- HTTP: http://localhost:8080
- gRPC: localhost:9080
Step 4: Test the Service
# Health check
curl http://localhost:8080/health
# API status
curl http://localhost:8080/api/v1/status
# User operations (generated CRUD endpoints)
curl http://localhost:8080/api/v1/usersAdding Components to Your Service
Generate API Endpoints
# Generate CRUD API for products
switctl generate api product --methods=crud --validation=true
# Generate custom API
switctl generate api order \
--methods=get,post,put \
--middleware=auth,loggingGenerate Middleware
# Generate authentication middleware
switctl generate middleware auth --type=jwt
# Generate rate limiting middleware
switctl generate middleware rate-limit \
--apply-to=http \
--config=trueGenerate Database Models
# Generate User model with GORM
switctl generate model User \
--database=gorm \
--validation=true \
--migration=trueThis generates:
internal/model/user.go- Model definitioninternal/repository/user_repository.go- Database operationsmigrations/001_create_users.sql- Database migration
Quality Assurance with switctl
Run All Checks
switctl check --allThis runs:
- ✅ Code formatting (gofmt)
- ✅ Code quality (golint)
- ✅ Security scanning
- ✅ Dependency vulnerabilities
- ✅ Test coverage
- ✅ Performance benchmarks
Specific Checks
# Security only
switctl check --security
# Found 2 issues:
# - Use of weak cryptographic primitive (MD5)
# - Potential SQL injection in query
# Tests with coverage
switctl check --tests --coverage --threshold=80
# Coverage: 85% (above threshold ✓)
# Performance benchmarks
switctl check --performance
# All benchmarks within acceptable limits ✓Development Workflow
1. Watch Mode for Development
# Auto-rebuild on file changes
switctl dev watchThis monitors your code and automatically:
- Rebuilds the service
- Runs tests
- Restarts the development server
2. Generate Documentation
# Generate service documentation
switctl dev docs --format=html --output=./docs3. Dependency Management
# Check for outdated dependencies
switctl deps check
# Update dependencies safely
switctl deps update --securityWorking with Templates
List Available Templates
switctl template listAvailable templates:
basic- Simple HTTP servicehttp-grpc- HTTP + gRPC servicefull-featured- All features enabledgrpc-only- Pure gRPC service
Use Specific Template
switctl new service payment-service \
--template=full-featured \
--database=postgresql \
--auth=jwt \
--cache=redis \
--monitoring=sentry \
--queue=rabbitmqCustom Template
Create your own template for team standards:
# Create template structure
mkdir -p ~/.switctl/templates/company-standard
# Generate template
switctl template create company-standard \
--based-on=http-grpc \
--add-feature=monitoring \
--add-feature=tracingConfiguration Management
Project Configuration
switctl creates .switctl.yaml for project-specific settings:
project:
name: "user-service"
type: "microservice"
templates:
default: "http-grpc"
database:
type: "postgresql"
migrations: true
testing:
coverage_threshold: 80
race_detection: true
security:
enabled: true
scan_deps: trueGlobal Configuration
Set global defaults:
# Set preferred template
switctl config set template.default full-featured
# Set database preference
switctl config set database.type postgresql
# Set coverage threshold
switctl config set testing.coverage_threshold 85Advanced Features
Plugin System
Extend switctl with custom plugins:
# List available plugins
switctl plugin list
# Install plugin for OpenAPI generation
switctl plugin install openapi-gen
# Use plugin
switctl openapi generate --input=./api/proto --output=./docs/openapi.yamlCI/CD Integration
Generated services include CI/CD configuration:
# GitHub Actions (automatically generated)
.github/workflows/
├── ci.yml # Build and test
├── security.yml # Security scans
└── deploy.yml # Deployment
# GitLab CI (with --cicd=gitlab)
.gitlab-ci.ymlMulti-Service Projects
Manage multiple services in a monorepo:
# Initialize multi-service project
switctl init company-platform --type=monorepo
# Add services
switctl new service user-service --directory=services/
switctl new service order-service --directory=services/
switctl new service notification-service --directory=services/
# Generate shared components
switctl generate shared-lib common --type=utilsBest Practices
1. Start Simple, Scale Up
# Begin with basic template
switctl new service my-service --template=basic
# Add features as needed
switctl generate api user --methods=crud
switctl generate middleware auth --type=jwt2. Use Quality Checks Early
# Run checks during development
switctl check --quality --security
# Set up pre-commit hooks
echo "switctl check --all" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit3. Maintain Configuration
# Keep .switctl.yaml in version control
git add .switctl.yaml
# Document team standards
switctl config set team.standards "company-standard"4. Leverage Templates
# Create team-specific templates
switctl template create team-api \
--based-on=http-grpc \
--add-middleware=auth,logging,cors \
--add-monitoring=sentry
# Share templates
git commit -m "Add team API template"Troubleshooting
Common Issues
Command not found:
# Check PATH
echo $PATH | grep switctl
# Use full path temporarily
/path/to/swit/bin/switctl --helpTemplate errors:
# List available templates
switctl template list
# Debug template generation
switctl --debug new service test --template=basicGeneration failures:
# Enable verbose output
switctl --verbose generate api user
# Check project configuration
switctl config getDebug Mode
Use debug mode to troubleshoot issues:
# Enable debug output
switctl --debug new service debug-test
# This shows:
# - Template resolution
# - File generation steps
# - Configuration validation
# - Error detailsNext Steps
Now that you've created your first service with switctl:
- Commands Reference - Learn all available commands
- Template System - Master the template system
- Framework Guide - Understand the underlying framework
- Examples - Explore complete examples
Quick Reference
# Essential commands
switctl new service <name> # Create service
switctl generate api <name> # Generate API
switctl check --all # Quality checks
switctl dev watch # Development mode
# Configuration
switctl config set <key> <value> # Set config
switctl config get # View config
# Templates
switctl template list # List templates
switctl template create <name> # Create template
# Help
switctl --help # General help
switctl <command> --help # Command helpWelcome to efficient microservice development with switctl! 🚀