# mathvaillant/ticket-booking-backend architecture

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

- Interactive diagram: https://gitdiagram.com/mathvaillant/ticket-booking-backend
- Repository: https://github.com/mathvaillant/ticket-booking-backend

## Overview

##### Project Type and Purpose
- This is a single‐service REST API written in Go.
- It implements authentication, event management, and ticketing endpoints.
- It persists data in a relational database and is containerized via Docker.

##### File Structure
- cmd/api/main.go: entry point for the API server
- handlers/: HTTP handler functions (auth.go, event.go, ticket.go)
- middlewares/: request filtering (auth‐protected endpoints)
- services/: business logic layer
- repositories/: data access layer (CRUD against DB)
- models/: domain entities (User, Event, Ticket, Auth structures)
- db/: database initialization and migration code
- config/: configuration loader (env, .env)
- utils/: JWT utilities for token generation and validation
- Dockerfile, docker-compose.yaml: container definitions
- Makefile, .air.toml: developer tooling (build, run, live‐reload)

##### README
- Minimal content; no architectural hints beyond “main” branch.
- Rely primarily on directory naming and code conventions.

##### Main Components
a. API Server
- Layered internally:
- HTTP Layer (handlers + middlewares)
- Service Layer (business logic)
- Repository Layer (DB access)
- Models (shared data structures)
- Config & Utils (global helpers)
b. Database
- Relational DB (e.g., PostgreSQL/MySQL) launched via docker-compose
- Migration subsystem (db/auto-migrate.go)
c. External Clients
- Web/Mobile clients or API consumers sending HTTP/HTTPS requests
d. Infrastructure
- Docker container for API
- Docker network linking API ←→ DB

##### Relationships & Data Flow
- Client → HTTP(S) → API Gateway (if present) or directly to API
- API Entry (cmd/api) → HTTP Middleware (auth, logging) → Handler
- Handler invokes Service methods
- Service calls Repository methods
- Repository runs SQL via db.Database instance → Database container
- Database returns rows → Repository → Service → Handler → Response to Client
- Config loader initializes environment variables; Utils (JWT) injects tokens into HTTP headers.

##### Architectural Patterns & Design Principles
- Layered (Onion) Architecture: separate concerns (Presentation, Domain, Persistence)
- Dependency Injection via config/config.go and main.go wiring
- Single Responsibility: each package handles a single aspect (e.g., auth, events)
- Containerization: reproducible environments with Docker & docker-compose
- Auto‐migration at startup ensures schema is in sync.

