194 lines
6.9 KiB
Makefile
194 lines
6.9 KiB
Makefile
# ULFlow Golang Starter Kit Makefile
|
|
# Provides common commands for development, testing, and deployment
|
|
|
|
# Load environment variables from .env file
|
|
ifneq (,$(wildcard ./.env))
|
|
include .env
|
|
export
|
|
endif
|
|
|
|
.PHONY: help init dev test lint build clean docker-build docker-run docker-clean docker-prune docker-compose-up docker-compose-down docker-compose-prod-up docker-compose-prod-down ci setup-git all
|
|
|
|
# Default target executed when no arguments are given to make.
|
|
default: help
|
|
|
|
# Show help message for all Makefile commands
|
|
help:
|
|
@echo "ULFlow Golang Starter Kit"
|
|
@echo ""
|
|
@echo "Usage:"
|
|
@echo " make <target>"
|
|
@echo ""
|
|
@echo "Targets:"
|
|
@echo " init - Initialize project dependencies and tools"
|
|
@echo " dev - Start development server with hot reload (Air Tomb)"
|
|
@echo " test - Run all tests"
|
|
@echo " lint - Run linters and code quality tools"
|
|
@echo " build - Build the application binary"
|
|
@echo " clean - Clean temporary files, build artifacts, and cache"
|
|
@echo " docker-build - Build Docker image"
|
|
@echo " docker-run - Run application in Docker container"
|
|
@echo " docker-clean - Remove project Docker containers"
|
|
@echo " docker-prune - Remove all unused Docker resources"
|
|
@echo " docker-compose-up - Start all services with Docker Compose for local development"
|
|
@echo " docker-compose-down - Stop all services started with Docker Compose"
|
|
@echo " docker-compose-prod-up - Start all services with Docker Compose for production"
|
|
@echo " docker-compose-prod-down - Stop all production services"
|
|
@echo " ci - Run CI workflow locally"
|
|
@echo " setup-git - Configure Git with commit message template and hooks"
|
|
@echo " all - Run lint, test, and build"
|
|
|
|
# Initialize project dependencies and tools
|
|
init:
|
|
@echo "Installing project dependencies and tools..."
|
|
go mod tidy
|
|
go install github.com/air-verse/air@latest
|
|
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
|
@echo "Creating necessary directories..."
|
|
if not exist tmp mkdir tmp
|
|
if not exist logs mkdir logs
|
|
|
|
@echo "Copying .env file if not exists..."
|
|
@if not exist ".env" (if exist "templates\.env.example" (copy "templates\.env.example" ".env" & echo Created .env file from example) else (echo Warning: Could not create .env file)) else (echo .env file already exists, skipping)
|
|
|
|
@echo "Initialization complete!"
|
|
|
|
# Start development server with hot reload
|
|
dev:
|
|
@echo "Starting development server with Air Tomb..."
|
|
air
|
|
|
|
# Run all tests
|
|
test:
|
|
@echo "Running tests..."
|
|
go test -v -cover ./...
|
|
|
|
# Run linters and code quality tools
|
|
lint:
|
|
@echo "Running linters..."
|
|
golangci-lint run ./...
|
|
|
|
# Build the application binary
|
|
build:
|
|
@echo "Building application..."
|
|
go build -o bin/app cmd/app/main.go
|
|
|
|
# Clean temporary files, build artifacts, and cache
|
|
clean:
|
|
@echo "Cleaning project..."
|
|
if exist tmp rmdir /s /q tmp
|
|
if exist bin rmdir /s /q bin
|
|
if exist logs rmdir /s /q logs
|
|
go clean -cache -testcache -modcache
|
|
@echo "Project cleaned!"
|
|
|
|
# Build Docker image
|
|
docker-build:
|
|
@echo "Building Docker image..."
|
|
docker build -t ulflow-zee:latest .
|
|
|
|
# Run application in Docker container
|
|
docker-run:
|
|
@echo "Running application in Docker container..."
|
|
@if not exist .env (
|
|
@echo "Warning: .env file not found. Running with default environment variables..."
|
|
docker run -p 3000:3000 ulflow-zee:latest
|
|
) else (
|
|
docker run -p 3000:3000 --env-file .env ulflow-zee:latest
|
|
)
|
|
|
|
# Run Docker Compose for local development
|
|
docker-compose-up:
|
|
@echo "Starting all services with Docker Compose for local development..."
|
|
docker-compose up -d
|
|
@echo "Services started! API is available at http://localhost:3000"
|
|
|
|
# Stop Docker Compose services for local development
|
|
docker-compose-down:
|
|
@echo "Stopping all development services..."
|
|
docker-compose down -v
|
|
|
|
# Run Docker Compose for production
|
|
docker-compose-prod-up:
|
|
@echo "Starting all services with Docker Compose for production..."
|
|
docker-compose -f docker-compose.prod.yml up -d
|
|
@echo "Production services started! API is available at http://localhost:3000"
|
|
|
|
# Stop Docker Compose services for production
|
|
docker-compose-prod-down:
|
|
@echo "Stopping all production services..."
|
|
docker-compose -f docker-compose.prod.yml down
|
|
|
|
# Setup Git configuration
|
|
setup-git-hooks:
|
|
@echo "Setting up Git hooks..."
|
|
git config --local commit.template .gitea/commit-template.txt
|
|
|
|
@REM Kiểm tra và tạo thư mục .git/hooks nếu chưa tồn tại
|
|
@if not exist .git\hooks (mkdir .git\hooks)
|
|
|
|
@REM Sao chép các Git hooks
|
|
@echo Copying pre-commit hook...
|
|
@copy /Y .gitea\hooks\pre-commit .git\hooks\pre-commit >nul || (echo Warning: Could not copy pre-commit hook & exit /b 1)
|
|
@echo Copying prepare-commit-msg hook...
|
|
@copy /Y .gitea\hooks\prepare-commit-msg .git\hooks\prepare-commit-msg >nul || (echo Warning: Could not copy prepare-commit-msg hook & exit /b 1)
|
|
|
|
git config --local core.hooksPath .git/hooks
|
|
@echo "Git setup complete!"
|
|
|
|
# Create git message template
|
|
setup-git-message:
|
|
@echo "Creating Git commit message template..."
|
|
git config --local commit.template .gitea/commit-template.txt
|
|
@echo "Git commit message template created!"
|
|
|
|
|
|
# Prune all unused Docker resources
|
|
docker-prune:
|
|
@echo "Pruning unused Docker resources..."
|
|
docker system prune -af --volumes
|
|
@echo "Docker resources pruned!"
|
|
|
|
# Run local CI simulation
|
|
ci:
|
|
@echo "Running CI workflow locally..."
|
|
make lint
|
|
make test
|
|
make build
|
|
make docker-build
|
|
@echo "CI simulation completed!"
|
|
|
|
# Create a new migration
|
|
migrate-create:
|
|
@read -p "Enter migration name: " name; \
|
|
migrate create -ext sql -dir migrations -seq $$name
|
|
|
|
# Run migrations up
|
|
m-up:
|
|
@echo "Running migrations with user: $(DATABASE_USERNAME)"
|
|
@echo "Database: $(DATABASE_NAME) on $(DATABASE_HOST):$(DATABASE_PORT)"
|
|
@echo "Using connection string: postgres://$(DATABASE_USERNAME):*****@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable"
|
|
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" up
|
|
|
|
# Run migrations down
|
|
m-down:
|
|
@echo "Reverting migrations..."
|
|
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" down
|
|
|
|
# Reset database (drop all tables and re-run migrations)
|
|
m-reset: m-down m-up
|
|
@echo "Database reset complete!"
|
|
|
|
# Show migration status
|
|
m-status:
|
|
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" version
|
|
|
|
# Force migration to specific version (fix dirty state)
|
|
m-force:
|
|
@echo "Forcing migration to version $(version)..."
|
|
@migrate -path migrations -database "postgres://$(DATABASE_USERNAME):$(DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/$(DATABASE_NAME)?sslmode=disable" force $(version)
|
|
|
|
# Run application (default: without hot reload)
|
|
run:
|
|
go run ./cmd/app/main.go
|