This is an example golang application. Commands list:
- Daemon - main service
- Product service - service that returns Product, an example of gRPC client/server interaction
- Health check service - this service is needed to show how convenient to understand on which of the services an error occurred in jaeger
- Migrate - commands for migration
- JWT - commands for generate JWT token
Packages which use in this example project
- sql-migrate - SQL migrations
- wire - dependency Injection
- viper - environment configuration
- cobra - create commands
- cast - easy casting from one type to another
- gorm - database ORM
- zap - logger
- mux - http router
- nats-streaming - NATS Streaming System
- gqlgen - graphql server library
- protobuf - Google's data interchange format
- grpc - RPC framework
- opentelemetry - OpenTelemetry
- jaeger - Jaeger Bindings for Go OpenTelemetry API
- casbin - Supports access control
- dataloaden - DataLoader for graphql
- nats - Golang client for NATS, the cloud native messaging system
Install the Golang and GO environment
https://golang.org/doc/install
Install Postgresql (if you want to run locally)
Clone repository
git clone git@github.com:Aristat/golang-example-app.git (go get)
Install Golang packages without modules
make install
Install database
make createdb
Sql migrations
sql-migrate up
Install Golang dependencies
make dependencies
Generate artifacts(binary files and configs)
make build
Packages for proto generator
https://grpc.io/docs/languages/go/quickstart/#prerequisites
Set APP_WD if you start to use html templates or path to ssh keys or run make test
export APP_WD=go_path to project_path/resources or project_path/artifacts
Generate docker image
DOCKER_IMAGE=golang-example-app TAG=development make docker-image
Up jaeger in docker-compose or disable Jaeger(and rebuild binary file) in resources/configs/*.yaml
docker-compose up jaeger
Start daemon (main service)
make start
or
./artifacts/bin daemon -c ./artifacts/configs/development.yaml -d
Start product service
./artifacts/bin product-service -c ./artifacts/configs/development.yaml -d
Start health-check service
./artifacts/bin health-check -c ./artifacts/configs/development.yaml -d
docker-compose rm # Remove previous containers
REMOVE_CONTAINERS=on DOCKER_IMAGE=golang-example-app TAG=development make docker-image # Generate new docker image
docker-compose up
./scripts/docker-compose-start.sh
http://localhost:16686
http://localhost:9096/products_grpc
http://localhost:9096/products_nats
http://localhost:9096/products_slowly
Graphql client for testing. End-point http://localhost:9096/query
.
Generate JWT for Graphql authorization
./artifacts/bin jwt token --key='./artifacts/keys/local/private_key.pem' --fields='{"sub": "owner", "iss": "test-service"}'
Set JWT token in headers
{
"Authorization": "bearer token"
}
Example query
query oneUser {
users {
one(email: "test@gmail.com") {
email
id
}
}
}
Example query with data loader
query allProducts {
products {
list {
products {
id
productItems {
id
name
}
}
}
}
}
Example mutation
mutation createUser {
users {
createUser(email: "test1@gmail.com", password: "123456789") {
id
email
}
}
}
➜ make test