# nsfoxer/cnblog architecture

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

- Interactive diagram: https://gitdiagram.com/nsfoxer/cnblog
- Repository: https://github.com/nsfoxer/cnblog

## Overview

##### Project Type and Purpose
- Type: Open-source CLI synchronization tool
- Purpose: Scan a local directory of Markdown files, store metadata in SQLite, Base64-encode the DB, and push/pull blog posts through the MetaWeblog API on Cnblog

##### File Structure
- Top level:
- Cargo.toml / Cargo.lock – Rust manifest & dependency lock
- README.md – project description & usage
- src/main.rs – CLI entry point
- src/lib.rs – library exports & orchestrator
- src/meta_weblog/ – module handling config, RPC, and weblog logic
· cfg.rs – config file parsing & storage path
· rpc.rs – HTTP client for MetaWeblog endpoints
· weblog.rs – blog-specific data structures & sync logic
- Implication: Clear separation between CLI, configuration, RPC, business logic, and persistence

##### Extract Key Components from README
- Config folder (defaults to ~/.config/cnblog/)
- CLI flags: `-c`, `-r`, `-h`, `-V`
- Workflow: scan directory → detect new/changed/deleted → update SQLite → base64-encode → upload via MetaWeblog → handle responses → move deleted posts locally

##### Main Components & Their Relationships
a. CLI Frontend (main.rs)
– Parses flags, invokes library functions
b. Config Manager (cfg.rs)
– Loads/saves JSON or TOML config (API keys, endpoints, local root path)
c. File Scanner (lib.rs / weblog.rs)
– Walks target directory, computes diffs (new/modified/deleted)
d. SQLite Storage
– Persists metadata: post IDs, timestamps, checksums
e. Base64 Encoder
– Encodes SQLite DB file before uploading as fallback storage
f. RPC Client (rpc.rs)
– Wraps XML-RPC calls to MetaWeblog API (newPost, editPost, getRecentPosts)
g. Cnblog Service (External)
– Remote blog host providing the MetaWeblog endpoint
h. Local Filesystem
– Source of truth for posts; `.cnblog_deleted` folder for removed items

##### Architectural Patterns & Design Principles
- Modular layering:
1. Presentation Layer: CLI
2. Application Layer: lib.rs orchestrator & weblog logic
3. Infrastructure Layer: config & RPC modules, SQLite persistence
4. External Systems: Local FS & Cnblog API
- Separation of concerns: each module has a single responsibility
- Data flow orchestration: pipeline from scanning → persistence → encoding → network

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Presentation Layer"
        Main["CLI Frontend\n(main.rs)"]:::cli
    end

    subgraph "Business Logic Layer"
        Orchestrator["Orchestrator\n(lib.rs)"]:::app
        Scanner["File Scanner & Sync Logic\n(weblog.rs)"]:::app
    end

    subgraph "Persistence & Encoding Layer"
        DB["SQLite Database"]:::db
        Encoder["Base64 Encoder"]:::persist
    end

    subgraph "External Integration Layer"
        Config["Config Manager\n(cfg.rs)"]:::infra
        RPC["RPC Client\n(rpc.rs)"]:::infra
    end

    subgraph "External Service & Filesystem"
        Service["Cnblog Service\n(MetaWeblog API)"]:::external
        FS["Local Filesystem"]:::fs
        Deleted[".cnblog_deleted Folder"]:::fs
    end

    %% Connections
    Main -->|"invoke sync"| Orchestrator
    Main -->|"load config"| Config
    Orchestrator -->|"scan → diff"| Scanner
    Scanner -->|"persist metadata"| DB
    DB -->|"encode DB file"| Encoder
    Encoder -->|"upload payload"| RPC
    RPC -->|"RPC calls"| Service
    Service -->|"responses"| RPC
    Scanner -->|"move deleted posts"| Deleted
    Deleted -.->|"stored in"| FS
    Scanner -->|"read/write files"| FS

    %% Click Events
    click Main "https://github.com/nsfoxer/cnblog/blob/main/src/main.rs"
    click Orchestrator "https://github.com/nsfoxer/cnblog/blob/main/src/lib.rs"
    click Config "https://github.com/nsfoxer/cnblog/blob/main/src/meta_weblog/cfg.rs"
    click RPC "https://github.com/nsfoxer/cnblog/blob/main/src/meta_weblog/rpc.rs"
    click Scanner "https://github.com/nsfoxer/cnblog/blob/main/src/meta_weblog/weblog.rs"
    click Service "https://github.com/nsfoxer/cnblog/blob/main/README.md"
    click Orchestrator "https://github.com/nsfoxer/cnblog/blob/main/src/meta_weblog/mod.rs"
    click CLI "https://github.com/nsfoxer/cnblog/blob/main/Cargo.toml"

    %% Styles
    classDef cli fill:#cce5ff,stroke:#004085,color:#004085;
    classDef app fill:#e2eafc,stroke:#004085,color:#004085;
    classDef persist fill:#d4edda,stroke:#155724,color:#155724;
    classDef db fill:#d1ecf1,stroke:#0c5460,color:#0c5460,shape:cylinder;
    classDef infra fill:#fff3cd,stroke:#856404,color:#856404;
    classDef external fill:#f8d7da,stroke:#721c24,color:#721c24,shape:cloud;
    classDef fs fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
```
