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
crud
directory contains code of CRUD framework. Refer docs.internal
directory contains the common code used by different modules.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.config.yaml
andconfig.docker.yaml
are samples of configuration file which are used while running the module locally on the host (or) via docker.Taskfile.yml
anddocker-compose.yml
in the project root directory controls how different modules are built and executed during local development.tools/tools.go
contains a list of Go tools which are necessary for the development.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
go.mod
andgo.sum
represents a Go module and contains module dependencies.main.go
containsfunc main()
which is the beginning of code execution in Go binaries.Taskfile.yml
contains task definitions which are specific to a module and primarily invoked from the parentTaskfile.yml
in the project root directory.Dockerfile
contains the instructions to build container image for a module.route
package contains API declarations. The project uses fiber library for handling API routes.controller
package contains API handlers, functions which process the REST requests and returns a response.model
package contains type definitions used for JSON request and response. It also contains the types which represents database tables and used with GORM.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
Some of the packages such as
server
,database
,config
andmigrate
are fundamental to the business logic.Some of the packages such as
logger
,utils
,lazyerrors
andmust
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
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.
To generate (or) update the contents of this directory, use the command
task swagger
from the project root (or) module root directory.