Skip to content

project tree

Azizul Hakim edited this page Aug 26, 2025 · 3 revisions

Project Structure Guide

This document provides a comprehensive overview of the IMS PocketBase BaaS Starter project structure, explaining the purpose and organization of each directory and key files.

Directory Structure Overview

ims-pocketbase-baas-starter/
β”œβ”€β”€ πŸ“ cmd/                     # Application entry points
β”œβ”€β”€ πŸ“ docs/                    # Project documentation
β”œβ”€β”€ πŸ“ internal/                # Private application code
β”œβ”€β”€ πŸ“ monitoring/              # Monitoring configurations (Prometheus, Grafana)
β”œβ”€β”€ πŸ“ pb_data/                 # PocketBase data directory
β”œβ”€β”€ πŸ“ pb_public/               # PocketBase public assets
β”œβ”€β”€ πŸ“ pkg/                     # Reusable packages
β”œβ”€β”€ πŸ“ scripts/                 # Build and setup scripts
β”œβ”€β”€ πŸ“ templates/               # Template files (email templates, etc.)
β”œβ”€β”€ πŸ“ tmp/                     # Temporary files (git ignored)
β”œβ”€β”€ 🐳 Dockerfile               # Production container definition
β”œβ”€β”€ 🐳 docker-compose.yml       # Production container orchestration
β”œβ”€β”€ 🐹 go.mod                   # Go module definition
β”œβ”€β”€ βš™οΈ makefile                 # Development commands
└── πŸ“„ README.md                # Main project documentation

Detailed Directory Breakdown

πŸ“ cmd/ - Application Entry Points

Contains the main application executables following Go project layout standards.

cmd/
β”œβ”€β”€ migrate-gen/          # Migration CLI generator
β”‚   β”œβ”€β”€ main.go          # CLI entry point
β”‚   β”œβ”€β”€ cli.go           # Command-line interface logic
β”‚   β”œβ”€β”€ cli_test.go      # CLI tests
β”‚   β”œβ”€β”€ template.go      # Migration template generation
β”‚   β”œβ”€β”€ template_test.go # Template generation tests
β”‚   └── types.go         # CLI-specific types
└── server/              # Main application server
    └── main.go          # Server entry point

Purpose: Separates different executable commands, making the project modular and following Go conventions.

πŸ“ internal/ - Private Application Code

Contains application-specific code that should not be imported by other projects.

internal/
β”œβ”€β”€ app/                 # Application setup and configuration
β”‚   β”œβ”€β”€ app.go          # Main app initialization and DI orchestration
β”‚   └── app_test.go     # Application setup tests
β”œβ”€β”€ commands/           # CLI commands registration
β”‚   └── commands.go     # CLI command registration and configuration
β”œβ”€β”€ crons/              # Cron job definitions
β”‚   └── crons.go        # Cron job registration and configuration
β”œβ”€β”€ database/           # Database-related code
β”‚   β”œβ”€β”€ migrations/     # Database schema migrations
β”‚   β”œβ”€β”€ schema/         # PocketBase schema JSON files
β”‚   └── seeders/        # Data seeding utilities
β”œβ”€β”€ handlers/           # Business logic handlers
β”‚   β”œβ”€β”€ cron/          # Cron job handlers
β”‚   β”œβ”€β”€ export/        # Data export handlers
β”‚   β”œβ”€β”€ hook/          # Event hook handlers
β”‚   β”œβ”€β”€ jobs/          # Job queue handlers
β”‚   └── route/         # Custom route handlers
β”œβ”€β”€ hooks/             # Event hook registration
β”‚   β”œβ”€β”€ hooks.go       # Hook registration orchestration
β”‚   └── hooks_test.go  # Hook system tests
β”œβ”€β”€ jobs/              # Job management
β”‚   β”œβ”€β”€ jobs.go        # Job handler registration (new pattern)
β”‚   └── manager.go     # Job manager singleton
β”œβ”€β”€ middlewares/       # HTTP middlewares
β”‚   β”œβ”€β”€ middlewares.go # Middleware registration (new pattern)
β”‚   β”œβ”€β”€ auth.go        # Authentication middleware
β”‚   β”œβ”€β”€ metrics.go     # Metrics collection middleware
β”‚   └── permission.go  # Permission-based access control
β”œβ”€β”€ routes/            # Custom API routes
β”‚   └── routes.go      # Route registration (new pattern)
└── apidoc/           # API documentation generation
    β”œβ”€β”€ generator.go   # OpenAPI spec generation
    β”œβ”€β”€ discovery.go   # Collection discovery
    β”œβ”€β”€ schema.go      # Schema generation
    └── endpoints.go   # API docs endpoints

πŸ“ pkg/ - Reusable Packages

