feat: init project

This commit is contained in:
yanghao05
2025-08-05 17:26:59 +08:00
parent c5d621ad03
commit e034a2e54e
30 changed files with 5159 additions and 0 deletions

240
Makefile Normal file
View File

@@ -0,0 +1,240 @@
# Database Render Application Makefile
# Variables
BINARY_NAME=database-render
APP_NAME=database-render
VERSION?=latest
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)"
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
GOFMT=gofmt
GOFMT_CHECK=$(GOFMT) -l
GOFMT_FMT=$(GOFMT) -w
# Directories
BIN_DIR=bin
DIST_DIR=dist
CONFIG_DIR=config
WEB_DIR=web
TEMPLATES_DIR=web/templates
STATIC_DIR=web/static
# Default target
.PHONY: all
all: clean build
# Build the application
.PHONY: build
build: deps
@echo "Building $(BINARY_NAME)..."
@mkdir -p $(BIN_DIR)
CGO_ENABLED=1 $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME) ./cmd/server
@echo "Build completed: $(BIN_DIR)/$(BINARY_NAME)"
# Build for multiple platforms
.PHONY: build-all
build-all: clean deps
@echo "Building for multiple platforms..."
@mkdir -p $(DIST_DIR)
# Linux AMD64 with CGO
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-linux-amd64 ./cmd/server
# Linux ARM64 with CGO
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-linux-arm64 ./cmd/server
# macOS AMD64 with CGO
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-amd64 ./cmd/server
# macOS ARM64 with CGO
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-darwin-arm64 ./cmd/server
# Windows AMD64 with CGO (requires mingw-w64)
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc $(GOBUILD) $(LDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe ./cmd/server
@echo "Multi-platform build completed in $(DIST_DIR)/"
# Install dependencies
.PHONY: deps
deps:
@echo "Installing dependencies..."
$(GOMOD) tidy
$(GOMOD) download
# Clean build artifacts
.PHONY: clean
clean:
@echo "Cleaning..."
$(GOCLEAN)
rm -rf $(BIN_DIR)
rm -rf $(DIST_DIR)
@echo "Clean completed"
# Run tests
.PHONY: test
test:
@echo "Running tests..."
$(GOTEST) -v ./...
# Run tests with coverage
.PHONY: test-coverage
test-coverage:
@echo "Running tests with coverage..."
$(GOTEST) -v -coverprofile=coverage.out ./...
$(GOCMD) tool cover -html=coverage.out -o coverage.html
@echo "Coverage report generated: coverage.html"
# Format code
.PHONY: fmt
fmt:
@echo "Formatting code..."
$(GOFMT_FMT) .
# Check code formatting
.PHONY: fmt-check
fmt-check:
@echo "Checking code formatting..."
@if [ -n "$$($(GOFMT_CHECK) .)" ]; then \
echo "Code is not formatted. Please run 'make fmt' to format the code."; \
exit 1; \
fi
@echo "Code formatting is correct"
# Lint code
.PHONY: lint
lint:
@echo "Running linter..."
@if command -v golangci-lint >/dev/null 2>&1; then \
golangci-lint run; \
else \
echo "golangci-lint not found. Please install it: https://golangci-lint.run/"; \
exit 1; \
fi
# Security scan
.PHONY: security
security:
@echo "Running security scan..."
@if command -v gosec >/dev/null 2>&1; then \
gosec ./...; \
else \
echo "gosec not found. Please install it: go install github.com/securecodewarrior/gosec/v2/cmd/gosec@latest"; \
exit 1; \
fi
# Run the application
.PHONY: run
run: build
@echo "Starting $(BINARY_NAME)..."
./$(BIN_DIR)/$(BINARY_NAME)
# Run with development configuration
.PHONY: dev
dev: build
@echo "Starting $(BINARY_NAME) in development mode..."
CONFIG_FILE=config/config.yaml ./$(BIN_DIR)/$(BINARY_NAME)
# Create sample configuration
.PHONY: init-config
init-config:
@echo "Creating sample configuration..."
@mkdir -p $(CONFIG_DIR)
@if [ ! -f $(CONFIG_DIR)/config.yaml ]; then \
cp config/config.example.yaml $(CONFIG_DIR)/config.yaml; \
echo "Sample configuration created at $(CONFIG_DIR)/config.yaml"; \
else \
echo "Configuration file already exists at $(CONFIG_DIR)/config.yaml"; \
fi
# Generate database schema example
.PHONY: init-db
init-db:
@echo "Creating sample database schema..."
@mkdir -p $(CONFIG_DIR)
@if [ ! -f $(CONFIG_DIR)/schema.sql ]; then \
cp config/schema.example.sql $(CONFIG_DIR)/schema.sql; \
echo "Sample database schema created at $(CONFIG_DIR)/schema.sql"; \
else \
echo "Database schema file already exists at $(CONFIG_DIR)/schema.sql"; \
fi
# Initialize project
.PHONY: init
init: init-config init-db deps
@echo "Project initialized successfully!"
@echo "Please edit $(CONFIG_DIR)/config.yaml to configure your database connection"
@echo "Then run: make run"
# Docker commands
.PHONY: docker-build
docker-build:
@echo "Building Docker image..."
docker build -t $(APP_NAME):$(VERSION) .
.PHONY: docker-run
docker-run:
@echo "Running Docker container..."
docker run -p 8080:8080 -v $(PWD)/config:/app/config $(APP_NAME):$(VERSION)
.PHONY: docker-compose-up
docker-compose-up:
@echo "Starting with Docker Compose..."
docker-compose up --build
.PHONY: docker-compose-down
docker-compose-down:
@echo "Stopping Docker Compose..."
docker-compose down
# Development helpers
.PHONY: watch
watch:
@echo "Watching for changes..."
@if command -v air >/dev/null 2>&1; then \
air; \
else \
echo "air not found. Please install it: go install github.com/cosmtrek/air@latest"; \
exit 1; \
fi
# Check system health
.PHONY: health
health:
@echo "Checking system health..."
@curl -f http://localhost:8080/health || echo "Server not running"
# Show help
.PHONY: help
help:
@echo "Database Render Application - Available Commands:"
@echo ""
@echo "Development:"
@echo " make init - Initialize project with sample config and database"
@echo " make run - Build and run the application"
@echo " make dev - Run in development mode"
@echo " make watch - Run with hot reload (requires air)"
@echo ""
@echo "Building:"
@echo " make build - Build the application"
@echo " make build-all - Build for multiple platforms"
@echo " make clean - Clean build artifacts"
@echo ""
@echo "Quality:"
@echo " make test - Run tests"
@echo " make test-coverage - Run tests with coverage"
@echo " make fmt - Format code"
@echo " make fmt-check - Check code formatting"
@echo " make lint - Run linter"
@echo " make security - Run security scan"
@echo ""
@echo "Docker:"
@echo " make docker-build - Build Docker image"
@echo " make docker-run - Run Docker container"
@echo " make docker-compose-up - Start with Docker Compose"
@echo " make docker-compose-down - Stop Docker Compose"
@echo ""
@echo "Utilities:"
@echo " make health - Check application health"
@echo " make help - Show this help message"