Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Building a Type-Safe Microservices Backend in Go


Constructing microservices purposes could be a ache since you usually must cope with a whole lot of boilerplate and it may be onerous to make sure end-to-end type-safety.🤯

✨On this information we’ll construct and deploy a totally type-safe microservices software in Go, implementing the backend for a Trello software for example.

To construct our app, we’ll be utilizing Encore, a backend growth platform that gives a type-safe Infrastructure SDK for declaratively defining infrastructure in Go code. We’ll then use Encore to robotically provision and deploy our software.

🚀 What’s on deck:

  • Set up Encore
  • Create your backend app from a template
  • Run regionally
  • Deploy to Encore’s free growth cloud

💽 Set up Encore

Set up the Encore CLI to run your native surroundings:

  • macOS: brew set up encoredev/faucet/encore
  • Linux: curl -L https://encore.dev/set up.sh | bash
  • Home windows: iwr https://encore.dev/set up.ps1 | iex

🔨 Create your app

We’ll be ranging from a template that has two companies, every with a few API endpoints and their very own databases.

(This instance is meant to point out the way you create microservices purposes with Encore. Nevertheless, Encore can simply as simply be used to construct monolithic architectures.)

That is what the structure appears to be like like:

Within the diagram, board and card are microservices, and as you’ll be able to see they every have their very own database and some endpoints.

When you’ve gotten put in Encore, create a brand new software from the template utilizing this command:

encore app create my-app-name --example=trello-clone
Enter fullscreen mode

Exit fullscreen mode

🏁 Working regionally

Earlier than working the appliance, ensure you have synced the mission dependencies by working go mod tidy and that you’ve got Docker put in and working. (Docker is required when working Encore purposes regionally that use SQL databases.)

To start out the Encore software, run:

encore run
Enter fullscreen mode

Exit fullscreen mode

You must see this, which suggests Encore has compiled and began your app, together with obligatory native infrastructure like PostgreSQL databases:

encore run

Whereas it is working, open http://localhost:9400/ to view Encore’s local developer dashboard.

local dev dashboard

From there you’ll be able to view API documentation, make API calls, and see native traces for every request.

That is what the board service appears to be like like within the built-in service catalog:

service catalog

🤔 The way it works: Companies & APIs

Defining companies

With Encore you create a microservice by defining one or more APIs inside an everyday Go package deal. Encore’s parser acknowledges this as a service and makes use of the package deal title because the service title.

Defining API endpoints

With Encore you solely want so as to add an annotation, //encore:api to an everyday perform to show it into and API endpoint.

You possibly can outline your individual schemas and use built-in auth-handers if you want. Be taught extra within the API schemas docs.📚

Making API calls

To make an API name between companies, you import the opposite service as a package deal, after which name it is APIs utilizing common perform calls.

When compiling your app, Encore robotically generates the required boilerplate code to wire up your software.✨

Versatile deployment methods

When deploying, Encore robotically provisions the required infrastructure for every service.

If you create your surroundings, you’ll be able to configure for those who want to deploy every service as a separate course of or for those who want to use a single course of for all companies.

This implies you’ll be able to deploy the identical code in several methods for various environments, to optimize for price and efficiency as required.💡

🤔 The way it works: Utilizing Databases with Encore

Encore treats SQL databases as logical assets and natively helps PostgreSQL databases.

To create a database, import encore.dev/storage/sqldb and name sqldb.NewDatabase, assigning the consequence to a package-level variable. (Databases have to be created from inside an Encore service.)

To start out utilizing the database, you solely must define the schema by making a migration file.

Encore takes care of provisioning the database, working new schema migrations throughout deploys, and connecting to it.

🕹 Let’s strive our new app

When your app is working, you’ll be able to ping the board.Create endpoint to create a brand new Trello board, which can get saved within the database:

curl 'http://localhost:4000/board.Create' -d '{"Identify":"my board"}'
Enter fullscreen mode

Exit fullscreen mode

Test the native growth dashboard (localhost:9400) to see the request you simply made and consider a hint of the response.🧐


🚀 Deploy to the cloud

Now let’s deploy our software to a staging surroundings in Encore’s free growth cloud. To do that, we solely must git push:

git add -A .
git commit -m 'Commit message'
git push encore
Enter fullscreen mode

Exit fullscreen mode

Then head over to the Cloud Dashboard to watch your deployment and discover your manufacturing URL.🌍

🍰 Nice job – you are performed!

You now have the foundations of a scalable and production-ready microservices backend working within the cloud.🎉

For actual production-scale site visitors, you’ll be able to join your cloud account (AWS/GCP) and deploy there with a click on. You possibly can deploy the identical code to varied companies like CloudRun, EKS, GKE, or Fargate.

Maintain constructing with these Open Source Encore App Templates.👈

In case you have questions or need to share your work, be part of the builders’ hangout in Encore’s community Slack.👈

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?