Skip to content

Commit

Permalink
User module gRPC setup
Browse files Browse the repository at this point in the history
  • Loading branch information
wondenge committed Feb 17, 2022
1 parent eb8e783 commit dc051bc
Show file tree
Hide file tree
Showing 16 changed files with 1,353 additions and 0 deletions.
3 changes: 3 additions & 0 deletions user/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Build artifacts
user
!user/
11 changes: 11 additions & 0 deletions user/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: dev deps proto

proto:
$(MAKE) -C proto proto

deps: proto
go mod download

dev:
air -c air.conf

23 changes: 23 additions & 0 deletions user/air.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
root = "."
tmp_dir = "/tmp"

[build]
cmd = "go build -o user user.go"
bin = "user"
log = "air_errors.log"
include_ext = ["go"]
exclude_dir = ["tmp", "docs", "node_modules"]
delay = 1000 # ms

[log]
time = true

[color]
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
clean_on_exit = true
28 changes: 28 additions & 0 deletions user/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

import (
"fmt"

"github.com/kelseyhightower/envconfig"
)

// Specification struct to load environment variables.
type Specification struct {
MetricsPort string `default:"9099" split_words:"true"`
Port string `default:"8888"`
GRPCReflection bool `default:"false"`
Env string `default:"branch" split_words:"true"`
LogLevel string `default:"debug" split_words:"true"`
}

// Parse reads config from environment.
func Parse() (Specification, error) {
config := Specification{}

err := envconfig.Process("", &config)
if err != nil {
return config, fmt.Errorf("reading config: %w", err)
}

return config, nil
}
25 changes: 25 additions & 0 deletions user/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSpecification_Parse(t *testing.T) {
os.Setenv("PORT", "9001")
os.Setenv("METRICS_PORT", "9002")
os.Setenv("ENV", "iire9457fjfjjkfjkf")
os.Setenv("LOG_LEVEL", "kdkoe8499599589jfjf")

spec, err := Parse()

if assert.Nil(t, err) {
assert.Equal(t, spec.Port, "9001", fmt.Sprintf("Expected Port %v, Got Port %v", 9001, spec.Port))
assert.Equal(t, spec.MetricsPort, "9002", fmt.Sprintf("Expected Port %v, Got Port %v", 9002, spec.MetricsPort))
assert.Equal(t, spec.Env, "iire9457fjfjjkfjkf", fmt.Sprintf("Expected Port %v, Got Port %v", "iire9457fjfjjkfjkf", spec.Env))
assert.Equal(t, spec.LogLevel, "kdkoe8499599589jfjf", fmt.Sprintf("Expected Log Level %v, Got Log Level %v", "kdkoe8499599589jfjf", spec.LogLevel))
}
}
46 changes: 46 additions & 0 deletions user/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module github.com/crafted-systems/smartcollect-pro/user

go 1.17

require (
github.com/crafted-systems/smartcollect-pro/common/go v0.0.0
github.com/crafted-systems/smartcollect-pro/common/go/logger v0.0.0
github.com/golang/mock v1.6.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.21.0
google.golang.org/grpc v1.41.0
google.golang.org/protobuf v1.26.0
)

require (
github.com/TheZeroSlave/zapsentry v1.9.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/crafted-systems/smartcollect-pro/common/go/sentry v0.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/getsentry/sentry-go v0.11.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

replace (
github.com/crafted-systems/smartcollect-pro/common/go v0.0.0 => ../common/go
github.com/crafted-systems/smartcollect-pro/common/go/logger v0.0.0 => ../common/go/logger
github.com/crafted-systems/smartcollect-pro/common/go/sentry v0.0.0 => ../common/go/sentry
github.com/crafted-systems/smartcollect-pro/common/proto v0.0.0 => ../common/proto
)
679 changes: 679 additions & 0 deletions user/go.sum

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions user/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package logging

import (
"fmt"

"go.uber.org/zap"

logger "github.com/crafted-systems/smartcollect-pro/common/go/logger/defaultlogger"
)

// Configure configures the logging for the server.
func Configure(logLevel string) error {
l, err := logger.NewLogger(logLevel)
if err != nil {
return fmt.Errorf("creating new logger: %w", err)
}

zap.ReplaceGlobals(l)

return nil
}
32 changes: 32 additions & 0 deletions user/metrics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# :chart_with_downwards_trend: Metrics

## Getting started

We use [Prometheus](https://prometheus.io/) for sending metrics.
To start to instrument your application, you can use [this Prometheus Go client](https://github.com/prometheus/client_golang).
You can create a `metrics.go` file under `metrics`.
An example code would be:

```go
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
ExampleCount = promauto.NewCounter(
prometheus.CounterOpts{
Name: "service_metrics",
Help: "The total number of something",
},
)
)
```

You can see the added metric on the `metrics/` endpoint that is exposed from `server.go`.

You can find our common metrics in `common/go/metrics`.

For more information and examples, take a look at the [official docs](https://prometheus.io/docs/guides/go-application/)
17 changes: 17 additions & 0 deletions user/proto/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.PHONY: proto protoc mock

PROTOC_VERSION=1.42_1

proto: protoc mock

protoc:
docker run --rm \
-v `pwd`:/defs/proto \
-w /defs \
--entrypoint="" \
namely/protoc-all:$(PROTOC_VERSION) \
bash -c \
"protoc --proto_path=/defs --go_out=paths=source_relative,plugins=grpc:. --doc_out . --doc_opt=markdown,PROTO.md /defs/proto/user.proto"

mock:
mockgen -source user.pb.go -destination mock/user.mock.pb.go -package mock_proto
95 changes: 95 additions & 0 deletions user/proto/mock/user.mock.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dc051bc

Please sign in to comment.