# glitchmymind/five-rule-server architecture

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

- Interactive diagram: https://gitdiagram.com/glitchmymind/five-rule-server
- Repository: https://github.com/glitchmymind/five-rule-server

## Overview

##### Project type and purpose
- From the file tree and README we see:
- A Gradle-Kotlin project (build.gradle.kts, settings.gradle.kts)
- A Ktor application entry point (Application.kt) plus Routing, Authentication, Serialization plugins
- Feature packages for authentication, feed, rules
- Database models under db/models and DbSettings pointing at PostgreSQL (pg_hba.conf)
- A small in-memory cache (TimedCache) and OTP session management
- A Procfile hinting at deployment (e.g., Heroku)
⇒ This is a standalone backend microservice (Ktor + Koin DI) that exposes REST endpoints for authentication, feeds, rule management, token handling, and uses PostgreSQL and external OTP/FlashCall services.

##### File structure
Top-level:
- Application.kt – Ktor server bootstrap
- plugins/ – Ktor plugin registration (Routing, Authentication, Serialization)
- di/ – KoinModule DI definitions
- features/ – Modular domain logic, each with Data, Interactor, Routing subpackages
- db/ – Database configuration and Exposed table definitions (models)
- cache/ – In-memory timed cache abstraction
- sessions/ – OTP session store
- models/ – DTOs bridging domain→API
- resources/logback.xml – logging
- Procfile, system.properties – deployment

