Friday, May 3, 2024
HomeGolangConstruct a RESTful API service in Golang with out repetitive boilerplate

Construct a RESTful API service in Golang with out repetitive boilerplate


There are a variety of supplies about how you can write providers, the place at first it’s essential select some framework to make use of, then comes wiring of handlers, configs, logs, storage, and so on, to not point out deploying that service someplace. We’ve been writing providers for fairly a while and as a rule you’d simply wish to skip all this tedious means of gluing stuff collectively and simply write some helpful code.

That’s why we created a device, known as Mify — it’s an open-source infrastructure boilerplate generator, which might provide help to construct a service, taking the perfect practices used thus far. So on this tutorial, we’ll present how you can create a easy service utilizing Mify with a basic instance — a to-do app.

Earlier than beginning this tutorial, right here’s the hyperlink to the whole instance: https://github.com/mify-io/todo-app-example

After putting in Mify, to start out the undertaking it’s essential create the workspace:COPYCOPYCOPYCOPY

$ mify init todo-app
$ cd todo-app

After entering into the brand new workspace, run:COPYCOPYCOPYCOPY

$ mify add service todo-backend

Now, this may create a Go template to your to-do backend. Right here’s a simplified tree of the workspace with all generated recordsdata:COPYCOPYCOPYCOPY

.
├── go-services
│   ├── cmd
│   │   ├── dev-runner
│   │   │   └── principal.go
│   │   └── todo-backend
│   │       ├── Dockerfile
│   │       └── principal.go
│   ├── go.mod
│   ├── go.sum
│   └── inside
│       ├── 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 many frequent Go layouts, which is appropriate for a number of providers in a single repository. In inside/pkg/generated there are frequent libraries for configs, logs, and metrics that may be reused for a number of providers. Your service go-to listing is in inside/todo-backend .

At this level this service is fairly naked, so we have to add API to it.

You could find the OpenAPI schema for the todo-backend in schemas/todo-backend/api/api.yaml file. Schemas listing within the root of the workspace is a spot the place all service configs associated to Mify are saved.

Let’s create a easy CRUD API to your todo backend:

  • POST /todos for including new to-do notes.
  • PUT,GET,DELETE /todos/{id} for updating, retrieving, and deleting them.

Right here’s how your OpenAPI schema would search for this API:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments