CEKREM . GITHUB . IO

Making a Clean Architecture Blog Engine From Scratch pt 1

#kotlin #clean architecture #blog #ktor #tdd

Let’s make a blog engine, ey?

Don’t get me wrong - Hugo (the stuff that I’m currently using to drive this site) is great. It’s blazing fast, feature-rich, and battle-tested. But as a developer who’s been diving deep into Clean Architecture lately (as you might have noticed from my recent posts), I’ve been itching to apply these principles to a real project. And what better way to learn than by potentially over-engineering my own blog engine?

The Plan

Rather than just throwing together another quick web app, I’m attempting to build this the “right” way:

  1. Following Clean Architecture principles to the letter
  2. Using Test-Driven Development (TDD)
  3. Keeping the codebase screaming its intent

The Architecture

Following Uncle Bob’s Clean Architecture principles, I’ve structured the project into clear layers:

 1src/main/kotlin/io/github/cekrem/
 2├── domain/                     # Core business entities and rules
 3│   └── model/                  # Content, ContentBlock, ContentType, etc.
 4├── application/               # Application-specific business rules
 5│   ├── config/               # Blog configuration (title, description, url, menu items etc)
 6│   ├── gateway/              # Port interfaces (ContentSource)
 7│   ├── parser/              # Content parsing interfaces
 8│   └── usecase/             # Application use cases
 9├── adapter/                  # Interface adapters
10│   └── presenter/           # Presentation interfaces
11└── infrastructure/          # External frameworks and tools
12    ├── contentsource/       # File, Mock, and RSS implementations
13    ├── factory/             # Dependency injection
14    ├── parser/              # Markdown parsing implementation
15    └── web/                 # Ktor setup, routes, and templates

Domain Layer

The domain models are pure Kotlin data classes, representing the core concepts:

 1data class Content(
 2    val path: String,
 3    val title: String,
 4    val blocks: List<ContentBlock>,
 5    val type: ContentType,
 6    val metadata: Metadata,
 7    val publishedAt: LocalDateTime? = null,
 8    val updatedAt: LocalDateTime? = null,
 9    val slug: String = path.split("/").last(),
10)

Application Layer

The application layer contains use cases that orchestrate the domain logic. I’m using the UseCase interface pattern I discussed in A Use Case for UseCases in Kotlin:

 1interface UseCase<in I, out O> {
 2    suspend operator fun invoke(input: I): O
 3}
 4
 5class GetContentUseCase(
 6    private val contentSource: ContentSource
 7) : UseCase<String, Content?> {
 8    override suspend fun invoke(input: String): Content? =
 9        contentSource.getByPath(input)
10}

Interface Adapters Layer

The adapters layer converts data between the formats most convenient for the use cases and external agencies. For this blog engine, the main adapter is the content presenter:

1interface ContentPresenter {
2    fun presentContent(content: Content): Any
3
4    fun presentContentList(contents: List<ContentSummary>): Any
5}

This layer ensures that our domain and application layers remain clean and independent of any presentation concerns. The Any return type provides maximum flexibility - implementations can return HTML strings, view models, or any other format needed by the infrastructure layer. This approach perfectly aligns with the Clean Architecture’s dependency rule while keeping our options open for different presentation formats.

Infrastructure Layer

The infrastructure layer handles the technical details of web serving (using Ktor), content storage, and template rendering:

 1fun startServer(
 2    getContent: GetContentUseCase,
 3    listContents: ListContentsByTypeUseCase,
 4    getListableContentTypes: GetListableContentTypes,
 5    contentPresenter: ContentPresenter,
 6    config: ServerConfig,
 7) {
 8    embeddedServer(Netty, port = config.port) {
 9        // ... Ktor setup
10    }.start(wait = true)
11}

Why This Matters

  1. Separation of Concerns: The domain logic knows nothing about Ktor, Mustache templates, or markdown parsing. It’s pure business rules.

  2. Testability: Each layer can be tested in isolation. The use cases don’t need a web server to be tested.

  3. Flexibility: Want to switch from Mustache to another template engine? Just create a new presenter implementation. Need to change how content is stored? The ContentSource interface makes that easy.

Current Status

So far I’ve only got the basic scaffolding in place. The next steps are:

  1. Add failing tests for everything; TDD for the win!
  2. Implement the markdown parser
  3. Add proper template rendering
  4. Add RSS feed generation
  5. (Implement beyond MVP features as needed)

Is This Over-Engineering?

Probably! But that’s not necessarily a bad thing when the goal is learning. As The Pragmatic Programmer reminds us, sometimes you need to go too far to find out where “too far” actually is. I’d rather go to far now and learn from it.

The real test will be whether this ends up being more maintainable and adaptable than Hugo for my specific needs. At worst, I’ll have learned a lot about Clean Architecture in practice. At best, I’ll have a blog engine that perfectly fits my needs and is a joy to extend.

Next Steps

I’ll be documenting this journey as I go. The next post will likely focus on implementing the markdown parser while maintaining our clean architecture principles. But I’ll write some failing tests first, and do go full TDD Stay tuned!

“Architecture is about making it clear what the application does by looking at the structure of the code.” - Uncle Bob

Check out Uncle Bob’s Clean Architecture book if you haven’t already. It’s a great read and a good reminder of what we’re trying to achieve.

The code is available on GitHub if you want to follow along or contribute. Just remember - this is very much a work in progress!

Disclaimer

I can’t promise I’ll add to this project weekly, there are other things I’d like to write about as well. Stay tuned in any case!

Reply to this post by email ↪