tjweir / http4s-ddd-example

a simple web service using Http4s + Doobie + Jooq + JWT

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Coverage Status Maintainability

in this project I applied the lessons learned in @codelyTv, this project is a simple API with authorization using JWT

🐣 Setup

  • sbt flyWayMigrate β€” run database migrations
  • SCALA_ENV=test sbt flyWayMigrate β€” run database migrations for tests
  • sbt run β€” run http4s server
  • sbt test β€” test suite

envs:

  API_POSTGRES_DATABASE=XXX
  API_POSTGRES_USER=XXX
  API_POSTGRES_PASS=XXX
  API_JWT_SECRET=XXXXXXX

  # tests suite envs
  API_POSTGRES_TEST_DATABASE=XXX
  API_POSTGRES_TEST_USER=XXX
  API_POSTGRES_TEST_PASS=XXX

πŸ“œ Endpoints

  • POST /authenticate: authentication payload: (name: String)
  • GET /tasks: Returns all tasks.
  • POST /tasks: creates a task payload: (title: String, done: Boolean)
  • GET /tasks/:id: returns a task
  • PUT /tasks/:id updates a task payload: (title: String, done: Boolean)
  • DELETE /tasks/:id deletes a task

Dependencies

  • http4s - A minimal, idiomatic Scala interface for HTTP
  • doobie - Functional JDBC layer for Scala.
  • jOOQ - jOOQ is the best way to write SQL in Java
  • jwt-scala - JWT support for Scala

πŸ€”Project structure

src
β”œβ”€β”€ main
β”‚   β”œβ”€β”€ resources
β”‚   β”‚   └── db
β”‚   β”‚       └── migration # β†’ Migration files
β”‚   β”‚           └── V1__create_tasks_table.sql
β”‚   └── scala
β”‚       β”œβ”€β”€ bounded_contexts # β†’ DDD Bounded contexts
β”‚       β”‚   β”œβ”€β”€ authentication
β”‚       β”‚   β”‚   β”œβ”€β”€ application # β†’ DDD application services (public access)
β”‚       β”‚   β”‚   β”‚   β”œβ”€β”€ AuthenticateUser.scala
β”‚       β”‚   β”‚   β”‚   └── DecodeToken.scala
β”‚       β”‚   β”‚   └── domain
β”‚       β”‚   β”‚       └── AuthenticationService.scala
β”‚       β”‚   β”œβ”€β”€ share # β†’ DDD share kernel
β”‚       β”‚   β”‚   β”œβ”€β”€ domain
β”‚       β”‚   β”‚   β”‚   └── UserEntity.scala
β”‚       β”‚   β”‚   └── infrastructure
β”‚       β”‚   β”‚       └── DoobieConnection.scala # β†’ DATABASE CONNECTION
β”‚       β”‚   └── tasks
β”‚       β”‚       β”œβ”€β”€ application # β†’ DDD application services (public access)
β”‚       β”‚       β”‚   β”œβ”€β”€ CreateTaskApplicationService.scala
β”‚       β”‚       β”‚   β”œβ”€β”€ DeleteTaskApplicationService.scala
β”‚       β”‚       β”‚   β”œβ”€β”€ FilterTaskApplicationService.scala
β”‚       β”‚       β”‚   β”œβ”€β”€ FindTaskApplicationService.scala
β”‚       β”‚       β”‚   └── UpdateTaskApplicationService.scala
β”‚       β”‚       β”œβ”€β”€ domain
β”‚       β”‚       β”‚   β”œβ”€β”€ TaskEntity.scala
β”‚       β”‚       β”‚   └── TaskRepositoryContract.scala
β”‚       β”‚       └── infrestucture
β”‚       β”‚           └── TaskRepository.scala
β”‚       β”œβ”€β”€ conf
β”‚       β”‚   └── Settings.scala # β†’ APP CONFIGURATION
β”‚       └── server # β†’ http4s server
β”‚           β”œβ”€β”€ Server.scala
β”‚           └── services
β”‚               β”œβ”€β”€ Services.scala
β”‚               β”œβ”€β”€ authentication
β”‚               β”‚   β”œβ”€β”€ AuthenticateService.scala
β”‚               β”‚   └── TokenSerializer.scala
β”‚               β”œβ”€β”€ helpers
β”‚               β”‚   └── Message.scala
β”‚               β”œβ”€β”€ middleware
β”‚               β”‚   └── AuthMiddleware.scala # β†’ api authorization
β”‚               └── tasks
β”‚                   β”œβ”€β”€ CreateTaskService.scala
β”‚                   β”œβ”€β”€ DeleteTaskService.scala
β”‚                   β”œβ”€β”€ GetTaskService.scala
β”‚                   β”œβ”€β”€ ListTaskService.scala
β”‚                   └── UpdateTaskService.scala

About

a simple web service using Http4s + Doobie + Jooq + JWT


Languages

Language:Scala 98.3%Language:Dockerfile 1.7%