Code Monkey home page Code Monkey logo

ktoreasy's Introduction

KtorEasy

This is a project to demonstrate a suggested Ktor architecture. It includes Backend implementation with Ktor, MariaDB database connection with Hikari, Docker command to build Backend container and Docker compose to run both database and backend instance.

Architecture explained

arch_image
This is a suggested architecture that pretends to follow CLEAN principles. The main idea behind it is to define each layer with one purpose and to allow unit testing each layer independently.

To achieve this, I'm using Koin as a dependency injection framework. https://insert-koin.io/

Also, to make each layer testable, Controllers and APIs should always implement an interface that will be injected where they are used, instead of the implemented type.

Application.kt

File to define Java app main method, Koin setup and configuration initialisation.

Module.kt

Application extension function where we configure Ktor application with features, interceptors and routing.

Modules

Each module will define routes related with its functionality. It will be a Route function extension to configure it and it will receive an injected controller object. This layer is responsible of reacting to API call, receive parameters and invoke controllers function with them.

Controllers

There will be one controller per module and it will receive by injection those API objects it will use to handle module requests. Here all the business logic should be defined. Controller will fetch/write data through API objects and unhappy paths will be handled throwing exceptions and answered by status pages.

APIs

This layer will use DAOs to recover/write information from/in database. It will be responsible of transforming data if needed.

DAOs

DAOs objects will define tables and those operations to do with them. It will also map table rows into API expected objects.

Kotlin London Meetup presentation

I've presented this implementation in the Kotlin London Meetup group in Youtube. You can find the presentation here: https://www.youtube.com/watch?v=RXSwQHur6zM

How to run it

This project is prepared to be run in Docker via docker-compose. With a simple command, you will deploy it to Docker with a MariaDB container linked to the backend instance. Just run: docker-compose up.

MariaDB is accesible through port 3308 with root password mypassword by default. The backend is accesible through port 3510. You can change this settings in the docker-compose.yml file.

UPDATE: After bumping dependency versions, I've integrated monitoring with Grafana + Prometheus to the Docker compose file.

ktoreasy's People

Contributors

dmcallejo avatar elraro avatar mathias21 avatar nartsisss avatar svill avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ktoreasy's Issues

[Improvement] Proposal for moving data related code away from Controller

I want to propose a BaseApi, which will handle querying of data:
fun <T> dbQuery(block: () -> T): T

Currently, this is handled in BaseController. See this line.

By doing this, Controller won't be concerned about transactions and 'how' the data is fetched. That part will be the API duty to provide the requested data.
I can make a PR for this.

Outdated dependencies

Dependencies seem to be outdated. It could be interesting to bump them to newer ones.

[Question] How/Where to put Join Selects?

I'm working on a Multi-Secret-Santa side-project. I'm not sure if your familiar with the concept, but basically a group of people are secretly assigned another member of the group to give a gift at Christmas. The problem with that was that when last year multiple families got together to do their secret santa thing, they all happened to be given members from their own family. It's not a huge problem but it sort of defeats the purpose of it.

So my goal is to build a secret-santas matcher to mitigate the above problem. I've created a DB diagram with a bunch of junction tables: https://dbdiagram.io/d/616c66dc940c4c4eec9968d9

The junction tables are used to allow a user to be part of multiple groups, and a group to have multiple users.

In summery:
User = User
Group = Group of people who are not supposed to be each other's secret santa
Party = Multiple groups.

Constraint:
A member of a group cannot be the secret santa of another member of the same group.

So, on to my question:

Where (and how) would I place the join selects in your architecture? I thought of putting it in the GroupAPI, but it doesn't quite feel right to have it at that level. Should I use it in the UsersGroupDao? But how would I make use of the UserDao and GroupDao to create the joins?

Any input would be greatly appreciated! In the mean time, I'm going to watch the videos you mentioned in my other issue :)

How start embeddedServer at Test ?

Dear Sir

very thx your post https://medium.com/@math21/testing-a-ktor-server-part-i-route-testing-84f8e82454d7 & demo

i have QA

as the code

embeddedServer(Netty, port = config.port) {

i can start a ktor server at normal mode ( src)

i look the test sourceSet

you craete a test demo

class ApplicationTest {

if i export " embeddedServer(Netty, port = config.port) {...}" as a class (Yes i want run ktor serve at Andorid or other JVM like)

how let us test this class at Test mode ? or how start embeddedServer at Test ?

THX

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.