A server code template using Kotlin, Gradle, and Ktor

Overview

Kotlin Server Template

This project is a server code template using Kotlin, Gradle, and Ktor. It aims to help you build a service by providing reusable code examples that is likely essential for a server.

Tech stack

Architecture

This project consists of several Gradle subprojects separated based on Domain-driven design (DDD) as below.

graph TD
    Infrastructure --> Domain
    Presentation --> Domain
    Boot --> Domain
    Boot --> Infrastructure
    Boot --> Presentation

domain

Domain contains pure conceptual business logic implemented in Kotlin code which uses limited and minimal external dependencies.

infrastructure

Infrastructure contains actual implementations of domain interfaces which use external dependencies such as database, network, and libraries that possibly be replaced.

presentation

Presentation provides domain functions as actual network APIs. It contains logic to support network protocols such as HTTP, gRPC and also API serialization / deserialization for domain.

boot

Boot depends on all other subprojects and connects implementations of them to run a server application. In other words, it is responsible for dependency injection (DI). It also contains several resource files to run a server.

Other subprojects

  • logging provides logback library dependency and configuration of it for global use in the project.

Gradle Setting

Root build.gradle.kts contains settings commonly shared to all subprojects, and also several task settings for the entire application.

  • Common external library dependencies
  • Main class definition
  • Tasks that build a fat jar and containerize it.

Each subproject's build.gradle.kts contains settings only for its own subproject.

  • Dependencies for the subproject.
  • Several plugin settings for the subproject.

Testing

Examples of unit test, integration test, and end-to-end test are all included in this project.

  • Unit tests are usually written for entities, services in domain layer.
  • Integration tests are usually written for repositories in infrastructure layer by using Testcontainers.
  • End-to-end tests are written in boot layer usually for testing the server APIs.

Server API

The server provides simple API examples as follows.

  • GET /greeting/{name}
    > curl -X GET http://localhost:8080/greeting/alice
    HTTP/1.1 200 OK
    Content-Type: text/plain; charset=UTF-8
    Hello, alice.
    
  • GET /users/{id}
    curl -X GET http://localhost:8080/users/non-existing-user HTTP/1.1 404 Not Found">
    > curl -X GET http://localhost:8080/users/existing-user
    HTTP/1.1 200 OK
    Content-Type: application/json; charset=UTF-8
    {"id":"existing-user","name":"..."}
    
    > curl -X GET http://localhost:8080/users/non-existing-user
    HTTP/1.1 404 Not Found
    
  • POST /users/{id}: Creates a user.
    curl -i -X POST http://localhost:8080/users -H "Content-Type: application/json" -d '{ "name": "!@" }' HTTP/1.1 400 Bad Request Content-Type: text/plain; charset=UTF-8 User name must be alphanumeric.">
    > curl -i -X POST http://localhost:8080/users -H "Content-Type: application/json" -d '{ "name": "bob" }'
    HTTP/1.1 200 OK
    Content-Type: application/json; charset=UTF-8
    {"id":"some-random-id","name":"bob"}
    
    > curl -i -X POST http://localhost:8080/users -H "Content-Type: application/json" -d '{ "name": "!@" }'
    HTTP/1.1 400 Bad Request
    Content-Type: text/plain; charset=UTF-8
    User name must be alphanumeric.
    
  • PATCH /users/{id}: Updates a user.
    curl -i -X POST http://localhost:8080/users/existing-user -H "Content-Type: application/json" -d '{ "name": "!@" }' HTTP/1.1 400 Bad Request Content-Type: text/plain; charset=UTF-8 User name must be alphanumeric. > curl -i -X POST http://localhost:8080/users/non-existing-user -H "Content-Type: application/json" -d '{ "name": "damien" }' HTTP/1.1 404 Not Found">
    > curl -i -X PATCH http://localhost:8080/users/existing-user -H "Content-Type: application/json" -d '{ "name": "charlie" }'
    HTTP/1.1 200 OK
    Content-Type: application/json; charset=UTF-8
    {"id":"existing-user","name":"charlie"}
    
    > curl -i -X POST http://localhost:8080/users/existing-user -H "Content-Type: application/json" -d '{ "name": "!@" }'
    HTTP/1.1 400 Bad Request
    Content-Type: text/plain; charset=UTF-8
    User name must be alphanumeric.
    
    > curl -i -X POST http://localhost:8080/users/non-existing-user -H "Content-Type: application/json" -d '{ "name": "damien" }'
    HTTP/1.1 404 Not Found
    
  • DELETE /users/{id}
    > curl -i -X DELETE http://localhost:8080/users/existing-user
    HTTP/1.1 200 OK
    
    > curl -i -X POST http://localhost:8080/users/non-existing-user
    HTTP/1.1 404 Not Found
    
You might also like...
Multi module architecture Android template project using MVVM, Dagger-Hilt, and Navigation Components
Multi module architecture Android template project using MVVM, Dagger-Hilt, and Navigation Components

ModularAppTemplate An Android template project following a multi module approach with clean architecture. It has been built following Clean Architectu

