# aanorlondo/go-auth-middleware architecture

> Architecture diagram of the GitHub repository aanorlondo/go-auth-middleware, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aanorlondo/go-auth-middleware
- Repository: https://github.com/aanorlondo/go-auth-middleware

## Overview

##### Project Type & Purpose
- This is a Dockerized, Go-based authentication microservice (“go-auth-middleware”) that exposes a RESTful HTTP API for signing up, logging in, user management and role-based access control (RBAC).
- It uses MySQL for persistent user data, Redis for caching and RBAC storage, JWT for stateless authentication, and Swagger for API documentation.

##### Core File Structure & Architectural Patterns
- Top-Level Directories:
- app/ : the Go application (config, models, server, middleware, utils, main entry point)
- db/ : Dockerfiles and init templates for MySQL and Redis
- scripts/ : Bash scripts to prepare env, spin up containers, initialize databases
- Layered Organization (akin to MVC + middleware + utils):
- config – environment & connection settings
- models – data access layer for MySQL
- server/api – Swagger/OpenAPI spec
- server/handlers – HTTP controllers (auth, user, rbac)
- server/middleware – JWT validation
- utils – logging & JWT helper functions

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients and Gateway
    Client["Client (Web/Mobile/CLI)"]:::client
    LB["API Gateway / Load Balancer"]:::service

    %% Go Auth Service Components
    subgraph "go-auth-middleware Service"
        direction TB
        main["Main Entry Point"]:::service
        conf["Configuration Loader"]:::service
        server["HTTP Server & Router"]:::service
        jwtMiddleware["JWT Middleware"]:::service
        subgraph "Handlers"
            direction TB
            auth["Auth Handler"]:::service
            user["User Handler"]:::service
            rbac["RBAC Handler"]:::service
        end
        subgraph "Models"
            direction TB
            userModel["MySQL User Model"]:::service
        end
        subgraph "Utils"
            direction TB
            jwtUtils["JWT Utilities"]:::service
            logger["Logger Utilities"]:::service
        end
        swagger["Swagger UI & API Docs"]:::service
    end

    %% Data Stores
    mysqlDB["MySQL Database Container\n(port 3306)"]:::datastore
    redisDB["Redis Server Container\n(port 6379)"]:::datastore

    %% Orchestration
    subgraph "Orchestration & Deployment Scripts"
        direction TB
        prepEnv["prepare_env.sh"]:::orchestration
        prepMySQL["prepare_mysql_init.sh"]:::orchestration
        prepRedis["prepare_redis_init.sh"]:::orchestration
        dockerMW["docker_middleware.sh"]:::orchestration
        dockerMySQL["docker_mysql.sh"]:::orchestration
        dockerRedis["docker_redis.sh"]:::orchestration
        goInit["go-init.sh"]:::orchestration
        goMod["go.mod / go.sum"]:::orchestration
        dockerfileApp["Go App Dockerfile"]:::orchestration
        dockerfileMySQL["MySQL Dockerfile"]:::orchestration
        mysqlInit["init_template.sql"]:::orchestration
        redisConf["redis_template.conf"]:::orchestration
    end

    %% Flows
    Client -->|HTTP| LB
    LB -->|HTTP| main
    main -->|loads| conf
    main -->|starts| server
    server -->|uses| jwtMiddleware
    jwtMiddleware -->|forwards| auth
    jwtMiddleware -->|forwards| user
    jwtMiddleware -->|forwards| rbac
    auth -->|queries| userModel
    user -->|queries| userModel
    rbac -->|uses| redisDB
    userModel -->|TCP 3306| mysqlDB
    auth -->|generates/verifies| jwtUtils
    user -->|logs| logger
    server -->|serves| swagger

    %% Initialization Flows
    prepEnv -.->|sets env vars| main
    prepMySQL -.->|prepares| mysqlInit
    prepRedis -.->|prepares| redisConf
    dockerMW -.->|builds & runs| main
    dockerMySQL -.->|builds & runs| mysqlDB
    dockerRedis -.->|builds & runs| redisDB
    goInit -.->|entrypoint| main
    goMod -.->|dependencies| main

    %% Click Events
    click conf "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/config/config.go"
    click main "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/main.go"
    click server "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/server/server.go"
    click jwtMiddleware "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/server/middleware/jwt.go"
    click auth "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/server/handlers/auth.go"
    click user "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/server/handlers/user.go"
    click rbac "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/server/handlers/rbac.go"
    click userModel "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/models/user.go"
    click jwtUtils "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/utils/jwt.go"
    click logger "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/utils/logger.go"
    click swagger "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/server/api/api.yaml"
    click dockerfileApp "https://github.com/aanorlondo/go-auth-middleware/tree/main/app/Dockerfile"
    click dockerfileMySQL "https://github.com/aanorlondo/go-auth-middleware/tree/main/db/mysql/Dockerfile"
    click mysqlInit "https://github.com/aanorlondo/go-auth-middleware/blob/main/db/mysql/init_template.sql"
    click redisConf "https://github.com/aanorlondo/go-auth-middleware/blob/main/db/redis/redis_template.conf"
    click prepEnv "https://github.com/aanorlondo/go-auth-middleware/blob/main/scripts/prepare_env.sh"
    click prepMySQL "https://github.com/aanorlondo/go-auth-middleware/blob/main/scripts/prepare_mysql_init.sh"
    click prepRedis "https://github.com/aanorlondo/go-auth-middleware/blob/main/scripts/prepare_redis_init.sh"
    click dockerMW "https://github.com/aanorlondo/go-auth-middleware/blob/main/scripts/docker_middleware.sh"
    click dockerMySQL "https://github.com/aanorlondo/go-auth-middleware/blob/main/scripts/docker_mysql.sh"
    click dockerRedis "https://github.com/aanorlondo/go-auth-middleware/blob/main/scripts/docker_redis.sh"
    click goInit "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/go-init.sh"
    click goMod "https://github.com/aanorlondo/go-auth-middleware/blob/main/app/go.mod"

    %% Styles
    classDef client fill:#E0E0E0,stroke:#333,stroke-width:1px
    classDef service fill:#D6E9FF,stroke:#1F4E79,stroke-width:1px
    classDef datastore fill:#FFE6CC,stroke:#A65E00,stroke-width:1px
    classDef orchestration fill:#DFF2BF,stroke:#448700,stroke-width:1px
```