##### README for additional insights
- The only note is a Gist on token handling. This confirms an emphasis on secure token management and OTP flows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        direction TB
        HTTP["HTTP Server\n(Application.kt)"]:::app
        RoutingPlugin["Routing Plugin\n(Routing.kt)"]:::app
        AuthPlugin["Authentication Plugin\n(Authentication.kt)"]:::app
        SerPlugin["Serialization Plugin\n(Serialisation.kt)"]:::app
        DI["Koin DI Container\n(KoinModule.kt)"]:::app
    end

    %% Feature Modules
    subgraph "Feature Modules"
        direction TB
        subgraph "Authentication Feature"
            direction TB
            AuthRoute["Auth Routing\n(AuthRouting.kt)"]:::feature
            AuthInteractor["Auth Interactor\n(AuthInteractor.kt)"]:::feature
            AuthData["Auth Data Models\n(AuthData.kt)"]:::feature
            OtpAPI["OTP API Adapter\n(OtpApi.kt)"]:::external
            FlashCall["FlashCall Service\n(FlashCall.kt)"]:::external
        end
        subgraph "Feed Feature"
            direction TB
            FeedRoute["Feed Routing\n(FeedRouting.kt)"]:::feature
            FeedInteractor["Feed Interactor\n(FeedInteractor.kt)"]:::feature
            FeedData["Feed Data Models\n(FeedData.kt)"]:::feature
        end
        subgraph "Rules Feature"
            direction TB
            RulesRoute["Rules Routing\n(RulesRouting.kt)"]:::feature
            RulesInteractor["Rules Interactor\n(RulesInteractor.kt)"]:::feature
            RulesData["Rules Data Models\n(RulesData.kt)"]:::feature
        end
    end

    %% Infrastructure
    subgraph "Infrastructure"
        direction TB
        DbSettings["Database Config\n(DbSettings.kt)"]:::infra
        DB["PostgreSQL DB\n(Exposed Models)"]:::infra
        Cache["In-Memory Cache\n(TimedCache.kt)"]:::infra
        Sessions["OTP Session Store\n(OtpSessions.kt)"]:::infra
        GlobalConfig["Global Config\n(GlobalConfig.kt)"]:::infra
    end

    %% External Services
    subgraph "External Services"
        direction TB
        FlashCall
        OtpAPI
    end

    %% Deployment
    subgraph "Deployment Environment"
        direction TB
        Procfile["Procfile"]:::deploy
        JVMProp["system.properties"]:::deploy
    end

    %% Flows
    HTTP -->|"registers"| RoutingPlugin
    HTTP -->|"registers"| AuthPlugin
    HTTP -->|"registers"| SerPlugin
    HTTP -->|"uses"| DI

    RoutingPlugin -->|routes| AuthRoute
    RoutingPlugin -->|routes| FeedRoute
    RoutingPlugin -->|routes| RulesRoute

    AuthRoute -->|calls| AuthInteractor
    FeedRoute -->|calls| FeedInteractor
    RulesRoute -->|calls| RulesInteractor

    AuthInteractor -->|reads/writes| DbSettings
    AuthInteractor -->|reads/writes| DB
    AuthInteractor -->|uses cache| Cache
    AuthInteractor -->|uses sessions| Sessions
    AuthInteractor -->|invokes| OtpAPI
    OtpAPI -->|calls| FlashCall

    FeedInteractor -->|reads/writes| DbSettings
    FeedInteractor -->|reads/writes| DB
    FeedInteractor -->|uses cache| Cache

    RulesInteractor -->|reads/writes| DbSettings
    RulesInteractor -->|reads/writes| DB

    DI -->|provides| AuthInteractor
    DI -->|provides| FeedInteractor
    DI -->|provides| RulesInteractor
    DI -->|provides| DbSettings
    DI -->|provides| Cache
    DI -->|provides| Sessions
    DI -->|provides| GlobalConfig

    SerPlugin -->|serializes| AuthRoute
    SerPlugin -->|serializes| FeedRoute
    SerPlugin -->|serializes| RulesRoute

    GlobalConfig -->|configures| DbSettings
    GlobalConfig -->|provides| OtpAPI

    %% Deployment linkage
    Procfile --> HTTP
    JVMProp --> HTTP

    %% Click Events
    click HTTP "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/Application.kt"
    click RoutingPlugin "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/plugins/Routing.kt"
    click AuthPlugin "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/plugins/Authentication.kt"
    click SerPlugin "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/plugins/Serialisation.kt"
    click DI "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/di/KoinModule.kt"
    click AuthRoute "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/authentication/AuthRouting.kt"
    click AuthInteractor "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/authentication/AuthInteractor.kt"
    click AuthData "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/authentication/AuthData.kt"
    click OtpAPI "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/authentication/otp/OtpApi.kt"
    click FlashCall "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/authentication/otp/FlashCall.kt"
    click FeedRoute "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/feed/FeedRouting.kt"
    click FeedInteractor "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/feed/FeedInteractor.kt"
    click FeedData "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/feed/FeedData.kt"
    click RulesRoute "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/rules/RulesRouting.kt"
    click RulesInteractor "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/rules/RulesInteractor.kt"
    click RulesData "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/features/rules/RulesData.kt"
    click DbSettings "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/db/DbSettings.kt"
    click DB "https://github.com/glitchmymind/five-rule-server/tree/develop/src/main/kotlin/com/fiverules/db/models/"
    click Cache "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/cache/TimedCache.kt"
    click Sessions "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/sessions/OtpSessions.kt"
    click GlobalConfig "https://github.com/glitchmymind/five-rule-server/blob/develop/src/main/kotlin/com/fiverules/config/GlobalConfig.kt"
    click Procfile "https://github.com/glitchmymind/five-rule-server/tree/develop/Procfile"
    click JVMProp "https://github.com/glitchmymind/five-rule-server/blob/develop/system.properties"

    %% Styles
    classDef app fill:#D0E6FF,stroke:#1E75FF,color:#000
    classDef feature fill:#E8F5E9,stroke:#43A047,color:#000
    classDef infra fill:#FFF3E0,stroke:#FB8C00,color:#000
    classDef external fill:#FFE0B2,stroke:#FB8C00,color:#000
    classDef deploy fill:#F3E5F5,stroke:#8E24AA,color:#000
```
