Skip to content

Golang Microservice Playground - REST, gRPC, GraphQL, Kafka, PostgreSQL, OpenTelemetry

License

Notifications You must be signed in to change notification settings

yolkhovyy/go-userv

Repository files navigation

User Management Services

This is a Go Microservice Playground project, implementing User Management Services with REST, gRPC and GraphQL APIs, PostgreSQL storage, and Kafka notifications.

Provides:

  • HTTP API
    • POST /api/v1/user
    • GET /api/v1/user/:id
    • GET /api/v1/users?page=1&limit=10&country=GB
    • PUT /api/v1/user/:id with JSON body
    • DELETE /api/v1/user/:id
  • gRPC/GraphQL API:
    • Create
    • User
    • Users
    • Update
    • Delete
  • User change notifications via Kafka
  • Health check
    • GET /health

Tools and Dependencies

Tools

  • Go 1.23.6
  • GNU Make 4.3
  • Docker 27.5.1
  • jq 1.6
  • graphviz 2.43.0

Dependencies

  • PostgreSQL - for storing user data
  • Apache Kafka - for notifying other services of user changes

Interactions

  • User HTTP/gRPC services handle user management requests
  • User data stored in PostgreSQL
  • Notifier notifies other services of user changes via Kafka

Interactions

Service Internals

Packages

Used Go Modules

  • Go-otelw for telemetry
  • Viper and Viperx for configuration
  • Gin for HTTP routing
  • Pgx and Pq PostgreSQL drivers for storage and notifications
  • Kafka client for publishing notifications

Interactions

REST

User Rest

gRPC

User gRPC

Directory Structure

go-userv
├── cmd
│   └── user-*
│       ├── config.go
│       ├── config.yml
│       ├── Dockerfile
│       └── main.go
├── db
│   └── postgres
│       ├── migrations
│       └── init.sql
├── contract
│   ├── dto
│   │   └── *.go
│   ├── graphql
│   │   └── *.graphql
│   └── proto
│       └── *.proto, *.pb.go
├── internal
│   ├── contract
│   │   ├── domain
│   │   ├── server
│   │   └── storage
│   ├── domain
│   ├── notifier
│   ├── router
│   │   ├── grpc
│   │   └── http
│   ├── server
│   │   ├── grpc
│   │   └── http
│   └── storage
│       └── postgres
├── make
│   └── *.mk
├── test
│   └── integration
├── docker-compose.yml
├── go.mod
├── go.sum
├── Makefile
└── README.md

Build System

The service build system is constructed with GNU Make and Docker BuildKit.

Make

  • Display help

    make help
  • All - install, lint, test, coverage, build, run

    make all

Step by step

  • Install project

    make install
  • Lint

    make lint
    • Currently limited to Go files only, mocks_*.go excluded.
  • Test

    make test
    • Runs:
    • Generates test results in JUnit XML Format, ready to be consumed by CI/CD pipelines, e.g. Jenkins, GitLab CI/CD, CircleCI, Azure DevOps, etc.
  • Make coverage report

    make coverage
    • This generates a coverage report in HTML format, try to open it in a browser
  • Generate dependency graph

    make deps
  • Build docker images

    make build
  • Run all

    make run
    • Runs User Service and 3rd party dependency services in Docker containers

Logs

  • All Logs

    docker compose logs -f
  • Selective Logs

    docker compose logs -f user-rest
    docker compose logs -f user-notifier kafka
    docker compose logs -f user-grpc postgres
  • Start dependency services only

    make doco-up-dependencies
    • Now you can run and debug the user-* service in vscode
  • Kafka consumer In 2nd terminal

    ./test/scripts/kafka-consumer.sh
    • This allows to observe user changes
  • Create, list, get, update, delete users In 3rd terminal:

    • REST - use UUID from the output of list-users.sh
      ./test/scripts/create-users.sh
      ./test/scripts/list-users.sh 
      ./test/scripts/get-user.sh UUID 
      ./test/scripts/update-user.sh UUID 
      ./test/scripts/delete-user.sh UUID
    • gRPC
    • GraphQL
  • Stop

    make stop
    • Stops all running services
  • Clean

    make clean
    • Stops running services, removes all containers, mocks and generated folders
  • Remove docker volume

    docker volume rm go-userv_user-data
    • Removes the docker volume with user data in PostgreSQL database
  • Health check

    ./scripts/health-check.sh
    • Returns health status of the User Service

TODO

  • More unit tests
  • API annotaions and documentation
  • Telemetry