# devfullcycle/imersao21 architecture

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

- Interactive diagram: https://gitdiagram.com/devfullcycle/imersao21
- Repository: https://github.com/devfullcycle/imersao21

## Overview

1. This repository is a 3-part home broker system:
   - a Go trading/matching engine,
   - a NestJS API layer,
   - a Next.js frontend.
   The architecture is split by responsibility: domain matching in Go, orchestration and I/O in NestJS, and user interaction in the browser.

2. The Go service is the core market engine. Its entrypoint is `go/cmd/trade/main.go`, and its domain package under `go/internal/market` models the trading book and matching logic with `asset`, `book`, `order`, `investor`, `transaction`, and `order_processor`. This is the component that appears to decide how orders are matched and how trades are produced.

3. Kafka is the main asynchronous boundary between the API and the Go engine. In Go, `internal/infra/kafka/consumer.go` and `producer.go` wrap broker communication. In NestJS, `src/kafka/confluent-kafka-server.ts` and `confluent-kafka-context.ts` implement the Kafka transport layer, and `src/orders/orders.consumer.ts` shows the API also consumes broker events.

4. The NestJS application is the system’s application/API layer. It is organized into feature modules for `assets`, `orders`, and `wallets`, each with controllers, services, DTOs, entities, and presenters. This is the layer that exposes HTTP endpoints and coordinates data flow between the UI, Kafka, and the domain services.

5. Within NestJS, `orders` is the most integration-heavy module. It has controllers, gateways, a consumer, DTOs for creating orders and trades, and entity classes for orders and trades. This suggests the order lifecycle is partially synchronous through HTTP and partially event-driven through Kafka and websockets.

6. The `assets` module handles market instruments and their time-series data. It includes an `asset-dailies` controller/service pair, a gateway, and presenter/entity classes for assets and daily price records. This indicates asset quotes and chart data are separate from order execution.

7. The `wallets` module represents user holdings. Its entities include a wallet and wallet-asset association, which is the persistence boundary for portfolio state. In the UI, wallet holdings are displayed as a dedicated list, so wallet updates are a first-class part of the user experience.

8. Real-time updates are delivered through websocket-style gateways. NestJS has `assets.gateway.ts` and `orders.gateway.ts`, while the frontend has `socket-io.ts`. This means the UI is not purely polling the API; it subscribes to live updates for prices, orders, and trade status.

9. The Next.js frontend is the presentation layer. Routes under `src/app` cover the home page, assets list, asset detail pages, and orders. Components like `OrderForm`, `WalletList`, `ChartComponent`, and `AssetShow` compose the screens, while `queries/queries.tsx` and `store.ts` support data access and client state.

10. The frontend is designed around market visualization and trading actions. It renders asset tables, asset charts, current prices, order status/type badges, and wallet balances. This is the user-facing boundary where market data, order submission, and execution feedback come together.

11. Data flow, at a high level, is:
   user action in Next.js → NestJS HTTP/API or websocket layer → Kafka eventing → Go matching engine → trade/order results back through Kafka → NestJS updates domain state and pushes live updates to the frontend.

12. The repo also includes operational scaffolding. Each subproject has its own `docker-compose.yaml` or Docker-related files, and the root README explicitly mentions an “all-in-docker” branch. The presence of a MongoDB Docker setup inside `nestjs-api/.docker/mongodb` suggests persistence for the API-side domain data is containerized as part of the deployment story.

13. Technically, this is a polyglot system: Go for the trading engine, TypeScript/NestJS for backend orchestration, and TypeScript/React/Next.js for the UI. The architectural diagram should therefore show clear service boundaries rather than a single monolith, with Kafka and websockets as the main integration channels.

