Troubleshooting โ
Common issues and their solutions when working with goserve.
Installation Issues โ
Port Already in Use โ
Problem: Docker compose fails with "port is already allocated" error.
Error starting userland proxy: listen tcp4 0.0.0.0:8080: bind: address already in useSolutions:
- Find what's using the port:
# macOS/Linux
lsof -i :8080
# Windows
netstat -ano | findstr :8080- Stop the conflicting process or change the port:
# docker-compose.yml
services:
app:
ports:
- "8081:8080" # Changed from 8080:8080- Stop all containers and restart:
docker compose down
docker compose up --build -dDocker Daemon Not Running โ
Problem: Cannot connect to the Docker daemon
Solutions:
- Start Docker Desktop (macOS/Windows)
- Start Docker service (Linux):
sudo systemctl start docker- Verify Docker is running:
docker psDatabase Issues โ
PostgreSQL Connection Refused โ
Problem: Application can't connect to PostgreSQL.
error connecting to database: dial tcp 127.0.0.1:5432: connect: connection refusedSolutions:
- Check container is running:
docker ps | grep goserver-postgres- Check container health:
docker compose ps
docker compose logs goserver-postgres- Verify environment variables:
# Check .env file has correct database URL
cat .env | grep DATABASE_URL- Test direct connection:
docker exec -it goserver-postgres psql -U goserve -d goserve_dbMongoDB Connection Issues โ
Problem: Application can't reach MongoDB.
Solutions:
- Check container status:
docker ps | grep goserver-mongo
docker compose logs goserver-mongo- Test MongoDB health:
docker exec -it goserver-mongo mongosh --eval "db.adminCommand('ping')"- Verify connection string in .env:
DATABASE_URL=mongodb://goserve:changeit@goserver-mongo:27017/goserve_db?authSource=goserve_dbDatabase Not Initialized โ
Problem: Tables/collections don't exist after first run.
PostgreSQL Solution:
# Recreate database with fresh migration
docker compose down -v
docker compose up --build -dMongoDB Solution:
# MongoDB creates collections automatically
# Check if indexes are created:
docker exec -it goserver-mongo mongosh goserve_db --eval "db.getCollectionNames()"Authentication Issues โ
JWT Keys Not Found โ
Problem: Application crashes with "unable to load keys" or "no such file or directory".
panic: unable to load keys from /app/keys: open /app/keys/private.pem: no such file or directorySolutions:
- Generate RSA keys:
go run .tools/rsa/keygen.go- Verify keys exist:
ls -la keys/
# Should show: private.pem, public.pem- Check Docker volume mount:
# docker-compose.yml
volumes:
- ./keys:/app/keys # Ensure this line exists- Rebuild containers:
docker compose up --build -dInvalid Token Errors โ
Problem: API returns 401 Unauthorized with valid-looking token.
Solutions:
- Check token expiration:
// Tokens expire based on .env settings
ACCESS_TOKEN_VALIDITY_SEC=3600 # 1 hour
REFRESH_TOKEN_VALIDITY_SEC=604800 # 7 days- Verify key pairs match:
# Regenerate both keys together
rm keys/*
go run .tools/rsa/keygen.go
docker compose restart- Check clock synchronization:
# System time must be accurate
date
# If off, sync your system clockAPI Key Authentication Failing โ
Problem: Request with x-api-key header returns 401.
Solutions:
- Verify API key format in database:
-- PostgreSQL
SELECT * FROM api_keys WHERE status = true;
-- MongoDB
db.api_keys.find({ status: true })- Check header name:
# Correct header:
curl -H "x-api-key: your-key-here" http://localhost:8080/endpoint- Verify middleware is registered:
// Should be in route setup
router.Use(middleware.ApiKeyValidator())Build & Compilation Issues โ
Go Module Errors โ
Problem: go: module requires Go 1.21 or later
Solutions:
- Check Go version:
go version
# Should be 1.21 or higherUpdate Go: Download from golang.org/dl
Clean module cache:
go clean -modcache
go mod downloadDependency Download Failures โ
Problem: go get fails or times out.
Solutions:
- Use Go proxy:
export GOPROXY=https://proxy.golang.org,direct
go mod download- Clear and rebuild:
go clean -modcache
rm go.sum
go mod tidyMicroservices-Specific Issues โ
Kong Gateway Not Starting โ
Problem: Kong container exits or shows errors.
Solutions:
- Check Kong logs:
docker compose logs kong- Verify database migrations:
docker compose run --rm kong kong migrations bootstrap
docker compose up kong- Check port conflicts:
# Kong uses ports 8000 (HTTP) and 8001 (Admin)
lsof -i :8000
lsof -i :8001NATS Connection Issues โ
Problem: Services can't communicate via NATS.
Solutions:
- Check NATS is running:
docker compose logs nats
docker compose ps | grep nats- Verify NATS URL in .env:
NATS_URL=nats://nats:4222- Test NATS connectivity:
docker exec -it <service-container> nc -zv nats 4222Service Discovery Problems โ
Problem: Services can't find each other.
Solutions:
- Check Docker network:
docker network ls
docker network inspect <network-name>- Verify service names in environment:
# Services use container names for DNS
AUTH_SERVICE_URL=http://goserver-auth:8080
BLOG_SERVICE_URL=http://goserver-blog:8080- Test inter-service connectivity:
docker exec -it goserver-blog ping goserver-authRedis Issues โ
Redis Connection Timeout โ
Problem: Application can't connect to Redis.
Solutions:
- Check Redis container:
docker compose logs redis
docker compose ps | grep redis- Test Redis connection:
docker exec -it <redis-container> redis-cli ping
# Should return: PONG- Verify Redis URL:
REDIS_URL=redis:6379Cache Not Working โ
Problem: Data not being cached or stale data served.
Solutions:
- Clear Redis cache:
docker exec -it <redis-container> redis-cli FLUSHALL- Check cache keys:
docker exec -it <redis-container> redis-cli KEYS '*'- Verify cache TTL:
// Check cache expiration settings
cacheClient.Set(ctx, key, value, 10*time.Minute)Testing Issues โ
Integration Tests Failing โ
Problem: Tests fail with database connection errors.
Solutions:
- Ensure test database is running:
docker compose ps- Check test environment variables:
# Tests may use different env file
cat .env.test- Run tests with verbose output:
go test -v ./...Test Database Pollution โ
Problem: Tests fail due to existing data.
Solutions:
- Use transaction rollback:
// In test setup
tx := db.Begin()
defer tx.Rollback()- Clean database between tests:
func (s *TestSuite) TearDownTest() {
s.DB.Exec("TRUNCATE TABLE users CASCADE")
}Performance Issues โ
Slow API Responses โ
Solutions:
- Check database indexes:
-- PostgreSQL
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@example.com';
-- Look for "Seq Scan" - add index if found
CREATE INDEX idx_users_email ON users(email);- Monitor Redis cache hits:
docker exec -it <redis-container> redis-cli INFO stats | grep hit- Enable query logging:
// In database config
db.Logger = logger.Default.LogMode(logger.Info)High Memory Usage โ
Solutions:
- Check connection pool settings:
// Adjust in database config
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(5)
db.SetConnMaxLifetime(5 * time.Minute)- Monitor container resources:
docker statsEnvironment Configuration โ
Missing Environment Variables โ
Problem: Application crashes with "ENV variable not set".
Solutions:
- Copy example environment file:
go run .tools/copy/envs.go
# Or manually:
cp .env.example .env- Verify all required variables:
# Check which variables are missing
cat .env.example
diff .env.example .env- Load environment in development:
# Using godotenv
export $(cat .env | xargs)
go run main.goDevelopment Workflow Issues โ
Hot Reload Not Working โ
Problem: Changes don't reflect without manual restart.
Solutions:
- Verify volume mounts:
# docker-compose.yml
volumes:
- .:/app- Check file watcher:
# Install air for hot reload
go install github.com/cosmtrek/air@latest
airVS Code Debugger Not Attaching โ
Problem: Breakpoints not hitting in VS Code.
Solutions:
- Check launch.json configuration:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}"
}
]
}- Ensure Delve is installed:
go install github.com/go-delve/delve/cmd/dlv@latestGetting More Help โ
If these solutions don't resolve your issue:
Check GitHub Issues: github.com/afteracademy/goserve/issues
Search Discussions: github.com/afteracademy/goserve/discussions
Ask the Community: Post in GitHub Discussions with:
- Full error message
- Steps to reproduce
- Environment details (OS, Go version, Docker version)
- Relevant logs (
docker compose logs)
Watch Tutorials: YouTube Channel
Preventive Measures โ
Before Starting Development โ
- โ Ensure Docker Desktop is running
- โ
Verify Go 1.21+ is installed:
go version - โ
Generate RSA keys:
go run .tools/rsa/keygen.go - โ
Copy environment file:
go run .tools/copy/envs.go - โ
Check port availability:
lsof -i :8080
Regular Maintenance โ
- ๐ Update dependencies:
go get -u ./... - ๐งน Clean Docker:
docker system prune -a - ๐ฆ Update goserve: Check latest releases
- ๐ Rotate JWT keys in production environments
Best Practices โ
- ๐ Use
.envfiles for configuration (never commit!) - ๐งช Write tests for critical paths
- ๐ Monitor container logs:
docker compose logs -f - ๐ Use
EXPLAINfor slow queries - ๐พ Backup databases before major changes
