Skip to main content

Generating Service Layout with API

First we need to add service to workspace:

mify add service --language go counting-backend

This command generates different layouts based on language that you've choosen:

  • Go service template it tries to follow this layout. This is not an official standard project layout, because Go doesn't have one, but it's pretty common.

  • Python service module based on Connexion and aiohttp.

  • NodeJS service is based on ExpressJS framework.

Now we need to define the API for this service. Open OpenAPI schema file, which is located at schemas/counting-backend/api/api.yaml and you will see this schema:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: counting-backend
  description: Service description
  contact:
    name: Maintainer name
    email: maintainer@example.com # Replace with your email
# Add your local and prod service url here for testing with Swagger UI.
# You can also add prefix for all paths like this:
#  - url: <service-url>/api
servers:
  - url: <service-url>/
paths: {}
# Example of a handler, uncomment and remove the above 'paths: {}' line.
# Check Petstore OpenAPI example for more possible options:
# https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore-expanded.yaml
#
# paths:
#   /path/to/api:
#     get:
#       summary: sample handler
#       responses:
#         '200':
#           description: OK
#           content:
#             application/json:
#               schema:
#                 $ref: '#/components/schemas/PathToApiResponse'
# components:
#   schemas:
#     PathToApiResponse:
#       type: object
#       properties:
#         value:
#           type: string
#       required:
#         - value

Let's add our handler definition to it:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: counting-backend
  description: Service description
  contact:
    name: Maintainer name
    email: maintainer@example.com # Replace with your email
# Add your local and cloud service url here for testing with Swagger UI.
# You can also add prefix for all paths like this:
#  - url: <service-url>/api
servers:
  - url: <service-url>/
paths:
  /counter/next:
    get:
      summary: get next number
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CounterNextResponse'

components:
  schemas:
    CounterNextResponse:
      type: object
      properties:
        number:
          type: integer
      required:
        - number

Now run command to apply changed schema:

mify generate

If you remove or change the name of the handler after you ran mify generate you need to delete directory with generated files and then re-run mify generate command.

Directory with generated files is located in different paths based on language:

  • Go: go-services/internal/counting-backend/generated
  • Python: py-services/counting_backend/generated
  • ExpressJS: js-services/counting-backend/generated

Now we have our handler and we are ready to implement the logic.