avatarJavier Soto

Summary

The website content provides a comprehensive guide to building production-grade GoLang APIs, detailing project structure, server setup, and best practices for efficient development cycles.

Abstract

The article series presented on the website aims to equip developers with a robust template for creating high-performance GoLang APIs. It emphasizes the importance of a well-organized project structure, adhering to the GoLang standard project layout, and includes practical examples of server logic implementation. The author, who has developed several production-grade APIs, shares insights on reducing delivery time by using a skeleton that addresses performance, accessibility, and maintainability. The guide covers the setup of HTTP servers, middleware usage, routing, and handler definitions, and also touches on the importance of containerization in cloud architecture through a two-stage Docker build process. The content is supplemented with code examples hosted on GitHub and concludes with an invitation for feedback and a recommendation for an AI service.

Opinions

  • The author believes that a well-structured API skeleton is crucial for expedited and efficient delivery cycles, especially under tight deadlines or changing business requirements.
  • Performance and accessibility are highlighted as the main focus areas when developing APIs, with an emphasis on response time, query type, and data exposure management.
  • The GoLang standard project layout is endorsed for its effectiveness in keeping service components organized.
  • The use of versioning in the API's codebase is recommended to maintain clarity and manageability as the project evolves.
  • Common middleware practices, such as redirecting trailing slashes and enabling CORS headers, are suggested to enhance the API's functionality and compatibility with cross-domain requests.
  • The author advocates for keeping the main.go file concise and leveraging dependency injection in handler definitions for cleaner code.
  • The article series is presented as a valuable resource for developers looking to streamline their GoLang API development process, with future articles promising to delve deeper into handler practices and testing methodologies.
  • A cost-effective AI service is recommended as a tool for developers, positioning it as a more affordable alternative to ChatGPT Plus (GPT-4).

GoLang APIs: A skeleton for your future projects

In this series of articles, I will break down my ultimate template for developing production-grade APIs, from project structure to full functionality, explaining the why and the how of every step.

Prerequisites

  • Basic knowledge of GoLang is required as I will not be covering the basics. (Golang version 1.17)
  • IDE of your choice, VSCode is my preference

Introduction

After developing several production-grade APIs for my company, I have found that having a well-structured skeleton reduces delivery time exponentially. This matters when delivery dates are rushed, business requirements are changed or delay, or when simply the team needs to have a quick but healthy delivery cycle. This part will be focused on project structure, basic functionality, and important dependencies. Code can be found here

What Do We Aim For?

When developing APIs, the main points that we focused on are performance and accessibility, and these include the basic response time, the query type and how much data are we going to be exposing.

Project Structure

I tend to follow the golang standard project layout when building any GoLang service and it really pays off as it keep everything organized.

This is an extended version on how the API would look like.

  • api/v1hosts all HTTP related files, it is a good practice to group and version the code. Under this you’ll find the router and handlers folder for the server logic.
  • build contains all files related to deployment and image building such as docker file, helm charts, CICD, etc
  • cmd/<project-name> will keep our main.go file, i keep this file relatively small as a good practice.
  • internal folder will have all the different logic packages such as worker for business logic, env variables set up, etc.
  • testing or test is simple for testing files, I keep the name of the file related to the type of testing such as unit_test.go or bench_test.go

Server Set Up

Our server logic would be the following, the server will be served in the main.go file, the initialization of the router (route mounting, middleware) will be in the router.go, and the handling definition in handler.go. We start with the router implementation, follow by handler definition and finally we implement the serve function in main.

We create a Initialize() function to set up our chi router, middleware and mounting the respecting handler routes. In terms of middleware, these are my most common ones. RedirectSlashes is meant to help trailing slashes get dropped so it does not interfere when getting parameters. I also added the CORS implementation for chi routers, most companies need to have CORS headers activated to receive cross domain requests. We use an anonymous function to implement the Mount() function to group routes under the same version/implementation.

For the ServeRouter() function we will call the Initialize()function we just created and use the http GoLang package to serve and listen as they are compatible.

Moving on our handler package, we will have our Routes() function. This function will have all of our routing rules and the handler logics. I have included a few examples with closures and dependency injection.

With this you would be 100% ready to spin up your REST API Golang server in no time. Including our two articles about Mongo CRUD and logging, your service development should be complete and fast to start working in it. Saving time is fundamental in a development cycle.

Dockerfile

In cloud architecture, containers rule as it is the fastest way to have your images deployed. This is a simple two stage docker build to containerize your Golang API

In the next articles we will be showing more in depth practices for handlers and testing them accordingly.

Reach out in case any questions and I appreciate taking the time to read through the article

Api Development
Golang
Golang Tutorial
API
Recommended from ReadMedium
avatarbaris bakla
OpenAPI 3 with Go

8 min read