# sipcapture/heplify-server architecture

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

- Interactive diagram: https://gitdiagram.com/sipcapture/heplify-server
- Repository: https://github.com/sipcapture/heplify-server

## Overview

##### Project type and purpose
- heplify-server is a stand-alone, single-binary Go backend service.
- Purpose: listen for HEP-encapsulated SIP/RTCP packets over UDP/TCP/TLS/WebSocket, decode them, persist into a Homer-compatible database (MySQL or PostgreSQL, H5 or H7 schema), rotate tables, expose Prometheus metrics, and optionally ship logs to remote backends.

##### File structure
- Top-level “cmd/heplify-server”: entry-point main.go.
- “server/”: network listeners for UDP, TCP, TLS, WS.
- “decoder/”: HEP parsing, SIP parsing (“sipparser/”), scripting engines (Lua, expr).
- “database/”: adapters for MySQL/Postgres, table management, mock/test.
- “rotator/”: timed/size-based database table rotation.
- “metric/”: metric definition, Prometheus exporter, reload functionality.
- “remotelog/”: remote log shippers (Elasticsearch, Loki, lineproto).
- “config/”: loading CLI, env, file, and HTTP/web form configuration.
- “scripts/” and “example/”: helper scripts and sample configs.
- “docker/”: several compose stacks tying heplify-server with Homer UI, Prometheus, Grafana, Alertmanager, Loki, Caddy, Influx.

##### README for additional insights
- Single binary optimized for speed/simplicity, no external SIP proxy required.
- Supports hot reload of Prometheus target settings via SIGHUP.
- Deployed stand-alone or via Docker Compose stacks (hom5, hom7, prom/graf, prom/loki/graf, etc.).

