# vpatel95/go-api-server architecture

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

- Interactive diagram: https://gitdiagram.com/vpatel95/go-api-server
- Repository: https://github.com/vpatel95/go-api-server

## Overview

##### Project type and purpose
- This is a monolithic, backend‐only RESTful API server written in Go.
- It exposes versioned endpoints under `/api/v1`, with user management and authentication functionality.
- It uses Gorilla Mux for HTTP routing and Gorm for ORM/database interactions.

##### File structure
- **Entry point**
- `main.go` – bootstraps the application.
- `env/env.go` – reads environment variables and sets up runtime config.
- **Application initialization**
- `app/app.go` – constructs the app context (config, router, DB).
- **Routing layer**
- `route/route.go` – wires Gorilla Mux router to the app and mounts API routes.
- `app/api/api.go` – groups versioned APIs.
- **Versioned API**
- `app/api/v1/routes.go` – defines common v1 route base.
- `app/api/v1/auth` – `auth.go` (handlers), `routes.go` (login/register).
- `app/api/v1/users` – `user.go` (model/handler), `routes.go` (user CRUD).
- **Middleware**
- `app/api/v1/middleware/middlewares.go` – auth checks, logging, etc.
- **Business logic & utilities**
- `app/lib/common/common.go` – shared helpers.
- `app/lib/validation/validate.go` – input validation.
- **Data layer**
- `database/db.go` – Gorm setup and connection pooling.
- `app/model` – Gorm models (`user.go`), migrations (`migrate.go`).
- **Config files**
- `configs/database.json` – DB connection details.
- `configs/jwtsecret.key` – secret for signing JWTs.
- `configs/session.json` – session store config.

