Dart on back as micro-services is that it?
Hello dev, if you came here because you are a Flutter developer, probably is because you need do create a backend for your project, but if you are already a backend developer, you might be interested how Dart behaves on the backend side.
So, in this article I’m going to show a very easy example and talk about create a micro-service using Dart. Off course I’m not an expert on that but I’ve been trying to study because I’m curious lol, so let’s get start.
First of all, we need to prepare the environment in order to create the project and see everything working, and if you already have the Flutter installed on your machine you just have to put the Dart path on you environment variables, however if you just want to try it on the backend you just need to install the Dart on your machine and follow the next steps.
Ok, once you have the Flutter or the Dart installed on your machine, let’s configure the environment variables and in my case I’m using a Mac, if you are using a different operating system you have to see how to add environment variables on it. On the Mac you just have to create a .zshrc file inside your user root folder and add those line as the image below. But pay attention to replace your user name or even the location that you have things installed.
Cool after that open the terminal and type “dart --version” to see if you can use the Dart command correctly, and if is ok you will see the version of the Dart installed, if not you wont be able to use this command.
Now let’s move to the fun part, and to create this backend micro-service we are going to install the darfn framework. But wait, if don’t know what is it I’ll explain briefly… basically this framework helps with some abstractions to create a functions as a service. Let’s put it in practice to understand better.
Open your terminal and type this command “dart pub global activate dartfn” and now you are good to go, easy as that. And to start a new project go to your desired folder on terminal and type “dartfn generate helloworld”, you will have something like the image below.
The structure it self is pretty much like the Flutter project structure, and if you are familiar with it you know that we need to create the classes inside the lib folder, so let’s go ahead and take a look on the file inside it. And as we can see in the image below, inside we have this functions.dart file which is the place we are going to have our simple example for now.
Simple as that, we have a function which receives a request object and give back a response with this “Hello, World” string. Let’s run this and see what happens.. by the way.. every time you start studying a new language execute first a “Hello World” example to be successful on your future projects. With that been said, open the terminal in the root folder of your project and execute the command “dart run bin/server.dart” and you will have something like the image below.
For instance the default port will be 8080 but you can choose a different one, latter I’ll show how. Now open a web browser and type “http://localhost:8080”, to se if it’s working and you probably have something like the image below.
All right, but where this server.dart came from? If you open the bin folder you will find the server.dart file, so let’s open and take a look on that. In the image below you can see that there is some code, and basically this is where all your functions will be declared in order to be funded and executed, and you don’t have to declare manually, because every time you create another function you just have to execute the command “dart run build_runner build --delete-conflicting-outputs” and your new function will be generated automatically like the image below. But first stop your server hitting cmd + c on your terminal and then execute the command above.
Ok, now let’s run this two functions and test if we can access both. For this we are going to specify the port to run each one, because remember that we are creating micro-services. To do that execute in one terminal the following command “dart run bin/server.dart --port 8080 --target function”, and in other terminal the command “dart run bin/server.dart --port 8080 --target function2”. Now you have something like the image below.
And again open your web browser and try to access the “http://localhost:8080” and the “http://localhost:8081” to see if it is working.
Now kill the function2 process and try to access both endpoints, you probably have seen that the function process is independently from the function2 right? Cool thats the objective, and now we can have multiple process running separately, micro-services definition!
Imagine having an entire team working with the same language, would be cool right? And if you got interested I will let on repository with a more complex code example with database connection and more, and also if you want to deploy it somewhere try to do in Google Cloud Functions because they already have support for it.