Main entry points: [`nestjs-api/src/orders`](https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/orders) (Orders module), [`nestjs-api/src/app.module.ts`](https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/app.module.ts) (App module), [`go/internal/market`](https://github.com/devfullcycle/imersao21/tree/main/go/internal/market) (Market core), [`nestjs-api/src/assets`](https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/assets) (Assets module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_go["Go engine"]
  node_go_main["Trade entry<br/>Go cmd<br/>[main.go]"]
  node_go_market{{"Market core<br/>Go domain"}}
  node_go_book["Order book<br/>Go entity<br/>[book.go]"]
  node_go_order["Order model<br/>Go entity<br/>[order.go]"]
  node_go_processor{{"Matcher<br/>Go service<br/>[order_processor.go]"}}
  node_go_transaction["Trade record<br/>Go entity<br/>[transaction.go]"]
  node_go_kafka["Kafka adapter<br/>Go infra"]
end

subgraph group_group_nest["NestJS API"]
  node_nest_main["API entry<br/>NestJS bootstrap<br/>[main.ts]"]
  node_nest_app["App module<br/>NestJS root<br/>[app.module.ts]"]
  node_nest_kafka["Kafka transport<br/>NestJS infra"]
  node_nest_orders{{"Orders module<br/>NestJS feature"}}
  node_nest_assets{{"Assets module<br/>NestJS feature"}}
  node_nest_wallets{{"Wallets module<br/>NestJS feature"}}
  node_nest_orders_consumer["Order consumer<br/>NestJS consumer<br/>[orders.consumer.ts]"]
  node_nest_orders_gateway["Orders gateway<br/>NestJS gateway<br/>[orders.gateway.ts]"]
  node_nest_assets_gateway["Assets gateway<br/>NestJS gateway<br/>[assets.gateway.ts]"]
end

subgraph group_group_frontend["Next.js UI"]
  node_ui_home["Home view<br/>Next page<br/>[page.tsx]"]
  node_ui_assets["Assets view<br/>Next page<br/>[page.tsx]"]
  node_ui_asset_detail["Asset detail<br/>Next page<br/>[page.tsx]"]
  node_ui_orders["Orders view<br/>Next page<br/>[page.tsx]"]
  node_ui_wallets["Wallet list<br/>UI component<br/>[WalletList.tsx]"]
  node_ui_order_form["Order form<br/>UI component<br/>[OrderForm.tsx]"]
  node_ui_chart["Chart view<br/>UI component<br/>[ChartComponent.tsx]"]
  node_ui_socket["Socket client<br/>UI infra<br/>[socket-io.ts]"]
  node_ui_queries["Data queries<br/>UI data<br/>[queries.tsx]"]
  node_ui_store["Client store<br/>UI state<br/>[store.ts]"]
end

node_go_main -->|"boots"| node_go_market
node_go_market -->|"uses"| node_go_book
node_go_market -->|"models"| node_go_order
node_go_market -->|"matches"| node_go_processor
node_go_processor -->|"produces"| node_go_transaction
node_go_main -->|"connects"| node_go_kafka
node_nest_main -->|"boots"| node_nest_app
node_nest_app -->|"imports"| node_nest_orders
node_nest_app -->|"imports"| node_nest_assets
node_nest_app -->|"imports"| node_nest_wallets
node_nest_app -->|"wires"| node_nest_kafka
node_nest_kafka -->|"drives"| node_nest_orders_consumer
node_nest_orders -->|"publishes"| node_nest_orders_gateway
node_nest_assets -->|"publishes"| node_nest_assets_gateway
node_nest_orders -->|"consumes"| node_nest_orders_consumer
node_nest_orders -->|"syncs"| node_go_kafka
node_go_kafka -->|"bridges"| node_nest_kafka
node_nest_orders -.->|"coordinates"| node_nest_assets
node_nest_orders -.->|"updates"| node_nest_wallets
node_ui_home -->|"navigates"| node_ui_assets
node_ui_assets -->|"opens"| node_ui_asset_detail
node_ui_orders -->|"contains"| node_ui_order_form
node_ui_asset_detail -->|"renders"| node_ui_chart
node_ui_orders -->|"shows"| node_ui_wallets
node_ui_socket -->|"subscribes"| node_nest_orders_gateway
node_ui_socket -->|"subscribes"| node_nest_assets_gateway
node_ui_queries -->|"fetches"| node_nest_orders
node_ui_queries -->|"fetches"| node_nest_assets
node_ui_queries -->|"fetches"| node_nest_wallets
node_ui_store -.->|"shares state"| node_ui_orders
node_ui_store -.->|"shares state"| node_ui_assets

click node_go_main "https://github.com/devfullcycle/imersao21/blob/main/go/cmd/trade/main.go"
click node_go_market "https://github.com/devfullcycle/imersao21/tree/main/go/internal/market"
click node_go_book "https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/book.go"
click node_go_order "https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/order.go"
click node_go_processor "https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/order_processor.go"
click node_go_transaction "https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/transaction.go"
click node_go_kafka "https://github.com/devfullcycle/imersao21/tree/main/go/internal/infra/kafka"
click node_nest_main "https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/main.ts"
click node_nest_app "https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/app.module.ts"
click node_nest_kafka "https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/kafka"
click node_nest_orders "https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/orders"
click node_nest_assets "https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/assets"
click node_nest_wallets "https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/wallets"
click node_nest_orders_consumer "https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/orders/orders.consumer.ts"
click node_nest_orders_gateway "https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/orders/orders.gateway.ts"
click node_nest_assets_gateway "https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/assets/assets.gateway.ts"
click node_ui_home "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/page.tsx"
click node_ui_assets "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/assets/page.tsx"
click node_ui_asset_detail "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/assets/[assetSymbol]/page.tsx"
click node_ui_orders "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/orders/page.tsx"
click node_ui_wallets "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/components/WalletList.tsx"
click node_ui_order_form "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/components/OrderForm.tsx"
click node_ui_chart "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/components/ChartComponent.tsx"
click node_ui_socket "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/socket-io.ts"
click node_ui_queries "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/queries/queries.tsx"
click node_ui_store "https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/store.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_go_main,node_go_market,node_go_book,node_go_order,node_go_processor,node_go_transaction,node_go_kafka toneBlue
class node_nest_main,node_nest_app,node_nest_kafka,node_nest_orders,node_nest_assets,node_nest_wallets,node_nest_orders_consumer,node_nest_orders_gateway,node_nest_assets_gateway toneAmber
class node_ui_home,node_ui_assets,node_ui_asset_detail,node_ui_orders,node_ui_wallets,node_ui_order_form,node_ui_chart,node_ui_socket,node_ui_queries,node_ui_store toneMint
```

## Components

### Go engine

Trading and matching core

- [Trade entry](https://github.com/devfullcycle/imersao21/blob/main/go/cmd/trade/main.go): Go cmd · Starts the trading engine process · `go/cmd/trade/main.go`
- [Market core](https://github.com/devfullcycle/imersao21/tree/main/go/internal/market): Go domain · Order book and matching domain · `go/internal/market`
- [Order book](https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/book.go): Go entity · Tracks bids, asks, and match state · `go/internal/market/entity/book.go`
- [Order model](https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/order.go): Go entity · Represents incoming and resting orders · `go/internal/market/entity/order.go`
- [Matcher](https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/order_processor.go): Go service · Applies matching rules and produces trades · `go/internal/market/entity/order_processor.go`
- [Trade record](https://github.com/devfullcycle/imersao21/blob/main/go/internal/market/entity/transaction.go): Go entity · Produced trade and settlement data · `go/internal/market/entity/transaction.go`
- [Kafka adapter](https://github.com/devfullcycle/imersao21/tree/main/go/internal/infra/kafka): Go infra · Consumes and publishes broker events · `go/internal/infra/kafka`

### NestJS API

Application layer and integrations

- [API entry](https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/main.ts): NestJS bootstrap · Starts the API application · `nestjs-api/src/main.ts`
- [App module](https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/app.module.ts): NestJS root · Composes the application modules · `nestjs-api/src/app.module.ts`
- [Kafka transport](https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/kafka): NestJS infra · Custom Kafka server and context · `nestjs-api/src/kafka`
- [Orders module](https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/orders): NestJS feature · Order lifecycle, events, and live updates · `nestjs-api/src/orders`
- [Assets module](https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/assets): NestJS feature · Instruments, dailies, and market views · `nestjs-api/src/assets`
- [Wallets module](https://github.com/devfullcycle/imersao21/tree/main/nestjs-api/src/wallets): NestJS feature · Portfolio holdings and balances · `nestjs-api/src/wallets`
- [Order consumer](https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/orders/orders.consumer.ts): NestJS consumer · Consumes broker events from Kafka · `nestjs-api/src/orders/orders.consumer.ts`
- [Orders gateway](https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/orders/orders.gateway.ts): NestJS gateway · Pushes order status updates to clients · `nestjs-api/src/orders/orders.gateway.ts`
- [Assets gateway](https://github.com/devfullcycle/imersao21/blob/main/nestjs-api/src/assets/assets.gateway.ts): NestJS gateway · Pushes asset and price updates to clients · `nestjs-api/src/assets/assets.gateway.ts`

### Next.js UI

Browser presentation and interaction

- [Home view](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/page.tsx): Next page · Landing page and navigation entry · `next-frontend/src/app/page.tsx`
- [Assets view](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/assets/page.tsx): Next page · Lists assets and opens asset detail pages · `next-frontend/src/app/assets/page.tsx`
- [Asset detail](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/assets/[assetSymbol]/page.tsx): Next page · Shows chart and current asset price · `next-frontend/src/app/assets/[assetSymbol]/page.tsx`
- [Orders view](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/app/orders/page.tsx): Next page · Displays order actions and order state · `next-frontend/src/app/orders/page.tsx`
- [Wallet list](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/components/WalletList.tsx): UI component · Renders holdings and portfolio balance · `next-frontend/src/components/WalletList.tsx`
- [Order form](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/components/OrderForm.tsx): UI component · Submits buy and sell orders · `next-frontend/src/components/OrderForm.tsx`
- [Chart view](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/components/ChartComponent.tsx): UI component · Renders market charts and price history · `next-frontend/src/components/ChartComponent.tsx`
- [Socket client](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/socket-io.ts): UI infra · Receives live updates from the API · `next-frontend/src/socket-io.ts`
- [Data queries](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/queries/queries.tsx): UI data · Shared client-side data access · `next-frontend/src/queries/queries.tsx`
- [Client store](https://github.com/devfullcycle/imersao21/blob/main/next-frontend/src/store.ts): UI state · Shared browser state for the app · `next-frontend/src/store.ts`

## Connections

- Trade entry → Market core: boots · Entrypoint initializes the market engine
- Market core → Order book: uses · The market core maintains the order book
- Market core → Order model: models · Orders are represented inside the domain
- Market core → Matcher: matches · Matching logic drives execution
- Matcher → Trade record: produces · Matches generate trade transactions
- Trade entry → Kafka adapter: connects · Engine communicates through Kafka
- API entry → App module: boots · Application bootstrap loads the root module
- App module → Orders module: imports · Orders feature is part of the app
- App module → Assets module: imports · Assets feature is part of the app
- App module → Wallets module: imports · Wallets feature is part of the app
- App module → Kafka transport: wires · Kafka transport is registered in the app
- Kafka transport → Order consumer: drives · Kafka transport feeds the order consumer
- Orders module → Orders gateway: publishes · Orders module exposes live updates
- Assets module → Assets gateway: publishes · Assets module exposes live updates
- Orders module → Order consumer: consumes · Orders module processes broker events
- Orders module → Kafka adapter: syncs · Orders flow crosses the Kafka boundary
- Kafka adapter → Kafka transport: bridges · Kafka is the asynchronous boundary between services
- Orders module → Assets module: coordinates · Order execution depends on market data
- Orders module → Wallets module: updates · Executed orders affect portfolio state
- Home view → Assets view: navigates · Users move from landing page into market views
- Assets view → Asset detail: opens · Asset list links to detail pages
- Orders view → Order form: contains · Orders screen centers on order entry
- Asset detail → Chart view: renders · Detail view includes charting
- Orders view → Wallet list: shows · Orders experience surfaces portfolio holdings
- Socket client → Orders gateway: subscribes · Frontend receives live order updates
- Socket client → Assets gateway: subscribes · Frontend receives live asset updates
- Data queries → Orders module: fetches · Client queries the API for order data
- Data queries → Assets module: fetches · Client queries the API for asset data
- Data queries → Wallets module: fetches · Client queries the API for wallet data
- Client store → Orders view: shares state · Order screen consumes shared client state
- Client store → Assets view: shares state · Asset screens consume shared client state
