Project Structure


This document gives an overview of the project structure of HashConnect backend.

1. Multiple Modules

HashConnect consists of multiple modules. Each module is responsible for a set of business functionality, and the code resides in their respective directories from the project root.

$ cd hashconnect $ tree --dirsfirst -L 1 . ├── bin ├── crud ├── internal ├── migrations ├── {module 1} ├── {module 2} ├── {module N} ├── swagger ├── tools ├── README.md ├── Taskfile.yml ├── config.docker.yaml ├── config.yaml ├── docker-compose.yml ├── go.mod ├── go.sum ├── traefik.yml └── trivy-ignore.yml

2. Common Files

  1. crud directory contains code of CRUD framework. Refer docs.

  2. internal directory contains the common code used by different modules.

  3. migrations directory contains SQL migration scripts. The project uses golang-migrate to execute these scripts. The table schema of the database and the migration scripts are common for all the modules.

  4. config.yaml and config.docker.yaml are samples of configuration file which are used while running the module locally on the host (or) via docker.

  5. Taskfile.yml and docker-compose.yml in the project root directory controls how different modules are built and executed during local development.

  6. tools/tools.go contains a list of Go tools which are necessary for the development.

  7. bin and .pgdata are git-ignored directories which are only created and utilized during local development.

3. Module Structure

Though the specifics of each module are different, the project uses a common pattern when it comes to a Golang module.

$ tree tas --dirsfirst -L 1 tas ├── controller ├── model ├── query ├── route ├── Dockerfile ├── main.go └── Taskfile.yml
  1. go.mod and go.sum represents a Go module and contains module dependencies.

  2. main.go contains func main() which is the beginning of code execution in Go binaries.

  3. Taskfile.yml contains task definitions which are specific to a module and primarily invoked from the parent Taskfile.yml in the project root directory.

  4. Dockerfile contains the instructions to build container image for a module.

  5. route package contains API declarations. The project uses fiber library for handling API routes.

  6. controller package contains API handlers, functions which process the REST requests and returns a response.

  7. model package contains type definitions used for JSON request and response. It also contains the types which represents database tables and used with GORM.

  8. query package contains functions which translates business logic into SQL queries. This package acts as an abstraction to interact with the database.

4. Common Packages

internal directory contains various packages are used different modules.

$ tree internal --dirsfirst -L 1 internal ├── config ├── constants ├── database ├── lazyerrors ├── logger ├── middleware ├── migrate ├── must ├── server └── utils
  1. Some of the packages such as server, database, config and migrate are fundamental to the business logic.

  2. Some of the packages such as logger, utils, lazyerrors and must contains helper routines used by the business logic.

5. Swagger Documentation

swagger directory contains Swagger API specification represented as both JSON and YAML. It also contains code for handling /swagger endpoint and Swagger UI.

$ tree swagger swagger ├── docs │   ├── docs.go │   ├── swagger.json │   └── swagger.yaml └── main.go
  1. The contents of this directory are auto-generated by a Go tool - swaggo which makes use of the comments (aka swagger annotations) in the module source code.

  2. To generate (or) update the contents of this directory, use the command task swagger from the project root (or) module root directory.

Related content