Skip to content

API for product management, with user system, Oauth authentication and JWT Bearer authorization.

Notifications You must be signed in to change notification settings

herbetyp/go-product-api

Repository files navigation

Go Product API 🌐

↘  API for product management, with user system, authentication and authorization.  ↙

Go Postgres Docker Redis

  

✨ API Features

Routes that only the admin user can access are marked with the flag ⛔

  • OAuth Basic Authentication Endpoint
  • Authorization from JWT Bearer Token
  • Create User
  • Get User
  • Update User (username, password)
  • List Users
  • Delete User (soft delete, hard delete) ⛔
  • Recovery User
  • Active/Deactive User
  • Create Product
  • Get Product
  • List Products
  • Update Product
  • Delete Product (soft delete, hard delete) ⛔

  

🔧 Run localy application:

  1. Copy and export variables (Run in root project)

cp -r docs/samples/*.sample $PWD; for i in *.sample ; do mv "$i" "$(basename "$i" .sample)" ; done && source envs.sh

Note

When you restart the terminal session, you will need to run the source envs.sh command again.

  1. 🔑 Genereta RSA private key for assign JWT token
openssl genpkey -out rsa_pvkey.pem -algorithm RSA -pkeyopt rsa_keygen_bits:2048
  1. 🐋 Run application in Docker container (with Docker Composer module) or directly from terminal

# using docker container, running on http://localhost:3000
docker compose up -d
# using direct in terminal, running on http://localhost:5000
docker compose up -d go_product_api_db go_product_api_cache
go run cmd/main.go
  1. 🧩 Pre commit (For development only)
pip intall pre-commit # install pre-commit with python pip
go install golang.org/x/tools/cmd/goimports@latest # pre-commit hook
go install github.com/golangci/golangci-lint/cmd/[email protected] # pre-commit hook
pre-commit install # install pre-commit hooks

🔐 Create user admin

Create user:

curl --request POST \
  --url http://localhost:3000/v1/users \
  --header 'Content-Type: application/json' \
  --data '{
	"username": "Anonymous1",
	"email": "[email protected]",
	"password": "mypassword123"
}'

Important

By default, users are created with active set to false, run the following command to make the admin user active and the system unlock:

docker exec -ti $DB_CONTAINER_NAME psql -U $DB_USER \
-d $DB_NAME -c "UPDATE public.users SET active = true, is_admin = true WHERE id = 1;"

🛡️ Generate JWT token

curl --request POST \
  --url http://localhost:3000/v1/oauth2/token \
  --header 'Content-Type: application/json' \
  --data '{
      "email": "[email protected]",
      "password": "mypassword123",
      "grant_type": "client_credentials"
  }'

🔍 Run localy tests

  • K6 simple tests
# build container image
docker build tests/performance -t go_product_api_k6
# run spike_k6_test.js example
docker run --network go-product-api_default --rm go_product_api_k6 run spike_k6_test.js
  • Unit tests

WIP

⚙️ Application systems

  • JSON format output log
  • Memory in cache (Redis)
  • Rate limiting by IP
  • Database auto migrations

🎯 Developed with:

About

API for product management, with user system, Oauth authentication and JWT Bearer authorization.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published