##### README for additional insights
- Confirms use of Gorilla Mux (router) and Gorm (ORM).
- Indicates it’s a “Sample API server,” so extensibility and clarity are priorities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry & Config
    subgraph "Startup Initialization"
        direction TB
        HTTP["main.go (HTTP Server Bootstrap)"]:::bootstrap
        ENV["env/env.go (Env Loader)"]:::config
        APPCTX["app/app.go (App Context Init)"]:::bootstrap
    end

    %% Config Store Files
    subgraph "Config Store"
        direction TB
        DBCONF["database.json"]:::configFile
        JWTKEY["jwtsecret.key"]:::configFile
        SESSCONF["session.json"]:::configFile
    end

    %% Router Layer
    subgraph "Routing Layer (Gorilla Mux)" 
        direction TB
        ROUTE["route/route.go (Router Setup)"]:::router
        APIGROUP["app/api/api.go (API Version Grouping)"]:::router
    end

    %% API v1 Handlers
    subgraph "API v1 Endpoints (/api/v1)" 
        direction TB
        V1ROUTES["app/api/v1/routes.go (v1 Base Routes)"]:::router
        subgraph "Auth Module" 
            direction TB
            AUTHHAND["app/api/v1/auth/auth.go (Auth Handlers)"]:::handler
            AUTHROUTES["app/api/v1/auth/routes.go (Auth Routes)"]:::router
        end
        subgraph "User Module" 
            direction TB
            USERMOD["app/api/v1/users/user.go (User Model/Handler)"]:::handler
            USERROUTES["app/api/v1/users/routes.go (User CRUD Routes)"]:::router
        end
    end

    %% Middleware
    MW["app/api/v1/middleware/middlewares.go (Middleware Chain)"]:::middleware

    %% Utility Libraries
    subgraph "Utility Libraries"
        direction TB
        COMMON["app/lib/common/common.go"]:::utility
        VALID["app/lib/validation/validate.go"]:::utility
    end

    %% Data Access Layer
    DAL["database/db.go (Gorm DB Setup)"]:::data

    %% Models & Migrations
    subgraph "Models & Migrations"
        direction TB
        MODEL["app/model/user.go"]:::dataModel
        MIGRATE["app/model/migrate.go"]:::dataModel
    end

    %% Database
    DB["PostgreSQL/MySQL Database"]:::database

    %% External JWT Service
    JWT["JWT Signing/Verification Service"]:::external

    %% Relationships
    HTTP -->|loads| ENV
    HTTP -->|calls| APPCTX
    APPCTX -->|reads| DBCONF
    APPCTX -->|reads| JWTKEY
    APPCTX -->|reads| SESSCONF
    APPCTX -->|initializes| ROUTE

    ROUTE --> APIGROUP
    APIGROUP --> V1ROUTES

    V1ROUTES --> AUTHROUTES
    V1ROUTES --> USERROUTES

    AUTHROUTES --> AUTHHAND
    USERROUTES --> USERMOD

    AUTHHAND -->|uses| MW
    USERMOD -->|uses| MW

    AUTHHAND -->|calls| COMMON
    AUTHHAND -->|calls| VALID
    USERMOD -->|calls| COMMON
    USERMOD -->|calls| VALID

    AUTHHAND -->|DB queries| DAL
    USERMOD -->|DB queries| DAL

    DAL -->|manages| MODEL
    DAL -->|executes| MIGRATE
    DAL -->|connects to| DB

    AUTHHAND <-->|issues/validates tokens| JWT
    USERMOD <-->|validates tokens| JWT

    %% Click Events
    click HTTP "https://github.com/vpatel95/go-api-server/blob/master/main.go"
    click ENV "https://github.com/vpatel95/go-api-server/blob/master/env/env.go"
    click APPCTX "https://github.com/vpatel95/go-api-server/blob/master/app/app.go"
    click ROUTE "https://github.com/vpatel95/go-api-server/blob/master/route/route.go"
    click APIGROUP "https://github.com/vpatel95/go-api-server/blob/master/app/api/api.go"
    click V1ROUTES "https://github.com/vpatel95/go-api-server/blob/master/app/api/v1/routes.go"
    click AUTHHAND "https://github.com/vpatel95/go-api-server/blob/master/app/api/v1/auth/auth.go"
    click AUTHROUTES "https://github.com/vpatel95/go-api-server/blob/master/app/api/v1/auth/routes.go"
    click USERMOD "https://github.com/vpatel95/go-api-server/blob/master/app/api/v1/users/user.go"
    click USERROUTES "https://github.com/vpatel95/go-api-server/blob/master/app/api/v1/users/routes.go"
    click MW "https://github.com/vpatel95/go-api-server/blob/master/app/api/v1/middleware/middlewares.go"
    click COMMON "https://github.com/vpatel95/go-api-server/blob/master/app/lib/common/common.go"
    click VALID "https://github.com/vpatel95/go-api-server/blob/master/app/lib/validation/validate.go"
    click DAL "https://github.com/vpatel95/go-api-server/blob/master/database/db.go"
    click MODEL "https://github.com/vpatel95/go-api-server/blob/master/app/model/user.go"
    click MIGRATE "https://github.com/vpatel95/go-api-server/blob/master/app/model/migrate.go"
    click DBCONF "https://github.com/vpatel95/go-api-server/blob/master/configs/database.json"
    click JWTKEY "https://github.com/vpatel95/go-api-server/blob/master/configs/jwtsecret.key"
    click SESSCONF "https://github.com/vpatel95/go-api-server/blob/master/configs/session.json"

    %% Styles
    classDef bootstrap fill:#D0E6A5,stroke:#333,stroke-width:1px
    classDef config fill:#FFFFC2,stroke:#333,stroke-width:1px
    classDef configFile fill:#FFF2CC,stroke:#333,stroke-width:1px
    classDef router fill:#A0C4FF,stroke:#333,stroke-width:1px
    classDef handler fill:#CAE9FF,stroke:#333,stroke-width:1px
    classDef middleware fill:#FFE4B5,stroke:#333,stroke-width:1px
    classDef utility fill:#E0BBE4,stroke:#333,stroke-width:1px
    classDef data fill:#C8E6C9,stroke:#333,stroke-width:1px
    classDef dataModel fill:#AED581,stroke:#333,stroke-width:1px
    classDef database fill:#B2DFDB,stroke:#333,stroke-width:1px
    classDef external fill:#FFCCCB,stroke:#333,stroke-width:1px
```