Contains reusable packages that could potentially be imported by other projects.

pkg/
β”œβ”€β”€ cache/             # Caching system
β”‚   β”œβ”€β”€ cache.go      # Cache service with TTL support
β”‚   └── cache_test.go # Cache system tests
β”œβ”€β”€ common/            # Common utilities
β”‚   β”œβ”€β”€ env.go        # Environment variable utilities
β”‚   β”œβ”€β”€ response.go   # HTTP response utilities
β”‚   └── route.go      # Route configuration
β”œβ”€β”€ cronutils/         # Cron execution utilities
β”‚   β”œβ”€β”€ utils.go      # Cron validation and execution context
β”‚   └── utils_test.go # Cron utilities tests
β”œβ”€β”€ jobutils/          # Job processing utilities
β”‚   β”œβ”€β”€ processor.go  # Job processor implementation
β”‚   β”œβ”€β”€ types.go      # Job-related types and interfaces
β”‚   β”œβ”€β”€ payload.go    # Job payload parsing utilities
β”‚   β”œβ”€β”€ file.go       # File handling for jobs
β”‚   └── worker_pool.go # Concurrent job processing
β”œβ”€β”€ logger/            # Centralized logging system
β”‚   β”œβ”€β”€ logger.go     # Logger singleton implementation
β”‚   β”œβ”€β”€ utils.go      # Logger utilities
β”‚   └── logger_test.go # Logger tests
β”œβ”€β”€ metrics/           # Metrics and observability
β”‚   β”œβ”€β”€ metrics.go    # Main metrics interface and factory
β”‚   β”œβ”€β”€ config.go     # Configuration management
β”‚   β”œβ”€β”€ prometheus.go # Prometheus implementation
β”‚   β”œβ”€β”€ opentelemetry.go # OpenTelemetry implementation
β”‚   β”œβ”€β”€ noop.go       # No-op implementation
β”‚   β”œβ”€β”€ instrumentation.go # Helper functions
β”‚   β”œβ”€β”€ types.go      # Metric types and constants
β”‚   └── *_test.go     # Comprehensive test suite
β”œβ”€β”€ migration/         # Migration utilities
β”‚   β”œβ”€β”€ scanner.go    # Migration file scanning
β”‚   β”œβ”€β”€ filesystem.go # File system operations
β”‚   └── *_test.go     # Migration tests
β”œβ”€β”€ permission/        # Permission system
β”‚   β”œβ”€β”€ permissions.go # Permission constants and definitions
β”‚   └── permissions_test.go # Permission tests
└── response/          # HTTP response utilities
    β”œβ”€β”€ response.go   # Standardized HTTP response helpers
    └── response_test.go # Response utility tests

πŸ“Š monitoring/ - Monitoring Configurations

Contains monitoring infrastructure configurations for both development and production environments.

monitoring/
β”œβ”€β”€ local/             # Development monitoring setup
β”‚   β”œβ”€β”€ grafana/       # Grafana configuration
β”‚   β”‚   β”œβ”€β”€ dashboards/    # Pre-built dashboards
β”‚   β”‚   └── provisioning/ # Grafana provisioning config
β”‚   └── prometheus/    # Prometheus configuration
β”‚       └── prometheus.yml # Local metrics scraping
└── production/        # Production monitoring setup
    β”œβ”€β”€ grafana/       # Production Grafana config
    β”œβ”€β”€ prometheus/    # Production Prometheus config
    └── alertmanager/  # Alert management configuration

πŸ“ docs/ - Project Documentation

Comprehensive documentation for all aspects of the project.

docs/
β”œβ”€β”€ README.md              # Documentation index
β”œβ”€β”€ dependency-injection.md # DI patterns and strategies
β”œβ”€β”€ cron-jobs.md          # Job queue and cron system
β”œβ”€β”€ hooks.md              # Event hooks system
β”œβ”€β”€ logger.md             # Logging system
β”œβ”€β”€ middleware.md         # Custom middleware
β”œβ”€β”€ migrations.md         # Database migrations
β”œβ”€β”€ apidoc.md            # API documentation
β”œβ”€β”€ cli-commands.md       # CLI commands and usage
β”œβ”€β”€ git-hooks.md          # Git hooks setup
└── project-tree.md       # This file - project structure

πŸ“ templates/ - Template Files

Contains template files used by the application.

templates/
└── emails/              # Email templates
    β”œβ”€β”€ welcome.html    # HTML welcome email template
    └── welcome.txt     # Plain text welcome email template

Purpose: Stores template files that can be used by the application for generating dynamic content, such as emails or documents.

Key Design Principles

1. Clean Architecture

  • Clear separation between internal/ (private) and pkg/ (public) code
  • Layered architecture with proper dependency flow
  • Domain-driven design with focused packages

