This project is to create a rest api using go for local my project
# create a new go module
go mod init local_my_api
# to install new package
go get -u github.com/gorilla/mux
# to list all the dependency
go mod vendor
# to install dependency
go mod tidy
# to build
go build -o local_my_api cmd/main.go
This project uses go module and dependency injection pattern for the project structure. Implcitly define interface and struct is a must for the project structure.
cmd
- for initial entry point and for building the projectinternal
- for all the internal package and logicpkg
- for all the external package and logictesting
- for all the testing purposeinternal/routes
folder and combine them before accessing the server for separation of concernhandler
-> service
-> repository
Recommended to use air
for live-reloading (rebuild all for each changes) and go run
for running the server without building the project go run cmd/main.go
Migrations by go lang is shit. Implemented custom bin sh for migrations purpose
chmod +x go_migrate.sh
./go_migrate --migration_name_by_snake
sudo apt-get install xclip
). Should output like thisβ ./go_migrate.sh --create_product_table
Migration file created: internal/db/migrations/20240825014020_create_product_table.go
File name copied to clipboard using xclip.
internal/db/migrations.go
This project uses Docker to deploy. To disable web app, comment out in yml file
'user=postgres password=postgres dbname=local_my host=db port=5432 sslmode=disable'
docker-compose.yml
as well in user and password./build.sh
) and ensure the local/vite:tag
is the same in docker composeversion.txt
and run ./build.sh
local/go:tag
docker-compose -f docker-compose.local.yml up -d
version.txt
and run ./build.sh
ghcr.io/hrithiqball/go:tag
ghcr.io/hrithiqball/go:tag
and ghcr.io/hrithiqball/vite:tag
is the same in docker composedocker-compose -f docker-compose.prod.yml up -d