- go 1.16
- gin-gonic for routing
- Postgre sql
- gorm for PostgreSQL database access
- crypto/bcrypt for password crypt
- testify, mockery for unit test
- swagger for api documentation
- go modules for package management
- dockerized environment
- structure design inspired by https://github.com/golang-standards/project-layout
- Setup docker environment use "docker-compose.yml" to docker-compose up.
- Go to project folder directory and execute
# start docker environment
$ docker-compose up -d --build
# list running services
$ docker-compose ps
# stop all containers
$ docker-compose stop
# remove all containers
$ docker-compose rm
-
Download posgresql from https://www.postgresql.org/download/ and Setup
-
On macos download from https://postgresapp.com
-
If download the server find pg_hba.conf file and change IPv4 and IPv6 connections trust (not scram-sha-256 or md5)
-
Setup postgresql connection variables with local.env file or just setup yours and change parameters inside local.env
POSTGRES_HOST=localhost
POSTGRES_USER=postgres
POSTGRES_DB=DBNAME
POSTGRES_PASSWORD=STRONGPASSWORD
- After installation create DB on postgre with your given "DBNAME" then just go to project directory and execute
$ go run main.go
✨-postgre sql runs automatically on 5432 port ✨ server run on 8080 port
Create mocks : Go to project folder directory and execute
$ go generate ./...
Run Tests : Go to project folder directory and execute
$ go test -v ./pkg/service/...
- PUT http://localhost:8080/v1/api/users
- body: { "Name": "Arden", "Email": "arden@mail.com", "Password": "strongpassword123" }
- returns auth token of inserted user
curl --location --request PUT 'http://localhost:8080/v1/api/users' \
--header 'Content-Type: application/json' \
--data-raw '{"Name":"Arden", "Email":"arden@mail.com", "Password":"strongpassword123"}'
- POST http://localhost:8080/v1/api/auth/login
- body: { "Email": "arden@mail.com", "Password": "strongpassword123" }
- returns auth token of loggedin user
curl --location --request POST 'http://localhost:8080/v1/api/auth/login' \
--header 'Content-Type: application/json' \
--data-raw '{"Email":"arden@mail.com", "Password":"strongpassword123"}'
- GET http://localhost:8080/v1/api/users/1
- headers : { "Authorization": "AUTHTOKEN"}
curl --location --request GET 'http://localhost:8080/v1/api/users/1' \
--header 'Authorization: AUTHTOKEN'
- GET http://localhost:8080/v1/api/users
- headers : { "Authorization": "AUTHTOKEN"}
curl --location --request GET 'http://localhost:8080/v1/api/users' \
--header 'Authorization: AUTHTOKEN'
- PATCH http://localhost:8080/v1/api/users/1
- headers : { "Authorization": "AUTHTOKEN"}
- body: { "Name": "ArdenNew", "Email": "arden_new@mail.com", "Password": "strongpassword1234" }
curl --location --request PATCH 'http://localhost:8080/v1/api/users/1' \
--header 'Authorization: AUTHTOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{"Name":"ArdenNew", "Email":"arden_new@mail.com", "Password":"strongpassword1234"}'
- DELETE http://localhost:8080/v1/api/users/1
- headers : { "Authorization": "AUTHTOKEN"}
curl --location --request DELETE 'http://localhost:8080/v1/api/users/1' \
--header 'Authorization: AUTHTOKEN'
Note: Response model showen on below
type Response struct {
Code int `json:"code"`
Status bool `json:"status"`
Message string `json:"message"`
Errors interface{} `json:"errors"`
Data interface{} `json:"data"`
}
Or just run the app, and browse to http://localhost:8080/swagger/index.html, but not forget to authorize on swagger.