- Golang 1.17 or above
- Git
- PostgreSQL
- SQL-MIGRATE tool
- Postman
- VSCode
NB: All commands below should be run on git bash or any bash supported terminal.
- Download and install git from here. This should also install git bash which can be opened from the start menu.
- Download and Install PgAdmin 4 from here. This should come with postgresql and should automatically run after install on port 5432. Make sure to take note of your passwords, there should be a PgAdmin password and the default user password. After install, open git bash and run the following commands to check if postgres is running on same port
- Connect user (use the default username, should be postgres if not changed)
psql -U postgres
- Check connection status
\conninfo
You should see output similar to the below
- Start PgAdmin from windows start menu and input your passwords.
- Create a database called
godb
with the user postgres.
- Install latest Golang 1.*. Download the variant for your operating system here
- After install, open git bash and type the command below to see if you get the help page
go version
- install ginkgo
go install github.com/onsi/ginkgo/v2/ginkgo
Open terminal/command line and try the command below and you should see the output.
ginkgo version
- Clone this repository
git clone https://gitlab.com/clasence/gobase.git
- Go to root directory and run this command to install go pakages.
go mod tidy
- Make a copy of the .env.example and rename it to .env
- Update the connection string on line 1 to include your postgres username and password (update the uppercase fields below )
CONNECTION_STRING=host=localhost port=5432 user=USERNAME dbname=godb password=PASSWORD sslmode=disable
-
Go into the .vscode/launch.json file the
program
on line 4 to the full path to the project's root. -
Install postman and create an account. Download from here.
-
Import the json filed in
/postman/gobase.postman_collection.json
into postman.
Run the postman test Add User Success
and it should be successful.
--migrate
--migrations/ file contains all migrations
--dbconfig.yml file contains configuration for sql-migrate tool. \
--pkg
--api functions which receive the api calls from the routes and calls the services. They also write the responses from the services.
--database functions to connect to the database and perform any db operation.
--models all database models used to connect to the database.
--registering, ...ing Folders ending in ing
are the services which have all the business logic. They have the service.go file which has the logic and the service_test.go file which has the tests of the logic. The errors.go file contains all the errors. The rest of the folders contain structs used in the business logic.
--postman This contains the postman tests
--.env.example This contains all the variables which are application secrets. To run the code, you will have to create a .env
file from this file and change all the variables to the ones you use locally.
--main.go The main file which connects to db and services and also starts running the code on the specified port (5010) in this case.
--services.go This file initializes all services.
--routes This file contains all the routes of the project. \
Ideally, all services in the project should have a service_test.go
. To run each test from vscode, open the file and click on the run test
or run go test ./...
from the root folder.