Skip to content

Clojure Microservice Boilerplate: Components, Reitit, Pedestal, Malli, Postgresql and Tests

License

Notifications You must be signed in to change notification settings

parenthesin/microservice-boilerplate-malli

Repository files navigation

microservice-boilerplate-malli

Clojure Microservice Boilerplate: Components, Reitit, Pedestal, Malli, Postgresql and Tests.

About this example

  • microservice-boilerplate: An example of how use the boilerplate, it's a simple btc wallet that fetch the current btc price in USD and you can check your transaction history, do deposits and withdrawals.

  • parenthesin/components: Helpers and component wrappers to give a foundation to create new services in clojure, you can find components for database, http, webserver and tools for db migrations.

Verb URL Description
GET /wallet/history get all wallet entries and current total
POST /wallet/deposit do a deposit in btc in the wallet
POST /wallet/withdrawal do a withdrawal in btc in the wallet if possible

Repl

To open a nrepl

clj -M:nrepl

To open a nrepl with all test extra-deps on it

clj -M:test:nrepl

Run dev

To open a dev nrepl

clj -M:dev:nrepl

Then, eval for instrument malli. Malli instrumentation enables input/output validation and clj-kondo lint annotations.

(start)

Run Tests

To run unit tests inside ./test/unit

clj -M:test :unit

To run integration tests inside ./test/integration

clj -M:test :integration

To run all tests inside ./test

clj -M:test

To generate a coverage report

clj -M:test --plugin kaocha.plugin/cloverage

Lint fix and format

clj -M:clojure-lsp format
clj -M:clojure-lsp clean-ns
clj -M:clojure-lsp diagnostics

Migrations

To create a new migration with a name

clj -M:migratus create migration-name

To execute all pending migrations

clj -M:migratus migrate

To rollback the latest migration

clj -M:migratus rollback

See Migratus Usage for documentation on each command.

Docker

Start containers with postgres user: postgres, password: postgres, hostname: db, port: 5432
and pg-admin email: [email protected], password: pg, port: 5433

docker-compose -f docker/docker-compose.yml up -d

Stop containers

docker-compose -f docker/docker-compose.yml stop

Running the server

First you need to have the database running, for this you can use the docker command in the step above.

Repl

You can start a repl open and evaluate the file src/microservice_boilerplate/server.clj and execute following code:

(start-system! (build-system-map))

Uberjar

You can generate an uberjar and execute it via java in the terminal:

# genarate a target/service.jar
clj -T:build uberjar
# execute it via java
java -jar target/service.jar

Features

System

  • malli Types, Schemas and generators.
  • component System Lifecycle and Dependencies
  • pedestal Http Server
  • reitit Http Routes System
  • clj-http Http Client
  • cheshire JSON encoding
  • aero Configuration file and enviroment variables manager
  • timbre Logging library
  • next-jdbc JDBC-based layer to access databases
  • hikaricp A solid, high-performance, JDBC connection pool at last
  • tools.build Clojure builds as Clojure programs

Tests & Checks

Directory Structure

./
├── .clj-kondo -- clj-kondo configuration and classes
├── .lsp -- clojure-lsp configuration
├── .github
│   └── workflows -- Github workflows folder.
├── docker -- docker and docker-compose files for the database
├── resources -- Application resources assets folder and configuration files.
│   └── migrations -- Current database schemas, synced on service startup.
├── src -- Library source code and headers.
│   └── microservice_boilerplate -- Source for the service example (wallet).
└── test -- Test source code.
    ├── integration -- Integration tests source (uses state-flow).
    │   └── microservice_boilerplate -- Tests for service example (wallet).
    └── unit -- Unity tests source (uses clojure.test).
        └── microservice_boilerplate -- Tests for service example (wallet).

Related

Based on this template, the work here tries to show what need to be refactored to move from Schema to Malli without loosing some key features like "typing", schema definition, coercion, generative testing and more.

Similar Projects

Using this Template

License

This is free and unencumbered software released into the public domain.
For more information, please refer to http://unlicense.org

About

Clojure Microservice Boilerplate: Components, Reitit, Pedestal, Malli, Postgresql and Tests

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published