# rifkiilmi/golang-restfullapi architecture

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

- Interactive diagram: https://gitdiagram.com/rifkiilmi/golang-restfullapi
- Repository: https://github.com/rifkiilmi/golang-restfullapi

## Overview

##### Project type and purpose
- This is a backend‐only RESTful microservice written in Go for a simple banking system (“Simple Bank”).
- It exposes HTTP endpoints to create users, manage accounts, perform transfers, and handle authentication via JWT or PASETO tokens.

##### File structure
- Top level:
- main.go – service entrypoint
- go.mod/go.sum – dependency management
- makefile, .github/workflows/ci.yaml – build & CI/CD orchestration
- sqlc.yaml – SQLC code‐generation config
- app.env – environment variables for DB, token keys, etc.
- api/ – HTTP layer: server setup, request handlers (user.go, account.go, transfer.go), middleware, validation, and associated tests
- db/ – data‐access layer:
- migration/ – raw SQL migration scripts
- query/ – SQL raw queries (for manual use or reference)
- sqlc/ – code generated by SQLC (querier, store, models, tests)
- mock/ – store mocks for unit tests
- token/ – token generation/verification implementations (JWT and PASETO) and tests
- util/ – shared helpers: config loader, password hashing, random data, currency utilities

##### README for additional insights
- (Empty here, but file names and test coverage imply a strong emphasis on layered architecture, testing, and configuration.)

a. Main components
- API Server (api/server.go & routes)
- Business Logic / Store (db/sqlc/store.go + mock store)
- Database (PostgreSQL with migration scripts)
- Token Service (token package: JWT & PASETO maker)
- Utilities (util package: config, password, random, currency)
- CI/CD Pipeline (GitHub Actions + Makefile)
b. Relationships & interactions
1. Client (e.g., curl, Postman, frontend) → HTTP request
2. API Server
– Loads config via util/config
– Passes request through middleware (authentication, validation)
– Calls token.Maker for login/signup or authorization checks
– Invokes Store methods to read/write data
3. Store (SQLC) → executes SQL against PostgreSQL
4. Database → returns rows → Store → API → HTTP response to client
5. CI/CD → on each push/PR runs tests, lint, build (configured in .github/workflows/ci.yaml & makefile)
c. Architectural patterns
– Layered architecture (Presentation → Application/Service → Data Access)
– Dependency Inversion: handlers depend on interfaces (store, token.Maker), allowing mock injection for tests
– Configuration via environment variables
– Code generation (SQLC) for safe, type‑checked DB access
d. Key technologies & frameworks
– Go standard library (net/http) or lightweight router in api/server.go
– SQLC for type‑safe SQL bindings
– PostgreSQL (migration scripts in db/migration)
– JWT and PASETO in token/
– GitHub Actions for CI

##### Elements
- Client → API Server
- API Server → Token Maker
- API Server → SQLC Store → PostgreSQL
- CI/CD → Build & Test → Deployment (optional)

