# alrf/go_redis_pg architecture

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

- Interactive diagram: https://gitdiagram.com/alrf/go_redis_pg
- Repository: https://github.com/alrf/go_redis_pg

## Overview

##### Project type and purpose
- This is a backend-focused Golang REST API service that exposes a single endpoint (/humai/echoservice).
- It persists incoming JSON payloads into Redis (cache) and PostgreSQL (primary store), and returns augmented JSON (adds timestamp).
- It uses Docker and docker-compose to orchestrate containers for the API, Redis, PostgreSQL, and a Jenkins server for CI/CD.

##### File structure
- Top-level:
- Dockerfile, .dockerignore, docker-compose.yml, .env, Makefile
- app/ contains the Go code (main.go, controllers, models, utils, go.mod/sum)
- data/ holds persistent volumes for Redis and PostgreSQL
- services/jenkins/ defines the Jenkins container, its Dockerfile, and pipeline config (init.groovy, jobs.groovy)
- Patterns & configs:
- Multi-stage Docker build in Dockerfile (builder → app)
- docker-compose.yml ties together four services: go-api, redis, postgres, jenkins
- Makefile automates curl-based tests
- Jenkins pipeline defined in services/jenkins/config/jobs.groovy

##### README for additional insights
- Endpoint behavior: POST checks Redis → if miss, write to Redis & PostgreSQL → respond with original payload + timestamp. GET returns last 10 entries from PostgreSQL.
- CI pipeline: clone repo → build Docker image → push to Docker Hub.
- Deployment steps: docker-compose up/down.

##### Detail without overthinking
- Every container and its role
- Sequence: code → Jenkins → build → Docker Hub → deploy → runtime
- Cache-aside logic step by step
- Persistent volume mounts from data/ directory

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entities
    Client["HTTP Client"]:::external
    CodeRepo["Code Repository"]:::external
    DockerHub["Docker Hub"]:::external

    %% Docker Host Environment
    subgraph "Docker Host"
        direction TB
        subgraph "Containers"
            direction TB
            API["Go-API Service"]:::compute
            Redis["Redis Cache"]:::db
            Postgres["PostgreSQL DB"]:::db
            Jenkins["Jenkins Server"]:::pipeline
        end
        subgraph "Volumes"
            direction TB
            DataVolumes["data/ (Redis & Postgres Volumes)"]:::storage
        end
    end

    %% CI/CD Pipeline Flow
    CodeRepo -->|push code| Jenkins
    Jenkins -->|build image| DockerHub
    DockerHub -->|pull images| DockerHost["docker-compose"]:::deployment
    DockerHost -->|deploy containers| API
    DockerHost -->|deploy containers| Redis
    DockerHost -->|deploy containers| Postgres
    DockerHost -->|deploy containers| Jenkins

    %% Runtime Data Flow
    Client -->|HTTP POST / GET| API
    API -->|GET/SET| Redis
    API -->|INSERT/SELECT| Postgres
    API -->|response JSON + timestamp| Client

    %% Click Events
    click API "https://github.com/alrf/go_redis_pg/tree/master/app/"
    click Main "https://github.com/alrf/go_redis_pg/blob/master/app/main.go"
    click Controllers "https://github.com/alrf/go_redis_pg/blob/master/app/controllers/inventoryControllers.go"
    click ModelBase "https://github.com/alrf/go_redis_pg/blob/master/app/models/base.go"
    click ModelInv "https://github.com/alrf/go_redis_pg/blob/master/app/models/inventory.go"
    click Utils "https://github.com/alrf/go_redis_pg/blob/master/app/utils/util.go"
    click GoMod "https://github.com/alrf/go_redis_pg/blob/master/app/go.mod"
    click GoSum "https://github.com/alrf/go_redis_pg/blob/master/app/go.sum"
    click Dockerfile "https://github.com/alrf/go_redis_pg/tree/master/Dockerfile"
    click Dockerignore "https://github.com/alrf/go_redis_pg/blob/master/.dockerignore"
    click Compose "https://github.com/alrf/go_redis_pg/blob/master/docker-compose.yml"
    click Env "https://github.com/alrf/go_redis_pg/blob/master/.env"
    click Makefile "https://github.com/alrf/go_redis_pg/tree/master/Makefile"
    click DataVolumes "https://github.com/alrf/go_redis_pg/tree/master/data/"
    click JenkinsDocker "https://github.com/alrf/go_redis_pg/tree/master/services/jenkins/Dockerfile"
    click JenkinsInit "https://github.com/alrf/go_redis_pg/blob/master/services/jenkins/config/init.groovy"
    click JenkinsJobs "https://github.com/alrf/go_redis_pg/blob/master/services/jenkins/config/jobs.groovy"

    %% Classes
    classDef compute fill:#D6E9F8,stroke:#31708F,color:#31708F;
    classDef db fill:#DFF0D8,stroke:#3C763D,color:#3C763D;
    classDef pipeline fill:#FCF8E3,stroke:#8A6D3B,color:#8A6D3B;
    classDef external fill:#F2DEDE,stroke:#A94442,color:#A94442;
    classDef storage fill:#EBF5FB,stroke:#2E86C1,color:#2E86C1;
    classDef deployment fill:#F5EEF8,stroke:#884EA0,color:#884EA0;

    %% Node Class Assignments
    class API compute
    class Redis db
    class Postgres db
    class Jenkins pipeline
    class Client external
    class CodeRepo external
    class DockerHub external
    class DataVolumes storage
    class DockerHost deployment

    %% Additional Clicks
    click Main "https://github.com/alrf/go_redis_pg/blob/master/app/main.go"
    click Controllers "https://github.com/alrf/go_redis_pg/blob/master/app/controllers/inventoryControllers.go"
    click ModelBase "https://github.com/alrf/go_redis_pg/blob/master/app/models/base.go"
    click ModelInv "https://github.com/alrf/go_redis_pg/blob/master/app/models/inventory.go"
    click Utils "https://github.com/alrf/go_redis_pg/blob/master/app/utils/util.go"
    click GoMod "https://github.com/alrf/go_redis_pg/blob/master/app/go.mod"
    click GoSum "https://github.com/alrf/go_redis_pg/blob/master/app/go.sum"
```