##### Technologies, Frameworks & Libraries
- Go (Go modules for dependency management)
- Gorilla Mux or built‐in net/http for routing (implied by handlers)
- Docker, docker-compose
- .air for hot‐reload in development
- JWT (utils/jwt.go) for stateless auth
- SQL driver (MySQL/Postgres) via db/database.go
- Makefile for build/test automation

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["External Client"]:::external

    %% API Container
    subgraph "API Container"
        direction TB

        %% Entry Point
        API_ENTRY["main.go"]:::infra

        %% HTTP Layer
        subgraph "HTTP Layer"
            direction TB
            Middleware["auth-protected.go"]:::presentation
            AuthHandler["auth.go"]:::presentation
            EventHandler["event.go"]:::presentation
            TicketHandler["ticket.go"]:::presentation
        end

        %% Domain Layer
        subgraph "Domain Layer"
            direction TB
            AuthService["auth.go"]:::domain
            UserModel["user.go"]:::domain
            AuthModel["auth.go"]:::domain
            EventModel["event.go"]:::domain
            TicketModel["ticket.go"]:::domain
        end

        %% Persistence Layer
        subgraph "Persistence Layer"
            direction TB
            AuthRepo["auth.go"]:::persistence
            EventRepo["event.go"]:::persistence
            TicketRepo["ticket.go"]:::persistence
        end

        %% Infrastructure
        subgraph "Infrastructure"
            direction TB
            ConfigLoader["config.go"]:::infra
            JWTUtils["jwt.go"]:::infra
            DatabaseInit["database.go"]:::infra
            AutoMigrate["auto-migrate.go"]:::infra
        end
    end

    %% Database Container
    Database["Database\n(Postgres/MySQL)"]:::db

    %% Data Flows
    Client -->|JSON/HTTP| API_ENTRY
    API_ENTRY -->|initialize| ConfigLoader
    API_ENTRY -->|initialize| JWTUtils
    API_ENTRY -->|startup| DatabaseInit
    DatabaseInit -->|runs migrate| AutoMigrate
    AutoMigrate -->|SQL migrate| Database
    API_ENTRY -->|register| Middleware
    Middleware -->|authorize| AuthHandler
    AuthHandler -->|calls| AuthService
    EventHandler -->|calls| AuthService
    TicketHandler -->|calls| AuthService
    AuthService -->|uses| UserModel
    AuthService -->|uses| AuthModel
    AuthService -->|uses| EventModel
    AuthService -->|uses| TicketModel
    AuthService -->|calls| AuthRepo
    AuthService -->|calls| EventRepo
    AuthService -->|calls| TicketRepo
    AuthRepo -->|SQL/TCP| Database
    EventRepo -->|SQL/TCP| Database
    TicketRepo -->|SQL/TCP| Database

    %% Click Events
    click API_ENTRY "https://github.com/mathvaillant/ticket-booking-backend/blob/main/cmd/api/main.go"
    click Middleware "https://github.com/mathvaillant/ticket-booking-backend/blob/main/middlewares/auth-protected.go"
    click AuthHandler "https://github.com/mathvaillant/ticket-booking-backend/blob/main/handlers/auth.go"
    click EventHandler "https://github.com/mathvaillant/ticket-booking-backend/blob/main/handlers/event.go"
    click TicketHandler "https://github.com/mathvaillant/ticket-booking-backend/blob/main/handlers/ticket.go"
    click AuthService "https://github.com/mathvaillant/ticket-booking-backend/blob/main/services/auth.go"
    click AuthRepo "https://github.com/mathvaillant/ticket-booking-backend/blob/main/repositories/auth.go"
    click EventRepo "https://github.com/mathvaillant/ticket-booking-backend/blob/main/repositories/event.go"
    click TicketRepo "https://github.com/mathvaillant/ticket-booking-backend/blob/main/repositories/ticket.go"
    click UserModel "https://github.com/mathvaillant/ticket-booking-backend/blob/main/models/user.go"
    click AuthModel "https://github.com/mathvaillant/ticket-booking-backend/blob/main/models/auth.go"
    click EventModel "https://github.com/mathvaillant/ticket-booking-backend/blob/main/models/event.go"
    click TicketModel "https://github.com/mathvaillant/ticket-booking-backend/blob/main/models/ticket.go"
    click DatabaseInit "https://github.com/mathvaillant/ticket-booking-backend/blob/main/db/database.go"
    click AutoMigrate "https://github.com/mathvaillant/ticket-booking-backend/blob/main/db/auto-migrate.go"
    click ConfigLoader "https://github.com/mathvaillant/ticket-booking-backend/blob/main/config/config.go"
    click JWTUtils "https://github.com/mathvaillant/ticket-booking-backend/blob/main/utils/jwt.go"
    click Dockerfile "https://github.com/mathvaillant/ticket-booking-backend/tree/main/Dockerfile"
    click DockerCompose "https://github.com/mathvaillant/ticket-booking-backend/blob/main/docker-compose.yaml"
    click Makefile "https://github.com/mathvaillant/ticket-booking-backend/tree/main/Makefile"
    click AirConfig "https://github.com/mathvaillant/ticket-booking-backend/blob/main/.air.toml"
    click EnvFile "https://github.com/mathvaillant/ticket-booking-backend/blob/main/.env"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef presentation fill:#bbdefb,stroke:#1e88e5,stroke-width:1px
    classDef domain fill:#c8e6c9,stroke:#43a047,stroke-width:1px
    classDef persistence fill:#ffe0b2,stroke:#fb8c00,stroke-width:1px
    classDef infra fill:#eceff1,stroke:#607d8b,stroke-width:1px
    classDef db fill:#d7ccc8,stroke:#795548,stroke-width:1px
```