##### Final recommendations
- Be exhaustive: break down the API layer into subcomponents (middleware, handlers) if detail is required.
- External dependencies (PostgreSQL, GitHub Actions).
- Important configurations (e.g., which env vars govern DB connection, token secrets).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Actor
    Client["Client"]:::external

    %% Configuration
    Config["Config Loader\n(util/config.go)"]:::infra
    click Config "https://github.com/rifkiilmi/golang-restfullapi/blob/main/util/config.go"

    %% API Layer
    subgraph "API Layer"
        direction TB
        Server["API Server\n(api/server.go)"]:::runtime
        click Server "https://github.com/rifkiilmi/golang-restfullapi/blob/main/api/server.go"
        Middleware["Middleware\n(api/middleware.go)"]:::runtime
        click Middleware "https://github.com/rifkiilmi/golang-restfullapi/blob/main/api/middleware.go"
        Validator["Validator\n(api/validator.go)"]:::runtime
        click Validator "https://github.com/rifkiilmi/golang-restfullapi/blob/main/api/validator.go"
        UserHandler["User Handler\n(api/user.go)"]:::runtime
        click UserHandler "https://github.com/rifkiilmi/golang-restfullapi/blob/main/api/user.go"
        AccountHandler["Account Handler\n(api/account.go)"]:::runtime
        click AccountHandler "https://github.com/rifkiilmi/golang-restfullapi/blob/main/api/account.go"
        TransferHandler["Transfer Handler\n(api/transfer.go)"]:::runtime
        click TransferHandler "https://github.com/rifkiilmi/golang-restfullapi/blob/main/api/transfer.go"
    end

    %% Business Logic / Data Access
    subgraph "Data Access Layer"
        direction TB
        Store["SQLC Store\n(db/sqlc/store.go)"]:::runtime
        click Store "https://github.com/rifkiilmi/golang-restfullapi/blob/main/db/sqlc/store.go"
        Querier["Querier\n(db/sqlc/querier.go)"]:::runtime
        click Querier "https://github.com/rifkiilmi/golang-restfullapi/blob/main/db/sqlc/querier.go"
        Models["Models\n(db/sqlc/models.go)"]:::runtime
        click Models "https://github.com/rifkiilmi/golang-restfullapi/blob/main/db/sqlc/models.go"
        MockStore["Mock Store\n(db/mock/store.go)"]:::runtime
        click MockStore "https://github.com/rifkiilmi/golang-restfullapi/blob/main/db/mock/store.go"
    end

    %% Token Service
    subgraph "Token Service"
        direction TB
        Maker["Token Maker\n(token/maker.go)"]:::runtime
        click Maker "https://github.com/rifkiilmi/golang-restfullapi/blob/main/token/maker.go"
        JWT["JWT Implementation\n(token/jwt_token.go)"]:::runtime
        click JWT "https://github.com/rifkiilmi/golang-restfullapi/blob/main/token/jwt_token.go"
        Paseto["PASETO Implementation\n(token/paseto_token.go)"]:::runtime
        click Paseto "https://github.com/rifkiilmi/golang-restfullapi/blob/main/token/paseto_token.go"
        Payload["Payload\n(token/payload.go)"]:::runtime
        click Payload "https://github.com/rifkiilmi/golang-restfullapi/blob/main/token/payload.go"
    end

    %% Utilities
    Utils["Utilities\n(util/config.go,\nutil/password.go,\nutil/random.go,\nutil/currency.go)"]:::infra
    click Utils "https://github.com/rifkiilmi/golang-restfullapi/blob/main/util/config.go"

    %% Database
    DB["PostgreSQL DB\n(db/migration)"]:::db

    %% CI/CD
    CICD["CI/CD Pipeline\n(.github/workflows/ci.yaml,\nmakefile)"]:::infra
    click CICD "https://github.com/rifkiilmi/golang-restfullapi/blob/main/.github/workflows/ci.yaml"

    %% Entrypoint
    Entrypoint["Service Entrypoint\n(main.go)"]:::infra
    click Entrypoint "https://github.com/rifkiilmi/golang-restfullapi/blob/main/main.go"

    %% Relationships
    Client -->|HTTP Request| Server
    Config --> Server
    Entrypoint --> Server
    Server --> Middleware
    Server --> Validator
    Server --> UserHandler
    Server --> AccountHandler
    Server --> TransferHandler
    Server --> Maker
    Server --> Store
    Store --> Querier
    Store --> Models
    Store --> DB
    Utils --> Server
    CICD --> Entrypoint
    CICD --> Server

    %% Styles
    classDef runtime fill:#e0f7fa,stroke:#00796b,stroke-width:2px
    classDef infra fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px
    classDef external fill:#f3e5f5,stroke:#6a1b9a,stroke-width:2px
    classDef db fill:#fff3e0,stroke:#ef6c00,stroke-width:2px
```
