Skip to content

Development Environment Setup

Prerequisites

Local Development Setup

1. Clone the Repository

git clone https://github.com/hopekali04/budget365API.git
cd budget365API

2. Install Dependencies

go mod download

3. Database Setup

# Start PostgreSQL container
docker run --name budget365-postgres \
  -e POSTGRES_DB=budget365 \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=password \
  -p 5432:5432 \
  -d postgres:15

Option B: Local PostgreSQL

  1. Install PostgreSQL on your system
  2. Create a database:
    CREATE DATABASE budget365;
    CREATE USER postgres WITH PASSWORD 'password';
    GRANT ALL PRIVILEGES ON DATABASE budget365 TO postgres;
    

4. Environment Configuration

Copy the example environment file and customize it:

cp .env .env.local

Edit .env.local with your specific configuration:

# Server Configuration
SERVER_ADDRESS=:8080

# Database Configuration (adjust as needed)
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=password
DATABASE_DBNAME=budget365
DATABASE_SSLMODE=disable

# Logging Configuration
LOGGING_LEVEL=debug
LOGGING_ENCODING=console  # Use 'console' for development, 'json' for production

5. Run Database Migrations

# Future: When migrations are implemented
# go run cmd/migrate/main.go up

Currently, GORM will auto-migrate tables when the application starts.

6. Start the Application

# Development mode
go run main.go

# Or build and run
go build -o budget365-api main.go
./budget365-api

The API will be available at http://localhost:8080

7. Verify Installation

Test the health endpoint:

curl http://localhost:8080/health

Expected response:

{
  "success": true,
  "message": "Service is healthy",
  "status": "ok",
  "timestamp": "2024-01-15T10:30:00Z",
  "version": "1.0.0"
}

Development Workflow

Code Structure

Follow the established patterns:

  1. Models - Define in /internal/models/
  2. Repositories - Interfaces in /internal/repositories/interfaces.go, implementations in separate files
  3. Services - Interfaces in /internal/services/interfaces.go, implementations in separate files
  4. API Handlers - In /internal/api/
  5. Routes - Configure in /internal/routes/

Running Tests

# Run all tests
go test ./...

# Run tests with coverage
go test -cover ./...

# Run tests for specific package
go test ./internal/services/

# Run integration tests (when available)
go test ./test/...

Code Quality

Linting

# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# Run linter
golangci-lint run

Formatting

# Format code
go fmt ./...

# Import organization
go mod tidy

Database Migrations

When implementing new models or changing existing ones:

  1. Update the model in /internal/models/
  2. GORM will auto-migrate during development
  3. For production, implement proper migrations in /cmd/migrate/

Adding New Features

  1. Define the model in /internal/models/
  2. Create repository interface in /internal/repositories/interfaces.go
  3. Implement repository in /internal/repositories/{feature}_repository.go
  4. Create service interface in /internal/services/interfaces.go
  5. Implement service in /internal/services/{feature}_service.go
  6. Create API handler in /internal/api/{feature}.go
  7. Add routes in /internal/routes/router.go
  8. Write tests for each layer

Environment Variables

Required Variables

Variable Description Default Example
DATABASE_HOST PostgreSQL host localhost localhost
DATABASE_PORT PostgreSQL port 5432 5432
DATABASE_USER Database user postgres postgres
DATABASE_PASSWORD Database password password mypassword
DATABASE_DBNAME Database name budget365 budget365

Optional Variables

Variable Description Default
SERVER_ADDRESS Server bind address :8080
LOGGING_LEVEL Log level info
LOGGING_ENCODING Log format json
AUTH_JWT_SECRET JWT secret key your-secret-key

Debugging

Logging

The application uses structured logging with Zap. Logs include: - Request/response details - Database operations - Error context - Request correlation IDs

Database Debugging

Enable GORM debug mode in development:

// In database/db.go, change logger mode
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
    Logger: logger.Default.LogMode(logger.Info), // Shows all SQL queries
})

API Testing

Use tools like: - curl - Command line testing - Postman - GUI API testing - httpie - User-friendly command line HTTP client

Example API calls:

# Create user
curl -X POST http://localhost:8080/api/v1/users \
  -H "Content-Type: application/json" \
  -d '{"name":"John Doe","email":"john@example.com","password":"password123"}'

# Get user
curl http://localhost:8080/api/v1/users/1

# List users
curl http://localhost:8080/api/v1/users?page=1&limit=10

Production Deployment

Build

# Build for Linux
GOOS=linux GOARCH=amd64 go build -o budget365-api main.go

# Build with version info
go build -ldflags "-X main.version=1.0.0" -o budget365-api main.go

Docker

Create a Dockerfile:

FROM golang:1.24-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o budget365-api main.go

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/budget365-api .
CMD ["./budget365-api"]

Environment

  • Use proper database connections
  • Set LOGGING_ENCODING=json for structured logs
  • Configure proper secrets for AUTH_JWT_SECRET
  • Set appropriate DATABASE_SSLMODE for security

Troubleshooting

Common Issues

  1. Database Connection Failed
  2. Check PostgreSQL is running
  3. Verify connection parameters
  4. Check firewall settings

  5. Port Already in Use

  6. Change SERVER_ADDRESS in environment
  7. Kill existing process: lsof -ti:8080 | xargs kill

  8. Import Errors

  9. Run go mod tidy
  10. Verify Go version compatibility

  11. Migration Issues

  12. Drop and recreate database for development
  13. Check model definitions for errors