# arp242/goatcounter architecture

> Architecture diagram of the GitHub repository arp242/goatcounter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/arp242/goatcounter
- Repository: https://github.com/arp242/goatcounter

## Overview

##### Main Components

a. Core Components:
- Web Server (HTTP/HTTPS endpoints)
- Database Layer (supports both SQLite and PostgreSQL)
- Analytics Processing Engine
- Data Collection Endpoints
- User Interface/Dashboard
- Background Jobs/Cron System

b. External Integrations:
- ACME/Let's Encrypt for SSL
- Client-side JavaScript Counter
- Log File Import System
- API Services

##### Key Relationships to Highlight

- Data Flow: Client → Collection Endpoints → Processing → Storage
- Background Processing: Cron → Stats Generation → Storage
- User Interface: Dashboard → API → Storage
- Authentication Flow: User → Auth → Permissions → Resources

##### Implementation Details

- Language: Go 1.21+
- Database Options: SQLite/PostgreSQL
- Web Server: Native Go HTTP
- Template Engine: Go templates
- Static Files: Embedded resources
- Internationalization: i18n support
- Authentication: Built-in user system

## Diagram (Mermaid)

```mermaid
graph TB
    %% Define styles
    classDef frontend fill:#E1F5FE,stroke:#0288D1
    classDef backend fill:#E8F5E9,stroke:#388E3C
    classDef database fill:#FCE4EC,stroke:#C2185B
    classDef processing fill:#FFF3E0,stroke:#F57C00
    classDef security fill:#FFEBEE,stroke:#D32F2F
    classDef external fill:#F5F5F5,stroke:#616161

    %% Frontend Layer
    subgraph Frontend
        UI["Dashboard UI"]
        JS["JavaScript Counter"]
        Assets["Static Assets"]
        Templates["Templates"]
    end
    click UI "https://github.com/arp242/goatcounter/blob/master/public/dashboard.js"
    click JS "https://github.com/arp242/goatcounter/blob/master/public/count.js"
    click Assets "https://github.com/arp242/goatcounter/tree/master/public/"
    click Templates "https://github.com/arp242/goatcounter/tree/master/tpl/"

    %% API/Service Layer
    subgraph APILayer
        API["API Endpoints"]
        CountHandler["Count Handler"]
        AuthHandler["Auth Handler"]
        DashHandler["Dashboard Handler"]
    end
    click API "https://github.com/arp242/goatcounter/blob/master/handlers/api.go"
    click CountHandler "https://github.com/arp242/goatcounter/blob/master/handlers/count.go"
    click AuthHandler "https://github.com/arp242/goatcounter/blob/master/handlers/user.go"
    click DashHandler "https://github.com/arp242/goatcounter/blob/master/handlers/dashboard.go"

    %% Core Processing Layer
    subgraph Processing
        HitProc["Hit Processing"]
        StatsGen["Statistics Generation"]
        Campaign["Campaign Tracking"]
        DataAgg["Data Aggregation"]
    end
    click HitProc "https://github.com/arp242/goatcounter/blob/master/hit.go"
    click StatsGen "https://github.com/arp242/goatcounter/tree/master/widgets/"
    click Campaign "https://github.com/arp242/goatcounter/blob/master/campaign.go"
    click DataAgg "https://github.com/arp242/goatcounter/tree/master/cron/"

    %% Storage Layer
    subgraph Storage
        DB[(Database)]
        Migrations["Migration System"]
        QueryLayer["Query Layer"]
    end
    click DB "https://github.com/arp242/goatcounter/tree/master/db/"
    click Migrations "https://github.com/arp242/goatcounter/tree/master/db/migrate/"
    click QueryLayer "https://github.com/arp242/goatcounter/tree/master/db/query/"

    %% Background Services
    subgraph Background
        CronJobs["Cron Jobs"]
        EmailReports["Email Reports"]
        BrowserStats["Browser Stats"]
        LocationStats["Location Stats"]
    end
    click CronJobs "https://github.com/arp242/goatcounter/tree/master/cron/"
    click EmailReports "https://github.com/arp242/goatcounter/blob/master/cron/email_reports.go"
    click BrowserStats "https://github.com/arp242/goatcounter/blob/master/cron/browser_stat.go"
    click LocationStats "https://github.com/arp242/goatcounter/blob/master/cron/location_stat.go"

    %% External Services
    subgraph External
        ACME["ACME/Let's Encrypt"]
        LogImport["Log Import System"]
    end
    click ACME "https://github.com/arp242/goatcounter/blob/master/acme/acme.go"
    click LogImport "https://github.com/arp242/goatcounter/blob/master/logscan/logscan.go"

    %% Relationships
    JS --> CountHandler
    UI --> API
    UI --> DashHandler
    CountHandler --> HitProc
    HitProc --> DB
    API --> DB
    AuthHandler --> DB
    DashHandler --> DB
    StatsGen --> DB
    Campaign --> DB
    DataAgg --> DB
    CronJobs --> StatsGen
    EmailReports --> DB
    BrowserStats --> DB
    LocationStats --> DB
    LogImport --> HitProc
    ACME --> APILayer

    %% Style Applications
    class UI,JS,Assets,Templates frontend
    class API,CountHandler,AuthHandler,DashHandler backend
    class DB,Migrations,QueryLayer database
    class HitProc,StatsGen,Campaign,DataAgg processing
    class ACME,LogImport external

    %% Legend
    subgraph Legend
        Frontend_["Frontend"]:::frontend
        Backend_["Backend"]:::backend
        Database_["Database"]:::database
        Processing_["Processing"]:::processing
        External_["External"]:::external
    end
```
