# shikihtm/blog-backend architecture

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

- Interactive diagram: https://gitdiagram.com/shikihtm/blog-backend
- Repository: https://github.com/shikihtm/blog-backend

## Overview

This is a Go backend for a personal blog: it serves MDX-authored posts and stores engagement counters in SQLite. The main workflow starts with HTTP requests routed to post handlers, which retrieve metadata/content or update counters through the repository; post creation writes MDX files. A separate startup-and-watch workflow syncs post metadata into SQLite. Authentication supports login and protects post creation. The README documents view/like endpoints; the sampled route registration does not show those routes, so that connection is documented rather than inferred from handler code.

Main entry points: [`cmd/api/main.go`](https://github.com/shikihtm/blog-backend/blob/main/cmd/api/main.go) (API startup), [`internal/handler/post/post_hanlder.go`](https://github.com/shikihtm/blog-backend/blob/main/internal/handler/post/post_hanlder.go) (Post handlers), [`internal/repository/repository.go`](https://github.com/shikihtm/blog-backend/blob/main/internal/repository/repository.go) (Blog repository), [`internal/repository/mdx_watcher.go`](https://github.com/shikihtm/blog-backend/blob/main/internal/repository/mdx_watcher.go) (Post sync watcher)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Service runtime"]
  node_main["API startup<br/>[main.go]"]
  node_domain_models["Domain models<br/>[post.go]"]
end

subgraph group_http["HTTP and auth"]
  node_post_handler["Post handlers<br/>[post_hanlder.go]"]
  node_auth_handler["Login handler<br/>[authentication.go]"]
  node_auth_config["Auth config<br/>[config.go]"]
  node_auth_middleware["Cookie auth<br/>[authentication.go]"]
end

subgraph group_content["Post content"]
  node_post_files["MDX posts"]
  node_sync_watcher["Post sync watcher<br/>[mdx_watcher.go]"]
  node_mdx_reader["MDX reader<br/>[mdx.go]"]
end

subgraph group_persistence["Persistence"]
  node_repo["Blog repository<br/>[repository.go]"]
  node_post_sync["Post metadata sync<br/>[post-repository.go]"]
  node_database_init["SQLite bootstrap<br/>[sqlite.go]"]
  node_sqlite[("SQLite store")]
end

node_client(("Blog client"))

node_client -->|"sends HTTP requests"| node_main
node_main -->|"initializes"| node_database_init
node_main -->|"loads config"| node_auth_config
node_main -->|"constructs repository"| node_repo
node_main -->|"registers routes"| node_post_handler
node_main -->|"registers routes"| node_auth_handler
node_main -->|"starts sync and watch"| node_sync_watcher
node_database_init -->|"creates schema"| node_sqlite
node_repo -->|"queries and updates"| node_sqlite
node_post_handler -->|"requests post operations"| node_repo
node_post_handler -->|"protects creation route"| node_auth_middleware
node_post_handler -->|"writes created post"| node_post_files
node_auth_handler -->|"uses user and claims"| node_domain_models
node_auth_middleware -->|"parses JWT claims"| node_domain_models
node_repo -->|"reads metadata and body"| node_mdx_reader
node_mdx_reader -->|"reads and parses"| node_post_files
node_sync_watcher -->|"syncs changed slugs"| node_post_sync
node_post_sync -->|"reads post metadata"| node_mdx_reader
node_post_sync -->|"upserts stats"| node_sqlite
node_sync_watcher -->|"monitors directory"| node_post_files
node_post_handler -.->|"increments views and likes"| node_repo

click node_main "https://github.com/shikihtm/blog-backend/blob/main/cmd/api/main.go"
click node_post_handler "https://github.com/shikihtm/blog-backend/blob/main/internal/handler/post/post_hanlder.go"
click node_auth_handler "https://github.com/shikihtm/blog-backend/blob/main/internal/handler/auth/authentication.go"
click node_auth_config "https://github.com/shikihtm/blog-backend/blob/main/internal/handler/auth/config.go"
click node_auth_middleware "https://github.com/shikihtm/blog-backend/blob/main/middleware/authentication.go"
click node_domain_models "https://github.com/shikihtm/blog-backend/blob/main/internal/model/post.go"
click node_post_files "https://github.com/shikihtm/blog-backend/blob/main/posts/tai-sao-nhan-doi-mang.mdx"
click node_sync_watcher "https://github.com/shikihtm/blog-backend/blob/main/internal/repository/mdx_watcher.go"
click node_mdx_reader "https://github.com/shikihtm/blog-backend/blob/main/internal/repository/mdx.go"
click node_repo "https://github.com/shikihtm/blog-backend/blob/main/internal/repository/repository.go"
click node_post_sync "https://github.com/shikihtm/blog-backend/blob/main/internal/repository/post-repository.go"
click node_database_init "https://github.com/shikihtm/blog-backend/blob/main/internal/database/sqlite.go"

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_main,node_domain_models,node_client toneBlue
class node_post_handler,node_auth_handler,node_auth_config,node_auth_middleware toneAmber
class node_post_files,node_sync_watcher,node_mdx_reader toneMint
class node_repo,node_post_sync,node_database_init,node_sqlite toneRose
```

## Components

### Service runtime

- [API startup](https://github.com/shikihtm/blog-backend/blob/main/cmd/api/main.go): `cmd/api/main.go`
- [Domain models](https://github.com/shikihtm/blog-backend/blob/main/internal/model/post.go): `internal/model/post.go`

### HTTP and auth

- [Post handlers](https://github.com/shikihtm/blog-backend/blob/main/internal/handler/post/post_hanlder.go): `internal/handler/post/post_hanlder.go`
- [Login handler](https://github.com/shikihtm/blog-backend/blob/main/internal/handler/auth/authentication.go): `internal/handler/auth/authentication.go`
- [Auth config](https://github.com/shikihtm/blog-backend/blob/main/internal/handler/auth/config.go): `internal/handler/auth/config.go`
- [Cookie auth](https://github.com/shikihtm/blog-backend/blob/main/middleware/authentication.go): `middleware/authentication.go`

### Post content

- [MDX posts](https://github.com/shikihtm/blog-backend/blob/main/posts/tai-sao-nhan-doi-mang.mdx): `posts/tai-sao-nhan-doi-mang.mdx`
- [Post sync watcher](https://github.com/shikihtm/blog-backend/blob/main/internal/repository/mdx_watcher.go): `internal/repository/mdx_watcher.go`
- [MDX reader](https://github.com/shikihtm/blog-backend/blob/main/internal/repository/mdx.go): `internal/repository/mdx.go`

### Persistence

- [Blog repository](https://github.com/shikihtm/blog-backend/blob/main/internal/repository/repository.go): `internal/repository/repository.go`
- [Post metadata sync](https://github.com/shikihtm/blog-backend/blob/main/internal/repository/post-repository.go): `internal/repository/post-repository.go`
- [SQLite bootstrap](https://github.com/shikihtm/blog-backend/blob/main/internal/database/sqlite.go): `internal/database/sqlite.go`
- **SQLite store**

### Other components

- **Blog client**

## Connections

- Blog client → API startup: sends HTTP requests (evidence: `README.md`)
- API startup → SQLite bootstrap: initializes (evidence: `cmd/api/main.go`)
- API startup → Auth config: loads config (evidence: `cmd/api/main.go`)
- API startup → Blog repository: constructs repository (evidence: `cmd/api/main.go`)
- API startup → Post handlers: registers routes (evidence: `cmd/api/main.go`)
- API startup → Login handler: registers routes (evidence: `cmd/api/main.go`)
- API startup → Post sync watcher: starts sync and watch (evidence: `cmd/api/main.go`)
- SQLite bootstrap → SQLite store: creates schema (evidence: `internal/database/sqlite.go`)
- Blog repository → SQLite store: queries and updates (evidence: `internal/repository/repository.go`)
- Post handlers → Blog repository: requests post operations (evidence: `internal/handler/post/post_hanlder.go`)
- Post handlers → Cookie auth: protects creation route (evidence: `internal/handler/post/post_hanlder.go`)
- Post handlers → MDX posts: writes created post (evidence: `internal/handler/post/post_hanlder.go`)
- Login handler → Domain models: uses user and claims (evidence: `internal/handler/auth/authentication.go`)
- Cookie auth → Domain models: parses JWT claims (evidence: `middleware/authentication.go`)
- Blog repository → MDX reader: reads metadata and body (evidence: `internal/repository/repository.go`)
- MDX reader → MDX posts: reads and parses (evidence: `internal/repository/mdx.go`)
- Post sync watcher → Post metadata sync: syncs changed slugs (evidence: `internal/repository/mdx_watcher.go`)
- Post metadata sync → MDX reader: reads post metadata (evidence: `internal/repository/post-repository.go`)
- Post metadata sync → SQLite store: upserts stats (evidence: `internal/repository/post-repository.go`)
- Post sync watcher → MDX posts: monitors directory (evidence: `internal/repository/mdx_watcher.go`)
- Post handlers → Blog repository: increments views and likes (evidence: `README.md`)