2. Dependency Injection Patterns

  • Singleton Pattern: Shared services (pkg/metrics, pkg/logger, pkg/cache)
  • Constructor Injection: Business logic components (internal/handlers)
  • Factory Pattern: Configurable implementations (pkg/metrics/config.go)
  • Interface-Based: Loose coupling throughout the application

3. Testing Strategy

  • Co-located test files following Go conventions (*_test.go)
  • Comprehensive test coverage for all packages
  • Mock-friendly architecture with interface-based design
  • Singleton reset functionality for isolated testing

4. Configuration Management

  • Environment-driven configuration (.env files)
  • Centralized environment utilities (pkg/common/env.go)
  • Sensible defaults with override capabilities
  • Production and development configurations

File Naming Conventions

Go Files

  • *.go - Implementation files
  • *_test.go - Test files (co-located with implementation)
  • types.go - Type definitions and constants
  • config.go - Configuration structures and loading

Documentation Files

  • README.md - Main documentation or directory index
  • *.md - Specific topic documentation
  • CONTRIBUTING.md - Contribution guidelines
  • LICENSE.md - License information

Configuration Files

  • .env - Environment variables (not in version control)
  • .env.example - Environment template
  • .env.production - Production environment template
  • docker-compose.yml - Production container configuration
  • docker-compose.dev.yml - Development container configuration
  • Dockerfile - Production container definition
  • makefile - Development commands

Temporary Files

  • tmp/ - Temporary files and directories (git ignored)
  • tmp/ - Temporary files directory (git ignored)

Monitoring Files

  • monitoring/local/ - Development monitoring setup
  • monitoring/production/ - Production monitoring deployment
  • monitoring/*/prometheus/ - Prometheus configurations
  • monitoring/*/grafana/ - Grafana dashboards and provisioning

Package Dependencies

Dependency Flow

internal/app/ (orchestrates everything)
    ↓
internal/middlewares/ (HTTP layer)
    ↓
internal/handlers/ (business logic)
    ↓
pkg/ (utilities and services)

Key Relationships

  • internal/app/app.go orchestrates all dependency injection
  • pkg/metrics/ provides observability across all layers
  • pkg/logger/ provides logging across all components
  • pkg/cache/ provides caching for performance optimization
  • internal/middlewares/ provides cross-cutting concerns
  • internal/handlers/ implements business logic

Development Workflow Integration

Hot Reload Support

  • .air.toml - Air configuration for hot reload
  • docker-compose.dev.yml - Development environment with volume mounts
  • Dockerfile.dev - Development container with Air

Code Quality

  • makefile - Standardized development commands
  • .githooks/ - Pre-commit and pre-push validation
  • scripts/ - Setup and utility scripts

Testing Infrastructure

  • Comprehensive test coverage across all packages
  • Benchmark tests for performance-critical components
  • Integration tests for complex workflows
  • Mock-friendly architecture for isolated testing

Production Deployment

Container Strategy

  • Multi-stage Docker builds for optimized production images
  • Separate development and production configurations
  • Volume mounts for persistent data (pb_data/)
  • Health checks and monitoring endpoints

Monitoring and Observability

  • Local Development: monitoring/local/ - Prometheus + Grafana for development
  • Production Deployment: monitoring/production/ - Scalable monitoring infrastructure
  • Metrics Collection: Prometheus scraping with configurable intervals
  • Visualization: Grafana dashboards with pre-built PocketBase metrics
  • Alerting: Alertmanager integration for production notifications
  • Structured Logging: Multiple log levels with centralized collection

Best Practices Demonstrated

  1. Go Project Layout: Follows standard Go project structure
  2. Separation of Concerns: Clear boundaries between layers
  3. Dependency Injection: Multiple strategies for different use cases
  4. Testing: Comprehensive test coverage with proper isolation
  5. Documentation: Extensive documentation for all components
  6. Configuration: Environment-driven with sensible defaults
  7. Containerization: Production-ready Docker setup
  8. Code Quality: Linting, formatting, and Git hooks

Navigation Tips

  • Start with README.md for project overview
  • Explore internal/app/app.go to understand application initialization
  • Review pkg/ packages to understand core utilities
  • Check docs/ for detailed guides on specific topics
  • Use makefile commands for development workflow
  • Refer to .env.example for configuration options

πŸ“š Documentation Structure

Home

  • Home - IMS PocketBase BaaS Starter Documentation

πŸš€ Getting Started

⚑ Core Features

πŸ“Š Performance & Monitoring

πŸ› οΈ Documentation & Development

πŸ—οΈ Advanced Topics

πŸ”— Quick Links

🀝 Community

Clone this wiki locally