πŸ’« A Gradle Plugin to generate your networking code from Swagger

Swagger Gradle Codegen A Gradle plugin to generate networking code from a Swagger spec file. This plugin wraps swagger-codegen, and exposes a configur

A Kotlin Multiplatform and Compose template that allows you to easily set up your project targeting: Android, Desktop, and Web

A Kotlin Multiplatform and Compose template that allows you to easily set up your project targeting: Android, Desktop, and Web

A template that utilizes both Scala and Kotlin because why not (And also because I endorse programming hell)

Fabric-Scala-Kotlin-template A template that utilizes both Scala and Kotlin because why not (And also because I endorse programming hell) I don't care

An Android template project (in Kotlin) with boilerplate and current patterns.

android-starter-v4 An Android template project (in Kotlin) with boilerplate and plumbing, exploring current architecture patterns. A bit too much for

Maildroid is a small robust android library for sending emails using SMTP server
Maildroid is a small robust android library for sending emails using SMTP server

Maildroid πŸŽ‰ Maildroid is a small robust android library for sending emails using SMTP server πŸŽ‰ Key Features β€’ Add to your project β€’ Documentation β€’

Kotlin multiplatform library template.

template-kmp-library Kotlin multiplatform library template. Has a baseline setup for a multiplatform library supporting all kotlin targets except andr

Kotlin Multiplatform Mobile App Template

KMMT : Kotlin Multiplatform Mobile Template Kotlin Multiplatform Mobile Development Simplified KMMT is a KMM based project template designed to simpli

Spring-Boot Kotlin template for new microservices

kotlin-ms-template Spring-Boot Kotlin template for new microservices REST and GRPC ready Kafka producer/consumer ready Logs in JSON Format Base ready

Owner
Dooho Chang
Devin, Head of Engineering @iamport
Dooho Chang
A Template for a Github Actions Pipeline for building and publishing Gradle-JVM Applications

github-actions-cd-template-jvm A Template for a Github Actions Pipeline for building and publishing Gradle-JVM Applications It build a executable shad

Raphael Panic 0 Dec 5, 2021
An advanced Minecraft plugin template made in Gradle

//DONT_APPLY_TEMPLATE_CHANGE Gradle Plugin Template Kotlin DSL Version ℹ️ This template was planned to support only kotlin, but it also supports Java!

null 0 Apr 17, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Practical Kotlin Multiplatform on the Web λ³Έ μ €μž₯μ†ŒλŠ” μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌ 기반 μ›Ή ν”„λ‘œκ·Έλž˜λ° μ›Œν¬μˆ(κ°•μ’Œ)을 μœ„ν•΄ μž‘μ„±λœ ν…œν”Œλ¦Ώ ν”„λ‘œμ νŠΈκ°€ μžˆλŠ” κ³³μž…λ‹ˆλ‹€. μ›Œν¬μˆ κ³Όμ •μ—μ„œ μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌμ„ 기반으둜 ν”„λ‘ νŠΈμ—”λ“œ(front-end)λŠ” Ko

SpringRunner 14 Nov 5, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Building a Full Stack Web App with Kotlin Multiplatform λ³Έ μ €μž₯μ†ŒλŠ” INFCON 2022μ—μ„œ μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌ 기반 μ›Ή ν”„λ‘œκ·Έλž˜λ° ν•Έμ¦ˆμ˜¨λž©μ„ μœ„ν•΄ μž‘μ„±λœ ν…œν”Œλ¦Ώ ν”„λ‘œμ νŠΈκ°€ μžˆλŠ” κ³³μž…λ‹ˆλ‹€. ν•Έμ¦ˆμ˜¨ κ³Όμ •μ—μ„œ μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌμ„

Arawn Park 19 Sep 8, 2022
Gradle plugin adding a task to run a Paper Minecraft server

Run Paper Run Paper is a Gradle plugin which adds a task to automatically download and run a Paper Minecraft server along with your plugin built by Gr

Jason 64 Dec 29, 2022
The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

Martmists 14 Dec 26, 2022
Pragmateam code challenge server (Kotlin)

Pragmateam code challenge server (Kotlin) Please refer to the provided document for the code challenge requirements. Framework & languages This projec

Pragmateam 0 Nov 9, 2021
This is a Kotlin multiplatform template project used to generate and deploy a natively compiled AWS lambda function using the custom runtime.

Overview This is a Kotlin multiplatform template project used to generate and deploy a natively compiled AWS Lambda function using a custom runtime. U

Greg Steckman 5 Jun 25, 2022
Kotlin and Ktor app, which can easily be deployed to Heroku

[ ?? Work in progress ??‍♀️ ⛏ ?? ??️ ?? ?? ?? ] Shoppe Kotlin Multiplatform App Kotlin and Ktor app, which can easily be deployed to Heroku. This appl

Adrian Witaszak 13 Oct 2, 2022
Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.

Delight-Playground ?? Native Android application built with Kotlin and Jetpack Compose. This app also illustrates the usage of advance libraries such

Kasem SM 41 Nov 6, 2022