Microservice Boilerplate

This project uses a Spec-First approach, where we always start with our spec and use it generates base code, tests, documentation, etc.

More on the Spec Driven Approach can be found at the confluence link below: https://sherwoodforest.atlassian.net/wiki/spaces/ARC/pages/2754609318/Spec+Driven+Development

Project Structure

The boilerplate is comprsed of a spec, src, hooks, and build folder, along with supporting files and directories. The developer will only have to make edits in the spec and src folders.

spec: Has the main OpenAPI spec and all the pieces that make up the spec.

src: Has the developer's code

hooks: Stores all the git hooks. These git hooks are only active until you run a build that pushes these hooks into the .git directory

build: Has the auto-generated code with the endpoints and models.

  • This folder will only appear after running a project build.

Git Strategies

Before we get started we will set up our Git Hooks and review Bakkt's Git Strategies.

Enable Git Hooks

Build the project by running ./gradlew build, this will run the gradle task installLocalGitHook.

Steps to Create a Spec-Driven Project

  1. Add Project Details to the Gradle Properties File
  2. Write Service Spec
  3. Rename Package of SRC Folder
  4. Generate Controllers and Models from the API Spec
  5. Write Business Logic by Extending Generated Code
  6. Start the App
  7. Write and Run Test Cases
  8. Create and Publish Docker Image of App
  9. Configure CI Pipeline
  10. Configure CD Pipeline

Add Project Details to the Gradle Properties File

Go to the gradle.properties file in the root directory and edit the values to reflect your microservice.

Write the Service Spec

The spec is broken into different pieces found in the ./spec folder, and the main spec is named openapi-v1.0. Change the version of this spec name everytime you update the version of the service.

Go to the ./spec folder and follow the Readme.md for details on the best way to write your spec.

Review the documentation found below for further details: https://sherwoodforest.atlassian.net/wiki/spaces/ARC/pages/2754806111/Writing+OpenAPI+Spec

Rename the folder under the "src" folder

Go the ./src and rename the package directory to match the package name defined in the gradle.properties file.

For instance, the package name defined in the gradle.properties file in this template is com.platform.boilerplate.example so the folder name in the ./src folder should be named the same thing (com.platform.boilerplate.example)

Generate Controllers and Models from the Spec

The command below will build the generated controller and model code using the spec.

./gradlew build

The build command takes the spec file (located in the ./spec folder) and uses it to generate code and documentation for your controller and model. The code can be found in the ./build/generated/src folder and documentation can be found in the ./build/generated/docs folder.

Write Business Logic by Extending Generated Code

Add your customizations and business logic in the package directory in the ./src folder.

Extend the classes generated by the code generator in the build folder, below is an example:

@Controller
public class PetController extends AbstractPetsController{
    @Override
    public Mono<Pet> showPetById(String petId) {
        return Mono.justOrEmpty(new Pet(Long.parseLong(petId), "Bob"));
    }
}

Few things to keep in mind...

  • Add the Controller annotation on the top
  • Use the methods that do not end with api.
  • The model classes are created in the generated code and is referenced in this code.
  • If you need a model that isn't associated with an endpoint then manually code it in the src folder.

Start the App

./gradlew run

View Services's in Swagger or Redocly

The added endpoints should be visible in the Swagger and Redocly pages.

Swagger

http://localhost:9080/swagger.html

Redocly

http://localhost:9080/redocly.html

Write and Run Test Cases

Reference best practices found at the page: https://sherwoodforest.atlassian.net/wiki/spaces/ARC/pages/2744451265/Testing

CI Pipeline

Go to the ci-pipeline.yml file and add your project details.

CD Pipeline

The CD Pipeline files will be setup in the Pipelines repository (https://github.com/Sherwood-forest/pipelines).

Follow the documentation found at the following page for further details on the setup: https://sherwoodforest.atlassian.net/wiki/spaces/ARC/pages/2766701107/CD+Pipeline+Setup