For a couple of years I've been working with Golang, is such a great experience, Golang is a great language, in my opinion, simple, fast, and focused on high performance. But, not only it can help us to create a microservice that is fast, but we can also code quite quickly.
In Golang we could create a microservice skeleton, but for that, we would need some basic structure to work on. Thinking of that we could use an approach to solve that problem as we are used to develop a golang application, which is the Clean Architecture. (if you don’t know what it is, you can see more here, I will not add here the benefits or why you should work with it.)
After a long time creating new applications with such structure, manually.
.
├── config
│ ├── .gitkeep
├── controller
│ ├── .gitkeep
├── domain
│ ├── .gitkeep
├── gateway
│ ├── .gitkeep
├── usecase
│ ├── .gitkeep
├── README.md
├── go.mod
├── go.sum
└── main.go
I could find a nice CLI tool called golangspell with the purpose to help us, developers, to quickly create a new application based on the previous structure on top of the Echo web framework, then we can focus on the domain. Since then I am using it for every new application in Golang, it's simple and clean, I can quickly jump to the business details and deliver at least a POC on the same day.
To install it, it’s super simple
go get github.com/golangspell/golangspell
Then, to create a new project you just need
cd /path/to/new/project
golangspell init github.com/igor822/spell-test spell-test
The output will be
Loading Spells ...
Spells loaded!
Rendering template: .gitignore
Rendering template: Dockerfile
Rendering template: README.md
Rendering template: context.got
Rendering template: context_test.got
Rendering template: environment.got
Rendering template: logger.got
Rendering template: version.got
Rendering template: health_controller.got
Rendering template: health_controller_test.got
Rendering template: info_controller.got
Rendering template: info_controller_test.got
Rendering template: router.got
Rendering template: router_test.got
Rendering template: model_health.got
Rendering template: model_info.got
Rendering template: logger.got
Rendering template: main.got
Rendering template: .gitkeep
go: creating new go.mod: module github.com/igor822/spell-test
Application created!
---------------------------------------------------------
To run your application, first build it with the command:
go build
---------------------------------------------------------
Then execute the application with the command (Unix based systems):
./spell-test
Then execute the application with the command (Windows based systems):
spell-test
---------------------------------------------------------
Available endpoints:
http://localhost:8080/spell-test/v1/info
http://localhost:8080/spell-test/v1/health
---------------------------------------------------------
Find more details on README.md file
Golangspell will provide you already a Dockerfile to containerize your application and also some common tools like
logger
and appcontext
where you can “store your objects” in memory to reuse it, like a repository. Appcontext it's a nice solution to use as a "container" of services inside your application.And simple as you can see in this example, it's done, you don’t need to manually write a bunch of code that you need to configure router, controllers, and even the port that your application will run. For sure it must have several new tools and improvements, like implement some util to connect quickly with NoSQL or MySQL, or even quickly installing gorm and the developer will be able to connect to a database really fast.
My idea here is to share a nice tool that I tested and was able to deliver a small service in less than a day, already deploying to production, without issues and allowing me to focus directly in my problem instead to worry about the noise of the standard code we need to write.
Also published at https://medium.com/@igor_a_carvalho/creating-applications-with-golangspell-608b971f20b8