# fisiu/kosync-dotnet architecture

> Architecture diagram of the GitHub repository fisiu/kosync-dotnet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fisiu/kosync-dotnet
- Repository: https://github.com/fisiu/kosync-dotnet

## Overview

##### Project Overview
- The project is a backend web API service built on .NET. It implements a sync server for KOReader that allows users to register, manage, and synchronize reading progress across devices.
- It is designed for containerized deployment (using Docker) and uses a LiteDB database file to persist user and document progress information.
- The README also describes management API endpoints for listing users, creating users, toggling active status, and updating passwords.

##### File Structure and Architectural Components
- Controllers (Controllers/ManagementController.cs, Controllers/SyncController.cs): These files indicate two primary API controllers that expose endpoints for admin management and synchronization operations.
- Services (e.g., Services/UserService.cs): This layer encapsulates business logic such as handling user operations. Controllers are expected to call these services.
- Models (e.g., Models/DocumentRequest.cs and others): These represent the data transfer objects (DTOs) used in communication between the controllers and the services.
- Database (Database/KosyncDb.cs, Database/Entities/*.cs): Contains database context and entity definitions modeled for LiteDB.
- GlobalUsings.cs and Program.cs: Point to modern .NET .NET Core implementation structure with startup configuration.
- Configuration files (appsettings.json, appsettings.Development.json) and Dockerfile: Provide important configuration for running the application, including environment-specific options and containerization support.
- Additional files (.github workflows, .gitignore, and Dockerfile) show the CI/CD integration and deployment configuration.

##### Key Components
a. API Layer:
- Controllers (ManagementController and SyncController) that expose HTTP endpoints for synchronization and management.
- A note that clients (KOReader’s syncing plugin, Postman for management interactions) interact with these endpoints.
b. Service Layer:
- A Services component (with UserService & possibly others) that encapsulates the core business logic.
c. Data Access Layer:
- Database component that includes:
- Entities (Document, User, etc.) as models.
- The KosyncDb abstracting the database interactions using LiteDB.
d. Configuration / External Integration:
- Configuration settings (appsettings.json/environment variables) that affect logging, connection details, and runtime behavior.
- How Docker (via the Dockerfile and provided CI/CD workflow files) is used to create a containerized version of the API server.
e. External Clients / Reverse Proxy:
- External clients: KOReader devices that use the inbuilt “Progress sync” plugin, and admins using Postman or similar tools to manage the API.
- That a reverse proxy (such as Nginx Proxy Manager) is recommended to route traffic from the internet to the container’s port 80.

##### Architectural Patterns and Design Principles
- The project clearly follows an MVC-like separation tailored for API design:
- Controllers: handling HTTP requests.
- Service layer: encapsulating business logic.
- Data access layer: interfacing with the LiteDB database.
- Separation of concerns, where each layer has its distinct responsibility.
- The usage of Docker indicates a containerized micro-deployment even if the project is a single monolith.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Section
    subgraph "Clients"
        koreader["KOReader Device"]:::clients
        postman["Admin/Postman"]:::clients
    end

    %% Docker Environment / Reverse Proxy Section
    subgraph "Docker Environment/Reverse Proxy"
        docker["Docker Container"]:::infra
        reverseProxy["Nginx Proxy Manager"]:::infra
    end

    %% API Server Section
    subgraph "API Server"
        %% API Layer (Controllers)
        subgraph "API Layer"
            mgmt["ManagementController"]:::controllers
            sync["SyncController"]:::controllers
        end

        %% Service Layer (Business Logic)
        subgraph "Service Layer"
            service["UserService"]:::services
        end

        %% Data Access Layer
        subgraph "Data Access Layer"
            db["KosyncDb (LiteDB)"]:::database
            subgraph "Entities"
                user["User Entity"]:::database
                document["Document Entity"]:::database
            end
        end

        %% Models / DTOs
        subgraph "Models (DTOs)"
            docReq["DocumentRequest"]:::models
            pwdReq["PasswordChangeRequest"]:::models
            userReq["UserCreateRequest"]:::models
        end

        %% Startup Configuration
        subgraph "Startup Configuration"
            globalUsings["GlobalUsings"]:::startup
            program["Program"]:::startup
        end
    end

    %% Configuration Files and CI/CD
    subgraph "Configuration Files"
        config1["appsettings.json"]:::config
        config2["appsettings.Development.json"]:::config
        cicd["CI/CD Workflow"]:::config
    end

    %% Connections: Clients to API Layer
    koreader -->|"HTTPSync"| sync
    postman -->|"HTTPManage"| mgmt

    %% Connections: API Layer to Service Layer
    mgmt -->|"calls"| service
    sync -->|"calls"| service

    %% Connection: Service Layer to Data Access Layer
    service -->|"DB_Operations"| db

    %% Indicating that database contains the entities
    db -->|"contains"| user
    db -->|"contains"| document

    %% Connection: Service Layer to Configuration Files
    service -->|"reads"| config1
    service -->|"reads"| config2

    %% Connections: Docker Environment influences API Server
    docker -->|"hosts"| mgmt
    reverseProxy -->|"proxies"| mgmt

    %% Styling for clarity (colors)
    classDef controllers fill:#f9c,stroke:#333,stroke-width:2px;
    classDef services fill:#ccf,stroke:#333,stroke-width:2px;
    classDef database fill:#cfc,stroke:#333,stroke-width:2px;
    classDef models fill:#fcf,stroke:#333,stroke-width:2px;
    classDef config fill:#ffd,stroke:#333,stroke-width:2px;
    classDef infra fill:#def,stroke:#333,stroke-width:2px;
    classDef startup fill:#eef,stroke:#333,stroke-width:2px;
    classDef clients fill:#efe,stroke:#333,stroke-width:2px;

    %% Click Events for API Layer Controllers
    click mgmt "https://github.com/fisiu/kosync-dotnet/blob/main/Controllers/ManagementController.cs"
    click sync "https://github.com/fisiu/kosync-dotnet/blob/main/Controllers/SyncController.cs"

    %% Click Event for Service Layer
    click service "https://github.com/fisiu/kosync-dotnet/blob/main/Services/UserService.cs"

    %% Click Events for Data Access Layer
    click db "https://github.com/fisiu/kosync-dotnet/blob/main/Database/KosyncDb.cs"
    click user "https://github.com/fisiu/kosync-dotnet/blob/main/Database/Entities/User.cs"
    click document "https://github.com/fisiu/kosync-dotnet/blob/main/Database/Entities/Document.cs"

    %% Click Events for Models / DTOs
    click docReq "https://github.com/fisiu/kosync-dotnet/blob/main/Models/DocumentRequest.cs"
    click pwdReq "https://github.com/fisiu/kosync-dotnet/blob/main/Models/PasswordChangeRequest.cs"
    click userReq "https://github.com/fisiu/kosync-dotnet/blob/main/Models/UserCreateRequest.cs"

    %% Click Events for Configuration Files and CI/CD
    click config1 "https://github.com/fisiu/kosync-dotnet/blob/main/appsettings.json"
    click config2 "https://github.com/fisiu/kosync-dotnet/blob/main/appsettings.Development.json"
    click cicd "https://github.com/fisiu/kosync-dotnet/blob/main/.github/workflows/docker-publish.yml"

    %% Click Events for Startup Configuration
    click globalUsings "https://github.com/fisiu/kosync-dotnet/blob/main/GlobalUsings.cs"
    click program "https://github.com/fisiu/kosync-dotnet/blob/main/Program.cs"
```