##### Detail
- Sub-modules (e.g., separate out lua vs. expr engines) if space permits.
- Call out hot-reload SIGHUP path from config file to Metrics/RemoteLog modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% heplify-server monolith
    subgraph "heplify-server"
        direction TB
        HS["heplify-server"]:::core
        click HS "https://github.com/sipcapture/heplify-server/blob/master/cmd/heplify-server/heplify-server.go"

        %% Configuration Manager
        subgraph "Configuration Manager"
            direction TB
            Config["Config Manager"]:::core
            click Config "https://github.com/sipcapture/heplify-server/blob/master/config/config.go"
            click Config "https://github.com/sipcapture/heplify-server/blob/master/config/regexstore.go"
            click Config "https://github.com/sipcapture/heplify-server/blob/master/config/webconfig.go"
        end

        %% Ingestion Layer
        subgraph "Ingestion Layer"
            direction TB
            UDP["UDP Listener"]:::core
            click UDP "https://github.com/sipcapture/heplify-server/blob/master/server/udp.go"
            TCP["TCP Listener"]:::core
            click TCP "https://github.com/sipcapture/heplify-server/blob/master/server/tcp.go"
            TLS["TLS Listener"]:::core
            click TLS "https://github.com/sipcapture/heplify-server/blob/master/server/tls.go"
            WS["WebSocket Listener"]:::core
            click WS "https://github.com/sipcapture/heplify-server/blob/master/server/ws.go"
        end

        %% Decoder Layer
        subgraph "Decoder Layer"
            direction TB
            HEP["HEP Parser"]:::core
            click HEP "https://github.com/sipcapture/heplify-server/blob/master/decoder/hep.go"
            click HEP "https://github.com/sipcapture/heplify-server/blob/master/decoder/hep.pb.go"
            SIP["SIP Parser"]:::core
            click SIP "https://github.com/sipcapture/heplify-server/tree/master/sipparser/"
            Expr["Expression Engine"]:::core
            click Expr "https://github.com/sipcapture/heplify-server/blob/master/decoder/exprengine.go"
            Lua["Script Engine (Lua)"]:::core
            click Lua "https://github.com/sipcapture/heplify-server/blob/master/decoder/luaengine.go"
            click Lua "https://github.com/sipcapture/heplify-server/tree/master/decoder/luar/"
        end

        %% Storage Layer
        subgraph "Storage Layer"
            direction TB
            DBIntf["DB Interface & Mocks"]:::core
            click DBIntf "https://github.com/sipcapture/heplify-server/blob/master/database/database.go"
            click DBIntf "https://github.com/sipcapture/heplify-server/blob/master/database/mock.go"
            MySQLAd["MySQL Adapter"]:::core
            click MySQLAd "https://github.com/sipcapture/heplify-server/blob/master/database/mysql.go"
            PostgresAd["Postgres Adapter"]:::core
            click PostgresAd "https://github.com/sipcapture/heplify-server/blob/master/database/postgres.go"
            MySQLDB[(MySQL DB)]:::db
            PostgresDB[(PostgreSQL DB)]:::db
        end

        %% Rotator
        Rotator["Table Rotator"]:::core
        click Rotator "https://github.com/sipcapture/heplify-server/blob/master/rotator/rotator.go"
        click Rotator "https://github.com/sipcapture/heplify-server/blob/master/rotator/mariafiles.go"
        click Rotator "https://github.com/sipcapture/heplify-server/blob/master/rotator/pgfiles.go"

        %% Metrics Exporter
        Metrics["Prometheus Exporter"]:::core
        click Metrics "https://github.com/sipcapture/heplify-server/blob/master/metric/prometheus.go"
        click Metrics "https://github.com/sipcapture/heplify-server/blob/master/metric/definition.go"
        click Metrics "https://github.com/sipcapture/heplify-server/blob/master/metric/reload.go"

        %% RemoteLog Shipper
        subgraph "RemoteLog Shipper"
            direction TB
            ELK["Elasticsearch Client"]:::core
            click ELK "https://github.com/sipcapture/heplify-server/blob/master/remotelog/elasticsearch.go"
            LokiCL["Loki Client"]:::core
            click LokiCL "https://github.com/sipcapture/heplify-server/blob/master/remotelog/loki.go"
            InfluxCL["Influx/Lineproto"]:::core
            click InfluxCL "https://github.com/sipcapture/heplify-server/blob/master/remotelog/lineproto.go"
            click InfluxCL "https://github.com/sipcapture/heplify-server/tree/master/remotelog/logproto/"
        end

        %% Helper Scripts and Examples
        subgraph "Helper Files"
            direction TB
            Scripts["Scripts"]:::core
            click Scripts "https://github.com/sipcapture/heplify-server/blob/master/scripts/build_binary.sh"
            click Scripts "https://github.com/sipcapture/heplify-server/blob/master/scripts/build_package.sh"
            click Scripts "https://github.com/sipcapture/heplify-server/blob/master/scripts/example.lua"
            click Scripts "https://github.com/sipcapture/heplify-server/blob/master/scripts/example*.expr"
            Examples["Example Configs"]:::core
            click Examples "https://github.com/sipcapture/heplify-server/tree/master/example/"
        end

        %% Internal flows
        HS -->|loads via| Config
        Config -->|configs| UDP
        Config -->|configs| TCP
        Config -->|configs| TLS
        Config -->|configs| WS
        Config -->|configs| HEP
        Config -->|configs| SIP
        Config -->|configs| Expr
        Config -->|configs| Lua
        Config -->|configs| DBIntf
        Config -->|configs| MySQLAd
        Config -->|configs| PostgresAd
        Config -->|configs| Rotator
        Config -->|configs| Metrics
        Config -->|configs| ELK
        Config -->|configs| LokiCL
        Config -->|configs| InfluxCL

        %% Data flows
        UDP -->|"HEP packets"| HEP
        TCP -->|"HEP packets"| HEP
        TLS -->|"HEP packets"| HEP
        WS -->|"HEP packets"| HEP

        HEP -->|"parsed HEP"| SIP
        HEP -->|"parsed HEP"| Expr
        HEP -->|"parsed HEP"| Lua

        SIP -->|"decoded events"| DBIntf
        Expr -->|"decoded events"| DBIntf
        Lua -->|"decoded events"| DBIntf

        DBIntf -->|writes| MySQLAd
        DBIntf -->|writes| PostgresAd
        MySQLAd -->|persists| MySQLDB
        PostgresAd -->|persists| PostgresDB

        MySQLDB -->|"rotate triggers"| Rotator
        PostgresDB -->|"rotate triggers"| Rotator

        SIP -->|"metrics events"| Metrics
        Expr -->|"metrics events"| Metrics
        Lua -->|"metrics events"| Metrics

        Metrics -->|"/metrics scrape"| Prometheus

        SIP -->|"log events"| ELK
        SIP -->|"log events"| LokiCL
        SIP -->|"log events"| InfluxCL
        Expr -->|"log events"| ELK
        Lua -->|"log events"| ELK

    end

    %% External services via Docker Compose
    subgraph "Docker Compose (External Services)"
        direction TB
        HomerUI["Homer UI"]:::external
        Prometheus["Prometheus"]:::external
        Grafana["Grafana"]:::external
        Alertmanager["Alertmanager"]:::external
        Caddy["Caddy (TLS)"]:::external
        LokiExt["Loki"]:::external
        InfluxExt["InfluxDB"]:::external
        click HomerUI "https://github.com/sipcapture/heplify-server/tree/master/docker/"
        click Prometheus "https://github.com/sipcapture/heplify-server/tree/master/docker/"
        click Grafana "https://github.com/sipcapture/heplify-server/tree/master/docker/"
        click Alertmanager "https://github.com/sipcapture/heplify-server/tree/master/docker/"
        click Caddy "https://github.com/sipcapture/heplify-server/tree/master/docker/"
        click LokiExt "https://github.com/sipcapture/heplify-server/tree/master/docker/"
        click InfluxExt "https://github.com/sipcapture/heplify-server/tree/master/docker/"
    end

    %% External flows
    Prometheus -->|"scrapes metrics"| Metrics
    Grafana -->|"dashboards"| Prometheus
    Alertmanager -->|"alerts from"| Prometheus
    Caddy -->|reverse-proxy| HS
    LokiExt -->|"receives logs"| ELK
    InfluxExt -->|"receives logs"| InfluxCL

    %% Styles
    classDef core fill:#bbdefb,stroke:#333,stroke-width:1px
    classDef external fill:#c8e6c9,stroke:#333,stroke-width:1px
    classDef db fill:#ffe082,stroke:#333,stroke-width:1px
```
