DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Building Monolithic AI Brains, Build a Specialist Team Instead
  • Lifecycle Microservices With GenAI Tools
  • FastAPI Got Me an OpenAPI Spec Really... Fast
  • AI-Driven API and Microservice Architecture Design for Cloud

Trending

  • Stop Debugging Glue Jobs Manually: Building an Agentic Observability Layer for Data Pipelines
  • Mocking Kafka for Local Spring Development
  • Why DDoS Protection Is an Architectural Decision for Developers
  • Data Contracts as the "Circuit Breaker" for Model Reliability
  1. DZone
  2. Data Engineering
  3. Databases
  4. Building a Microservice in Perl (Part 1): Designing the API

Building a Microservice in Perl (Part 1): Designing the API

A brief description of microservices, then jumping into defining one with OpenAPI.

By 
Mark Gardner user avatar
Mark Gardner
DZone Core CORE ·
Updated Mar. 02, 21 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
4.5K Views

Join the DZone community and get the full member experience.

Join For Free

A microservice can be thought of as the distributed computing implementation of the Unix philosophy of "Do one thing and do it well." Usually modeled around specific business domains, a well-designed set of microservices each have their own lifecycle and communicate with each other and with consuming applications via technology-agnostic protocols like HTTP.

Because of this, a microservice may be implemented using whatever is the best programming language, database, or other environment to fit the job. Perl can be an excellent choice for development because of its strengths as a battle-tested, expressive multi-paradigm language with a broad open-source library of modules.

In this series of articles, we'll be using Perl, the Mojolicious web framework, and the OpenAPI (formerly Swagger) specification for describing web services to develop an example microservice that adheres to the REST architectural style for communication over HTTP. Some knowledge of Perl and web development is assumed, and we'll be linking to supporting documentation and literature as we go.

In a full microservices implementation, we would start by modeling the business domains being serviced and the explicit boundaries between them. For the purposes of this series, we are limiting ourselves to developing only one microservice with a trivial purpose-a toy, if you will. This service will act as a simple dictionary allowing consumers to both query for the definitions of words as well as add new entries.

We could envision a variety of consumers: a web front-end, a mobile app, maybe even SMS text messaging. The key is that the design of our application programming interface (API) should not dictate the implementation or lifecycle of any of these consumers, and it should be easy for their developers to understand its specification.

To that end, we turn to OpenAPI, which gives both our microservice and its consumers an unambiguous machine-readable description of the interface without the need for additional code or documentation. And as we'll see later, we'll use that very same specification to drive our web framework to produce HTTP routes that accept valid input and produce valid output.

A full OpenAPI document can be written in either JSON or YAML format, but it must ultimately be able to be represented in both formats, so there are some limitations described in the spec. Here's the OpenAPI document for our dictionary microservice in YAML:

YAML
 




x
72


 
1
openapi: 3.0.3
2
info:
3
  title: Dictionary
4
  description: The PhoenixTrap.com dictionary microservice
5
  version: 1.0.0
6
  license:
7
    name: Artistic License 2.0
8
    url: https://www.perlfoundation.org/artistic-license-20.html
9
paths:
10
  /health:
11
    get:
12
      summary: Check if this service is online
13
      x-mojo-to: monitoring#heartbeat
14
      responses:
15
        200:
16
          description: All systems operational
17
          content:
18
            application/json:
19
              schema:
20
                type: object
21
        500:
22
          description: Something is wrong
23
  /word/{word}:
24
    get:
25
      summary: Get the definition of a word
26
      x-mojo-to: word#define
27
      parameters:
28
      - $ref: '#/components/parameters/word'
29
      responses:
30
        200:
31
          description: Found word
32
          content:
33
            application/json:
34
              schema:
35
                type: string
36
        404:
37
          description: Could not find word
38
    post:
39
      summary: Add or replace the definition of a word
40
      x-mojo-to: word#save
41
      parameters:
42
      - $ref: '#/components/parameters/word'
43
      requestBody:
44
        description: Definition of a word
45
        required: true
46
        content:
47
          application/x-www-form-urlencoded:
48
            schema:
49
              type: object
50
              properties:
51
                definition:
52
                  type: string
53
      responses:
54
        200:
55
          description: Word saved
56
    delete:
57
      summary: Delete an entry from the dictionary
58
      x-mojo-to: word#remove
59
      parameters:
60
      - $ref: '#/components/parameters/word'
61
      responses:
62
        200:
63
          description: Word deleted
64
components:
65
  parameters:
66
    word:
67
      description: A word in the dictionary
68
      in: path
69
      name: word
70
      required: true
71
      schema:
72
        type: string



(I worked on this in the free online Swagger Editor, which checks your syntax, previews what API documentation might look like, and offers suggestions if it finds errors in your document.)

Most of the above should be fairly self-explanatory to any web developer, and you can check the OpenAPI specification for required and optional fields and their allowed values. The only odd field listed is x-mojo-to; we'll be using the Mojolicious::Plugin::OpenAPI module to read that field when generating routes to controllers in the Mojolicious web framework.

In the next installment, we'll actually be writing some Perl by building our Mojo application and tests.

microservice API Perl (programming language)

Published at DZone with permission of Mark Gardner. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Stop Building Monolithic AI Brains, Build a Specialist Team Instead
  • Lifecycle Microservices With GenAI Tools
  • FastAPI Got Me an OpenAPI Spec Really... Fast
  • AI-Driven API and Microservice Architecture Design for Cloud

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook