Skip to content

Repo structure and components

Aman Gupta edited this page Apr 16, 2020 · 1 revision

Welcome to the bookstore_users-api wiki!

MVC structure for a golang REST API

Main.go
   |
   v
+--+-----------+        +--------------+
| Application  +------->+ Controller   |
+--------------+        +------+-------+
                               |
                               v
                        +------+-------+
                        | Services     +
                        +--------------+
                               |
                               v
                        +------+-------+        +----+
                        | Domain       +------->+ DB |
                        +--------------+        +----+
  • MVC development is opposite to domain driven developement

APPLICATION

  • We invoke the application via main.go
  • In application we initiate our choice of http router and only initiate only this time throughout the dev process.
  • All the routes/url_mappings are also defined in applications

CONTROLLER

  • We define the handler functions for url_mappings inside the controllers.
  • In controller we basically process the request body and parameters (ie JSON marshalling/unmarshalling) and send to the functions in SERVICES.

SERVICES

  • In services, we define our business logic functions.
  • These functions are basically same as controllers, but we define here how we want to deal with data internally, i.e encrypting password or create current time etc.

DOMAIN

  • This is the main component of our application.
  • We define our main object definition here i.e. the type struct in the data_transfer_object (dto).
  • We will also have our data_access_object(dao) here, which talks to the database and we define all the database queries here.

DATASOURCES

  • Here we initiate our database and its required go packages, in our case it was database/sql and go-sql-driver/mysql.

Using gogin as an http framework

Making Utils for errors, mysqlerrors, date etc.

Connecting mysql database via go/database/sql library

Using custom logger --> Uber/zap