# codegod100/runes architecture

> Architecture diagram of the GitHub repository codegod100/runes, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/codegod100/runes
- Repository: https://github.com/codegod100/runes

## Overview

##### Main Components

a. Frontend Layer:
- Svelte Components (src/routes directory)
- Client-side Authentication Handler (src/lib/auth/client.ts)
- WebSocket Client (src/lib/websocket.ts)

b. Backend Layer:
- Server-side Routes (API endpoints in src/routes/api)
- OAuth Authentication System (src/lib/oauth.ts)
- Database Interface (src/lib/db.ts)

c. External Services:
- OAuth Provider Integration
- File Upload Service (indicated by /upload route)
- Chat Server Integration

##### Component Relationships

- Frontend → Backend: Show API calls and WebSocket connections
- Backend → External Services: OAuth flow and data persistence
- Backend → Database: Data storage and retrieval operations

##### Architectural Patterns to Highlight

a. SvelteKit Architecture:
- Server-side rendering capabilities
- API routes structure
- Page-based routing system

b. Authentication Flow:
- OAuth implementation
- Session management
- Token handling

##### Specific Implementation Details

- Blue rectangles: Core application components
- Green diamonds: External services
- Red lines: Authentication flows
- Blue lines: Data flows
- Purple lines: WebSocket connections

**[Client Browser] → [SvelteKit Frontend]**
- Pages (/*.svelte)
- API Clients
- WebSocket Client

**[API Layer] → [Core Services]**
- Auth Service
- Chat Service
- File Upload Service

**[OAuth Provider]**
**[Chat Server]**
**[File Storage]**

##### Important Flows to Highlight

- Authentication Flow:
- Client → OAuth Routes → External OAuth Provider → Callback → Session Storage
- Chat Flow:
- Client → WebSocket → Chat Server → Message Distribution
- File Upload Flow:
- Client → Upload Route → Storage Service → URL Return

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        Browser["Web Browser"]
        FrontendPages["Frontend Pages"]:::frontend
        AuthClient["Auth Client"]:::frontend
        WSClient["WebSocket Client"]:::frontend
        TypeSystem["Type System (Lexicon)"]:::shared
    end

    subgraph Server
        direction TB
        APILayer["API Layer"]:::backend
        AuthService["Auth Service"]:::backend
        ChatService["Chat Service"]:::backend
        UploadService["File Upload Service"]:::backend
        ServerHooks["Server Hooks"]:::backend
        DBInterface["Database Interface"]:::backend
        EnvConfig["Environment Config"]:::backend
        ChannelMgmt["Channel Management"]:::backend
    end

    subgraph External
        OAuth["OAuth Provider"]:::external
        Storage["File Storage"]:::external
        ChatServer["Chat Server"]:::external
    end

    %% Client Connections
    Browser --> FrontendPages
    FrontendPages --> AuthClient
    FrontendPages --> WSClient
    FrontendPages --> TypeSystem

    %% Server Connections
    APILayer --> AuthService
    APILayer --> ChatService
    APILayer --> UploadService
    ServerHooks --> AuthService
    AuthService --> DBInterface
    ChatService --> DBInterface
    UploadService --> Storage
    ChatService --> ChannelMgmt
    ChannelMgmt --> ChatServer

    %% External Connections
    AuthService --> OAuth
    UploadService --> Storage

    %% Cross-layer Connections
    FrontendPages --> APILayer
    WSClient --> ChatServer
    AuthClient --> AuthService

    %% Styles
    classDef frontend fill:#a8d5ff,stroke:#333,stroke-width:2px
    classDef backend fill:#ffb366,stroke:#333,stroke-width:2px
    classDef external fill:#90EE90,stroke:#333,stroke-width:2px
    classDef shared fill:#DDA0DD,stroke:#333,stroke-width:2px

    %% Click Events
    click AuthClient "https://github.com/codegod100/runes/blob/main/src/lib/auth/client.ts"
    click WSClient "https://github.com/codegod100/runes/blob/main/src/lib/websocket.ts"
    click DBInterface "https://github.com/codegod100/runes/blob/main/src/lib/db.ts"
    click AuthService "https://github.com/codegod100/runes/blob/main/src/lib/oauth.ts"
    click FrontendPages "https://github.com/codegod100/runes/blob/main/src/routes/+page.svelte"
    click ServerHooks "https://github.com/codegod100/runes/blob/main/src/hooks.server.ts"
    click UploadService "https://github.com/codegod100/runes/blob/main/src/routes/upload/+page.server.ts"
    click TypeSystem "https://github.com/codegod100/runes/blob/main/src/lib/lexicon/index.ts"
    click EnvConfig "https://github.com/codegod100/runes/blob/main/src/lib/env.ts"
    click ChannelMgmt "https://github.com/codegod100/runes/blob/main/src/lib/channel_map.ts"

    %% Legend
    subgraph Legend
        Frontend["Frontend Component"]:::frontend
        Backend["Backend Component"]:::backend
        ExternalSvc["External Service"]:::external
        SharedComp["Shared Component"]:::shared
    end
```
