Site icon Golang Libraries, Apps, Golang Jobs and Go Tutorials

Build a RESTful API service in Golang without repetitive boilerplate

Golang RESTful API

There are a lot of materials about how to write services, where at first you need to choose some framework to use, then comes wiring of handlers, configs, logs, storage, etc, not to mention deploying that service somewhere. We’ve been writing services for quite some time and more often than not you’d just want to skip all this tedious process of gluing stuff together and just write some useful code.

That’s why we created a tool, called Mify — it’s an open-source infrastructure boilerplate generator, which would help you build a service, taking the best practices used to date. So in this tutorial, we’ll show how to create a simple service using Mify with a classic example — a to-do app.

Prerequisites

Before starting this tutorial, here’s the link to the complete example: https://github.com/mify-io/todo-app-example

Creating Project

After installing Mify, to start the project you need to create the workspace:COPYCOPYCOPYCOPY

$ mify init todo-app
$ cd todo-app

After getting into the new workspace, run:COPYCOPYCOPYCOPY

$ mify add service todo-backend

Now, this will create a Go template for your to-do backend. Here’s a simplified tree of the workspace with all generated files:COPYCOPYCOPYCOPY

.
├── go-services
│   ├── cmd
│   │   ├── dev-runner
│   │   │   └── main.go
│   │   └── todo-backend
│   │       ├── Dockerfile
│   │       └── main.go
│   ├── go.mod
│   ├── go.sum
│   └── internal
│       ├── pkg
│       │   └── generated
│       │       ├── configs
│       │       │   └── ...
│       │       ├── consul
│       │       │   └── ...
│       │       ├── logs
│       │       │   └── ...
│       │       └── metrics
│       │           └── ...
│       └── todo-backend
│           ├── app
│           │   ├── request_extra.go
│           │   ├── router
│           │   │   └── router.go
│           │   └── service_extra.go
│           └── generated
│               ├── api
|               |   └── ...
│               ├── app
│               │   └── ...
│               ├── apputil
│               │   └── ...
│               └── core
│                   └── ...
├── schemas
│   └── todo-backend
│       ├── api
│       │   └── api.yaml
│       └── service.mify.yaml
└── workspace.mify.yaml

Mify loosely follows one of the common Go layouts, which is suitable for multiple services in one repository. In internal/pkg/generated there are common libraries for configs, logs, and metrics that can be reused for multiple services. Your service go-to directory is in internal/todo-backend .

At this point this service is pretty bare, so we need to add API to it.

Defining API

You can find the OpenAPI schema for the todo-backend in schemas/todo-backend/api/api.yaml file. Schemas directory in the root of the workspace is a place where all service configs related to Mify are stored.

Let’s create a simple CRUD API for your todo backend:

Here’s how your OpenAPI schema would look for this API:

Exit mobile version