AgentSkillsCN

backend-api-debugging

Go 后端 API 端点调试与测试指南。适用于被要求排查 API 错误、测试端点,或调试认证问题时使用。

SKILL.md
--- frontmatter
name: backend-api-debugging
description: Guide for debugging and testing Go backend API endpoints. Use this when asked to troubleshoot API errors, test endpoints, or debug authentication issues.

Backend API Debugging

Starting the Backend Server

CRITICAL: Always run from ecommerce-backend/ directory:

powershell
cd ecommerce-backend

# Ensure database is running
docker compose ps  # Check if PostgreSQL is running

# Run migrations first (if not done)
make migrate-up

# Start server
make run
# OR
go run cmd/api/main.go

Server runs on: http://localhost:8080

Quick Health Checks

powershell
# 1. Health endpoint
curl http://localhost:8080/health

# 2. Check if server is listening
netstat -an | findstr "8080"

Testing API Endpoints

Register New User

powershell
curl -X POST http://localhost:8080/api/v1/auth/register `
  -H "Content-Type: application/json" `
  -d '{
    "email": "test@example.com",
    "password": "SecurePass123!",
    "full_name": "Test User"
  }'

Login

powershell
curl -X POST http://localhost:8080/api/v1/auth/login `
  -H "Content-Type: application/json" `
  -d '{
    "email": "test@example.com",
    "password": "SecurePass123!"
  }'

Test Protected Endpoint

powershell
# Save access token from login response
$token = "your_access_token_here"

curl http://localhost:8080/api/v1/auth/me `
  -H "Authorization: Bearer $token"

Common API Issues

"relation does not exist"

Cause: Database tables not created Solution:

powershell
cd ecommerce-backend
make migrate-up

"dial tcp: connection refused"

Cause: PostgreSQL not running or wrong DATABASE_URL Solution:

powershell
# Check PostgreSQL
docker compose ps  # If using Docker
# OR check Windows service

# Verify DATABASE_URL in .env
cat .env | Select-String "DATABASE_URL"

# Test connection
$env:Path += ";D:\Program Files\PostgreSQL\18\bin"
$env:PGPASSWORD = "postgres"
psql -U postgres -d ecommerce -c "\dt"

"invalid or expired JWT"

Cause: Token expired or invalid JWT_SECRET Solutions:

  1. Get new token by logging in again
  2. Check JWT_SECRET in .env matches what was used to create token
  3. Verify token format: Authorization: Bearer <token>

"password hash mismatch"

Cause: Password stored incorrectly or bcrypt issue Solution: Check password hashing in pkg/crypto/password.go

Port 8080 already in use

powershell
# Find process using port 8080
netstat -ano | findstr ":8080"

# Kill process (replace PID)
taskkill /PID <PID> /F

# Or change port in config

Debugging with Logs

The API uses structured logging. Check console output for:

  • Request IDs (for tracing)
  • Error messages with stack traces
  • Database query errors
  • Validation errors

Swagger Documentation

Access API docs at: http://localhost:8080/swagger/index.html

Regenerate if needed:

powershell
cd ecommerce-backend
make swagger
# OR
swag init -g cmd/api/main.go -o docs

Testing Suite

powershell
cd ecommerce-backend

# Run all tests
make test

# Run with coverage
make test-coverage

# Test specific package
go test -v ./internal/service/...

Database Inspection

powershell
# Set up PostgreSQL access
$env:Path += ";D:\Program Files\PostgreSQL\18\bin"
$env:PGPASSWORD = "postgres"

# Connect to database
psql -U postgres -d ecommerce

# Common queries
\dt                    # List tables
\d users              # Describe users table
SELECT * FROM users;  # View users
\q                    # Quit

Request Validation Issues

The API uses go-playground/validator. Common validation errors:

  • Email format: must be valid email
  • Password: minimum 8 characters
  • Required fields: cannot be empty

Check validation tags in internal/api/dto/ files.