Sitemap
DavidNazareno

DavidNazareno

Press enter or click to view image in full size
https://www.reddit.com/r/golang/comments/6gzj63/wallpaper_golang/
https://www.reddit.com/r/golang/comments/6gzj63/wallpaper_golang/

Golang Basic Structure API

3 min readMay 23, 2020

--

Golang is an in a compiled language with power and simplicity in terms of its syntax that we have fallen in love with all programmers who work with every day.

Today in this story I will talk about how we can create a basic golang structure to organize our work files in case we are making an API.

To start I have this project is an Api which connects to a database in mongodb, and it is simply for data return in case of requests. If you can see there I have these folders:

  • Config
  • Handlers
  • Helpers
  • Middleware
  • Models
  • Routes
  • Services
  • Security

Now I will explain what functionality each folder has.

Config

This folder contains the codes referring to the project configuration, for example there I have the method that looks for the .env file and traverses its variables which I have configured for the project, such as the URL of the database, its credentials, etc…

Handler

A Handler is a rather ambiguous concept in the sense that there is no univocal definition of what it is, or how it should be built, but in my project I use the Hanlder as a controller of access routes and security for these cases I use mux and cors.

mux : https://github.com/gorilla/mux

cors : https://github.com/rs/cors

Helpers

In the helpers folder I keep all the functions that I use temporarily as tools, for example encrypting a password, converting data types, etc.

Middleware

Middleware has a very important function in the case of apis since it is the layer through which, before entering the logical process of response or data entry, that layer interferes in the verification of what you need, in my case I have in the middleware folder I have two functions:

Get DavidNazareno’s stories in your inbox

Join Medium for free to get updates from this writer.

1- Verification to the database, each time an Api call is made it verifies if the database is connected.

2- Verification that the communication token generated by the login is correct and its life of use has not ended.

Models

This folder is very common and you should already know more or less what it is for, but in case I don’t explain it.

The models’ folder is where all the required models are added in sending data or receiving data, it is usually used to reflect the same fields that are in the database so you have better control.

Routes

In the routes folder there are all the methods that are called when making a request to the api.

Security

This folder I use it to store all the functions based on the authentication and the security of the Api for example there is I have a method that is the one that controls all the processes referring to jwt.

jwt : https://jwt.io/

Services

For projects the services folder is one of the most important since it contains all the functions which are based on the functionality of your api.

In my project, when passing the routes functions, I call the services which do all this communication process between the data until its exit, so that the Api can return the information processed by request of the app that is consuming the Api.

--

--