Building A Containerized Microservice In Golang: A Step-by-step Guide

Prerequisites:

  • A beginner’s level of understanding of web services, Rest APIs and gRPC
  • GoLand/ VS Code
  • Properly installed and configured Go. If not, check it out here
  • Set up a new project directory under the GOPATH
  • Understanding of the standard Golang project. For reference, visit here
  • PostgreSQL client installed
  • Go kit

What are we going to do?

  • A global publishing company that publishes books and journals wants to develop a service to watermark their documents. A document (books, journals) has a title, author, and a watermark property
  • The watermark operation can be in Started, InProgress, and Finished status
  • The specific set of users should be able to do the watermark on a document
  • Once the watermark is done, the document can never be re-marked

Architecture:

Microservice Architecture

Authentication Service:

APIs:

  • /user/access, Method: GET, Secured: True, payload: user: <name>
    It will take the user name as an input and the auth service will return the roles and the privileges assigned to it
  • /authenticate, Method: GET, Secured: True, payload: user: <name>, operation: <op>
    It will authenticate the user with the passed operation if it is accessible for the role
  • /healthz, Method: GET, Secured: True
    It will return the status of the service

Database Service:

APIs:

  • /get, Method: GET, Secured: True, payload: filters: []filter{“field-name”: “value”}
    It will return the list of documents according to the specific filters passed
  • /update, Method: POST, Secured: True, payload: “Title”: <ID>, document: {“field”: “value”, …}
    It will update the document for the given title id
  • /add, Method: POST, Secured: True, payload: document: {“field”: “value”, …}
    It will add the document and return the title-ID
  • /remove Method: POST, Secured: True, payload: title: <id>
    It will remove the document entry according to the passed title-id
  • /healthz, Method: GET, Secured: True
    It will return the status of the service

Watermark Service:

APIs:

  • /get, Method: GET, Secured: True, payload: filters: []filter{“field-name”: “value”}
    It will return the list of documents according to the specific filters passed
  • /status, Method: GET, Secured: True, payload: “Ticket”: <ID>
    It will return the status of the document for watermark operation for the passed ticket-id
  • /addDocument, Method: POST, Secured: True, payload: document: {“field”: “value”, …}
    It will add the document and return the title-ID
  • /watermark, Method: POST, Secured: True, payload: title: <id>, mark: “string”
    It is the main watermark operation API which will accept the mark string
  • /healthz, Method: GET, Secured: True
    It will return the status of the service

Operations and Flow:

  1. The user will access the API Gateway URL with the required user name, the ticket-id, and the mark with which the user wants the document to apply the watermark
  2. The user should not know about the authentication or database services
  3. Once the request is made by the user, it will be accepted by the API Gateway. The gateway will validate the request along with the payload
  4. An API forwarding rule of configuring the traffic of a specific request to a service should be defined in the gateway. The request when validated will be forwarded to the service according to that rule.
  5. We will define an API forwarding rule where the request made for any watermark will be first forwarded to the authentication service which will authenticate the request, check for authorized users, and return the appropriate status code.
  6. The authorization service will check for the user from which the request has been made, into the user database, and its roles and permissions. It will send the response accordingly
  7. Once the request has been authorized by the service, it will be forwarded back to the actual watermark service
  8. The watermark service then performs the appropriate operation of putting the watermark on the document or add a new entry of the document or any other request
  9. The operation from the watermark service of Get, Watermark, or AddDocument will be performed by calling the database CRUD APIs and forwarded to the user
  10. If the request is to AddDocument then the service should return the “TicketID” or if it is for watermark then it should return the status of the operation

Getting Started:

Project Layout:

  • api: Stores the versions of the APIs swagger files and also the proto and pb files for the gRPC protobuf interface.
  • cmd: This will contain the entry point (main.go) files for all the services and also any other container images if any
  • docs: This will contain the documentation for the project
  • config: All the sample files or any specific configuration files should be stored here
  • deploy: This directory will contain the deployment files used to deploy the application
  • internal: This package is the conventional internal package identified by the Go compiler. It contains all the packages which need to be private and imported by its child directories and immediate parent directory. All the packages from this directory are common across the project
  • pkg: This directory will have the complete executing code of all the services in separate packages.
  • tests: It will have all the integration and E2E tests
  • vendor: This directory stores all the third-party dependencies locally so that the version doesn’t mismatch later

Watermark Service:

  • Create a file named endpoints.go. This file will contain the actual calling of the service implemented functions.

Wrapping up:

--

--

--

Velotio Technologies is an outsourced software and product development partner for technology startups & enterprises. #Cloud #DevOps #ML #UI #DataEngineering

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

IoT Project ( Flutter + ZigBee )

Understanding the structure of an object of class Phylo

Let’s write a sudoku

Double Down on Service Orientation

Alerting when a GCP instance fails

The Essential Checklist for Procuring a Reservation System

Introducing the MATTR Platform

Step by Step guide for deploying a Flask application from GitHub on AWS EC2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Velotio Technologies

Velotio Technologies

Velotio Technologies is an outsourced software and product development partner for technology startups & enterprises. #Cloud #DevOps #ML #UI #DataEngineering

More from Medium

Migrate RDS SQL Server database to Custom RDS SQL Server

Best Database Management Software for 2022

Redis: Cache Invalidation Done Better

Using Localstack SNS and SQS for local testing