# bt2701/facebook-be-v2 architecture

> Architecture diagram of the GitHub repository bt2701/facebook-be-v2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bt2701/facebook-be-v2
- Repository: https://github.com/bt2701/facebook-be-v2

## Overview

Facebook Backend V2 is a Kong-fronted Go microservice backend for a social platform. A typical request enters through the gateway and a service’s inbound adapter, passes through its application layer, and reaches a repository. Product workflows include account/authentication, posts and interactions, friendships, messaging, media, notifications, games, and an AI chatbox. The README documents Redis events from friend and post actions to notification persistence, and friend suggestions calling user-service. Most service internals were not sampled; their links follow the documented adapter pattern, while uncertain cross-service wiring is omitted.

Main entry points: [`friend-service/internal/app/service`](https://github.com/bt2701/facebook-be-v2/tree/main/friend-service/internal/app/service) (Friend services), [`post-service/internal/app/service`](https://github.com/bt2701/facebook-be-v2/tree/main/post-service/internal/app/service) (Post services), [`ai-service/internal/app/service/ai_service.go`](https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/internal/app/service/ai_service.go) (AI service), [`user-service/internal/adapters/inbound`](https://github.com/bt2701/facebook-be-v2/tree/main/user-service/internal/adapters/inbound) (User API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_accounts["Accounts and friends"]
  node_user_api["User API"]
  node_user_app["User service"]
  node_user_repo["User repository<br/>[user_repository.go]"]
  node_user_db[("User database<br/>[db.go]")]
  node_friend_api["Friend APIs"]
  node_friend_app["Friend services"]
  node_friend_repo["Friend repositories"]
  node_friend_db[("Friend database<br/>[mongodb.go]")]
end

subgraph group_social["Social content"]
  node_post_api["Post APIs"]
  node_post_app["Post services"]
  node_post_repo["Post repositories"]
  node_post_db[("Post database<br/>[mongodb.go]")]
  node_notification_api["Notification API"]
  node_notification_app["Notification service"]
  node_notification_repo["Notification repository"]
  node_notification_db[("Notification database<br/>[mongodb.go]")]
end

subgraph group_messaging["Messaging and AI"]
  node_chat_api["Chat APIs"]
  node_chat_app["Chat service<br/>[chat_service.go]"]
  node_chat_repo["Message repository"]
  node_chat_db[("Chat database<br/>[mongodb.go]")]
  node_ai_api["AI chat API<br/>[ai_handler.go]"]
  node_ai_app["AI service<br/>[ai_service.go]"]
  node_conversation_repo["Conversation storage"]
  node_ai_db[("AI database<br/>[mongodb.go]")]
  node_llm{{"LLM provider"}}
end

subgraph group_platform["Media and games"]
  node_media_api["Image API<br/>[image_handler.go]"]
  node_media_app["Image service<br/>[image_service.go]"]
  node_media_repo["Image repository"]
  node_media_db[("Media database<br/>[db.go]")]
  node_game_api["Game APIs"]
  node_game_app["Game services"]
end

subgraph group_shared["Events and gateway"]
  node_kong{{"Kong gateway"}}
  node_redis_events["Redis events<br/>[bus.go]"]
end

node_client(("Frontend client"))

node_client -->|"sends requests"| node_kong
node_kong -->|"routes requests"| node_user_api
node_kong -->|"routes requests"| node_friend_api
node_kong -->|"routes requests"| node_post_api
node_kong -->|"routes requests"| node_notification_api
node_kong -->|"routes requests"| node_chat_api
node_kong -->|"routes requests"| node_ai_api
node_kong -->|"routes requests"| node_media_api
node_kong -->|"routes requests"| node_game_api
node_user_api -->|"invokes"| node_user_app
node_user_app -->|"calls"| node_user_repo
node_user_repo -->|"reads/writes"| node_user_db
node_friend_api -->|"invokes"| node_friend_app
node_friend_app -->|"calls"| node_friend_repo
node_friend_repo -->|"reads/writes"| node_friend_db
node_friend_app -->|"fetches suggestions"| node_user_api
node_post_api -->|"invokes"| node_post_app
node_post_app -->|"calls"| node_post_repo
node_post_repo -->|"reads/writes"| node_post_db
node_notification_api -->|"invokes"| node_notification_app
node_notification_app -->|"calls"| node_notification_repo
node_notification_repo -->|"reads/writes"| node_notification_db
node_friend_app -->|"publishes actions"| node_redis_events
node_post_app -->|"publishes actions"| node_redis_events
node_redis_events -->|"delivers events"| node_notification_app
node_chat_api -->|"invokes"| node_chat_app
node_chat_app -->|"calls"| node_chat_repo
node_chat_repo -->|"reads/writes"| node_chat_db
node_ai_api -->|"invokes"| node_ai_app
node_ai_app -->|"calls"| node_conversation_repo
node_conversation_repo -->|"reads/writes"| node_ai_db
node_ai_app -.->|"requests completion"| node_llm
node_media_api -->|"invokes"| node_media_app
node_media_app -->|"calls"| node_media_repo
node_media_repo -->|"reads/writes"| node_media_db
node_game_api -->|"invokes"| node_game_app

click node_user_api "https://github.com/bt2701/facebook-be-v2/tree/main/user-service/internal/adapters/inbound"
click node_user_app "https://github.com/bt2701/facebook-be-v2/tree/main/user-service/internal/app/services"
click node_user_repo "https://github.com/bt2701/facebook-be-v2/blob/main/user-service/internal/adapters/outbound/user_repository.go"
click node_user_db "https://github.com/bt2701/facebook-be-v2/blob/main/user-service/pkg/database/db.go"
click node_friend_api "https://github.com/bt2701/facebook-be-v2/tree/main/friend-service/internal/adapters/inbound"
click node_friend_app "https://github.com/bt2701/facebook-be-v2/tree/main/friend-service/internal/app/service"
click node_friend_repo "https://github.com/bt2701/facebook-be-v2/tree/main/friend-service/internal/adapters/outbound"
click node_friend_db "https://github.com/bt2701/facebook-be-v2/blob/main/friend-service/pkg/database/mongodb.go"
click node_post_api "https://github.com/bt2701/facebook-be-v2/tree/main/post-service/internal/adapters/inbound"
click node_post_app "https://github.com/bt2701/facebook-be-v2/tree/main/post-service/internal/app/service"
click node_post_repo "https://github.com/bt2701/facebook-be-v2/tree/main/post-service/internal/adapters/outbound"
click node_post_db "https://github.com/bt2701/facebook-be-v2/blob/main/post-service/pkg/database/mongodb.go"
click node_notification_api "https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/internal/adapters/inbound/notification_handler.go"
click node_notification_app "https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/internal/app/services/notification_service.go"
click node_notification_repo "https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/internal/adapters/outbound/notification_repository.go"
click node_notification_db "https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/pkg/database/mongodb.go"
click node_redis_events "https://github.com/bt2701/facebook-be-v2/blob/main/shared/events/bus.go"
click node_chat_api "https://github.com/bt2701/facebook-be-v2/tree/main/chat-service/internal/adapters/in"
click node_chat_app "https://github.com/bt2701/facebook-be-v2/blob/main/chat-service/internal/app/service/chat_service.go"
click node_chat_repo "https://github.com/bt2701/facebook-be-v2/blob/main/chat-service/internal/adapters/out/db_message_repository.go"
click node_chat_db "https://github.com/bt2701/facebook-be-v2/blob/main/chat-service/pkg/database/mongodb.go"
click node_ai_api "https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/internal/adapters/inbound/ai_handler.go"
click node_ai_app "https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/internal/app/service/ai_service.go"
click node_conversation_repo "https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/internal/adapters/outbound/conversation_repository.go"
click node_ai_db "https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/pkg/database/mongodb.go"
click node_media_api "https://github.com/bt2701/facebook-be-v2/blob/main/media-service/internal/adapters/inbound/image_handler.go"
click node_media_app "https://github.com/bt2701/facebook-be-v2/blob/main/media-service/internal/app/services/image_service.go"
click node_media_repo "https://github.com/bt2701/facebook-be-v2/blob/main/media-service/internal/adapters/outbound/image_repository.go"
click node_media_db "https://github.com/bt2701/facebook-be-v2/blob/main/media-service/pkg/database/db.go"
click node_game_api "https://github.com/bt2701/facebook-be-v2/tree/main/game-service/internal/adapters/inbound"
click node_game_app "https://github.com/bt2701/facebook-be-v2/tree/main/game-service/internal/app/service"

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_user_api,node_user_app,node_user_repo,node_user_db,node_friend_api,node_friend_app,node_friend_repo,node_friend_db,node_client toneBlue
class node_post_api,node_post_app,node_post_repo,node_post_db,node_notification_api,node_notification_app,node_notification_repo,node_notification_db toneAmber
class node_chat_api,node_chat_app,node_chat_repo,node_chat_db,node_ai_api,node_ai_app,node_conversation_repo,node_ai_db,node_llm toneMint
class node_media_api,node_media_app,node_media_repo,node_media_db,node_game_api,node_game_app toneRose
class node_kong,node_redis_events toneIndigo
```

## Components

### Accounts and friends

- [User API](https://github.com/bt2701/facebook-be-v2/tree/main/user-service/internal/adapters/inbound): `user-service/internal/adapters/inbound`
- [User service](https://github.com/bt2701/facebook-be-v2/tree/main/user-service/internal/app/services): `user-service/internal/app/services`
- [User repository](https://github.com/bt2701/facebook-be-v2/blob/main/user-service/internal/adapters/outbound/user_repository.go): `user-service/internal/adapters/outbound/user_repository.go`
- [User database](https://github.com/bt2701/facebook-be-v2/blob/main/user-service/pkg/database/db.go): `user-service/pkg/database/db.go`
- [Friend APIs](https://github.com/bt2701/facebook-be-v2/tree/main/friend-service/internal/adapters/inbound): `friend-service/internal/adapters/inbound`
- [Friend services](https://github.com/bt2701/facebook-be-v2/tree/main/friend-service/internal/app/service): `friend-service/internal/app/service`
- [Friend repositories](https://github.com/bt2701/facebook-be-v2/tree/main/friend-service/internal/adapters/outbound): `friend-service/internal/adapters/outbound`
- [Friend database](https://github.com/bt2701/facebook-be-v2/blob/main/friend-service/pkg/database/mongodb.go): `friend-service/pkg/database/mongodb.go`

### Social content

- [Post APIs](https://github.com/bt2701/facebook-be-v2/tree/main/post-service/internal/adapters/inbound): `post-service/internal/adapters/inbound`
- [Post services](https://github.com/bt2701/facebook-be-v2/tree/main/post-service/internal/app/service): `post-service/internal/app/service`
- [Post repositories](https://github.com/bt2701/facebook-be-v2/tree/main/post-service/internal/adapters/outbound): `post-service/internal/adapters/outbound`
- [Post database](https://github.com/bt2701/facebook-be-v2/blob/main/post-service/pkg/database/mongodb.go): `post-service/pkg/database/mongodb.go`
- [Notification API](https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/internal/adapters/inbound/notification_handler.go): `notification-service/internal/adapters/inbound/notification_handler.go`
- [Notification service](https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/internal/app/services/notification_service.go): `notification-service/internal/app/services/notification_service.go`
- [Notification repository](https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/internal/adapters/outbound/notification_repository.go): `notification-service/internal/adapters/outbound/notification_repository.go`
- [Notification database](https://github.com/bt2701/facebook-be-v2/blob/main/notification-service/pkg/database/mongodb.go): `notification-service/pkg/database/mongodb.go`

### Messaging and AI

- [Chat APIs](https://github.com/bt2701/facebook-be-v2/tree/main/chat-service/internal/adapters/in): `chat-service/internal/adapters/in`
- [Chat service](https://github.com/bt2701/facebook-be-v2/blob/main/chat-service/internal/app/service/chat_service.go): `chat-service/internal/app/service/chat_service.go`
- [Message repository](https://github.com/bt2701/facebook-be-v2/blob/main/chat-service/internal/adapters/out/db_message_repository.go): `chat-service/internal/adapters/out/db_message_repository.go`
- [Chat database](https://github.com/bt2701/facebook-be-v2/blob/main/chat-service/pkg/database/mongodb.go): `chat-service/pkg/database/mongodb.go`
- [AI chat API](https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/internal/adapters/inbound/ai_handler.go): `ai-service/internal/adapters/inbound/ai_handler.go`
- [AI service](https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/internal/app/service/ai_service.go): `ai-service/internal/app/service/ai_service.go`
- [Conversation storage](https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/internal/adapters/outbound/conversation_repository.go): `ai-service/internal/adapters/outbound/conversation_repository.go`
- [AI database](https://github.com/bt2701/facebook-be-v2/blob/main/ai-service/pkg/database/mongodb.go): `ai-service/pkg/database/mongodb.go`
- **LLM provider**

### Media and games

- [Image API](https://github.com/bt2701/facebook-be-v2/blob/main/media-service/internal/adapters/inbound/image_handler.go): `media-service/internal/adapters/inbound/image_handler.go`
- [Image service](https://github.com/bt2701/facebook-be-v2/blob/main/media-service/internal/app/services/image_service.go): `media-service/internal/app/services/image_service.go`
- [Image repository](https://github.com/bt2701/facebook-be-v2/blob/main/media-service/internal/adapters/outbound/image_repository.go): `media-service/internal/adapters/outbound/image_repository.go`
- [Media database](https://github.com/bt2701/facebook-be-v2/blob/main/media-service/pkg/database/db.go): `media-service/pkg/database/db.go`
- [Game APIs](https://github.com/bt2701/facebook-be-v2/tree/main/game-service/internal/adapters/inbound): `game-service/internal/adapters/inbound`
- [Game services](https://github.com/bt2701/facebook-be-v2/tree/main/game-service/internal/app/service): `game-service/internal/app/service`

### Events and gateway

- **Kong gateway**
- [Redis events](https://github.com/bt2701/facebook-be-v2/blob/main/shared/events/bus.go): `shared/events/bus.go`

### Other components

- **Frontend client**

## Connections

- Frontend client → Kong gateway: sends requests
- Kong gateway → User API: routes requests
- Kong gateway → Friend APIs: routes requests
- Kong gateway → Post APIs: routes requests
- Kong gateway → Notification API: routes requests
- Kong gateway → Chat APIs: routes requests
- Kong gateway → AI chat API: routes requests
- Kong gateway → Image API: routes requests
- Kong gateway → Game APIs: routes requests
- User API → User service: invokes
- User service → User repository: calls
- User repository → User database: reads/writes
- Friend APIs → Friend services: invokes
- Friend services → Friend repositories: calls
- Friend repositories → Friend database: reads/writes
- Friend services → User API: fetches suggestions
- Post APIs → Post services: invokes
- Post services → Post repositories: calls
- Post repositories → Post database: reads/writes
- Notification API → Notification service: invokes
- Notification service → Notification repository: calls
- Notification repository → Notification database: reads/writes
- Friend services → Redis events: publishes actions
- Post services → Redis events: publishes actions
- Redis events → Notification service: delivers events
- Chat APIs → Chat service: invokes
- Chat service → Message repository: calls
- Message repository → Chat database: reads/writes
- AI chat API → AI service: invokes
- AI service → Conversation storage: calls
- Conversation storage → AI database: reads/writes
- AI service → LLM provider: requests completion
- Image API → Image service: invokes
- Image service → Image repository: calls
- Image repository → Media database: reads/writes
- Game APIs → Game services: invokes
