Go + echo + PostgreSQL = web server
In this template, we will implement the reporting system. You should also be able to host a server through docker compose.
On your first initial commit, use this command
make init
It will automatically set the module name to
your-repository
name.
Go version 1.20
Echo v.4
PostgreSQL:14
Must use tools to make our life easier.
- GVM - go version manager
- Pre-commit - save the commits and CI/CD runs on remote
- Make - use the aliases instead of typing the long commands
- GoLand - all essential tools or just use VS code if you want to custom your editors a lot
- Docker + AIR - blazing-ly fast hot reload
- OpenAPI - REST building tools, but the generated code could be terrible sometimes.
If you wonder what each folder does, just read the README.md
inside.
go mod tidy
Install golangci-lint for linting.
Once installed, you should be able these lint commands.
Lint check and automatically fix.
make lzl
Make your life easier by integrate it to format your code.
GOLAND TIP!
argument: run --fix
brew install pre-commit
precommit install
Create .env.development
based on .env.example
for development. For production, create .env.production
and set APP_ENV=production
.
In dev stage, you need creds from your PM or Devs and put them to secrets
before running the following command.
docker compose up
The exposed port should be 32423
and 2345
I suggest writing the spec.yaml
on separated platform, use them for communication.
Build and run the project.
make run
Build the project.
make compile
Inject the stubs and test by functions. Also, keep the code coverage up to 80%+, you may leave the failed cases on a rush.
Test the project.
make test
make lzm
Using mockgen, we can just run the above command to lazy mock.
You can generated mock client using the following command as an example. Mock only works on interface
mockgen --source=services/secret_manager_service.go --package=mocked SecretClient > controllers/mocked/secret_service.mocked.go
Or use go:generate magic comment
mockgen -source=$GOFILE -destination=../mocks/your_file.mock.go -package=mocks
go generate ./repositories ./services
Only in docker, you can migrate tables
make mig-up
Only in docker, you can remove tables
make mig-down
Define each service init functions and pack it up with the Initialize function.
make wire
Follow the Controller-Service-Repositories pattern. We can test only just the controllers and services, leaving out some coverages to mock offline.
- Any loop must be stored in a function (1 for-loop per function)
- Nested If is cursed code. Please replace it with nested functions
- Bundling up erroneous functions into 1 function and up to 15 functions can be stored
- Define structs for any result from function can help reducing the complexity (Export_full_report.go is a good example)
- Public functions should be at the top, and leave private functions at the bottom.
- Structs should better be generated from spec.yml
- If possible, create a struct for any function that require expansion later on. (Server Parameters for example)
- Proofread your code again and make sure that it is understandable without comments in the code.
This could efficiently save your time.
- If you name your function uniquely, it is quicker for IDE to search the project for usages (CreateSomething, PatchSomething, UpdateSomething).
- Use debugger (TBC on docker)
- You can let the lint format automatically by saving your files
Set up your environment as followed, you can either check your docker compose
LOG_LEVEL
- Set toerror
,warn
, ordebug
.APP_ENV
- Set todocker
orproduction
This project uses Git Flow
- To deploy dev, just push to develop branch
- To deploy QA, merged from develop to a new branch name release/*, formatted it as release/
- To deploy UAT and Production, merged from release branch to main branch and create a version tag (v)
Assume that you have the container,
docker cp path/to/dump.sql <container-name>:/
docker exec -it <container-id> /bin/sh
psql -U <username> -d <database> -f dump.sql
Question 1 : Why my docker container cannot find the database?
Check the database port in .env.docker
, make sure it is matched to the container exposed port.
We found that it switches between 5432
and 5458
.
Question 2 : I need assistance ASAP! Please contact Possawat or Narongdej in MS team.