This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Makefile
219 lines (172 loc) · 6.81 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#! /usr/bin/make -f
# Project variables.
VERSION := $(shell git describe --tags)
BUILD := $(shell git rev-parse --short HEAD)
PROJECT_NAME := $(shell basename "$(PWD)")
MARKET_SERVICE := worker
MARKET_API := api
MARKET_SEED_DB := seed
MARKET_PROXY := proxy
MARKET_PG_HEALTH := pg-health
# Go related variables.
GOBASE := $(shell pwd)
GOBIN := $(GOBASE)/bin
GOPKG := $(.)
# A valid GOPATH is required to use the `go get` command.
# If $GOPATH is not specified, $HOME/go will be used by default
GOPATH := $(if $(GOPATH),$(GOPATH),~/go)
DOCKER_REDIS_IMAGE_NAME := redis
DOCKER_LOCAL_DB_IMAGE_NAME := test_db
DOCKER_LOCAL_DB_USER :=user
DOCKER_LOCAL_DB_PASS :=pass
DOCKER_LOCAL_DB := watchmarket
DOCKER_REPOSITORY := trust/watchmarket
HASH ?= local
# Environment variables
CONFIG_FILE=config.yml
# Go files
GOFMT_FILES?=$$(find . -name '*.go' | grep -v vendor)
# Use linker flags to provide version/build settings
LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)"
# Redirect error output to a file, so we can show it in development mode.
STDERR := /tmp/.$(PROJECT_NAME)-stderr.txt
# PID file will keep the process id of the server
PID_MARKET := /tmp/.$(PROJECT_NAME).$(MARKET_SERVICE).pid
PID_MARKET_API := /tmp/.$(PROJECT_NAME).$(MARKET_API).pid
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent
all: help
## install: Install missing dependencies. Runs `go get` internally. e.g; make install get=github.com/foo/bar
install: go-get
## start: Start market API server, Observer, and swagger server in development mode.
start:
@bash -c "$(MAKE) clean compile start-market-observer start-market-api"
## start-market-observer: Start market observer in development mode.
start-market-observer: stop
@echo " > Starting $(PROJECT_NAME) Sync"
@-$(GOBIN)/$(MARKET_SERVICE)/worker -c $(CONFIG_FILE) 2>&1 & echo $$! > $(PID_MARKET)
@cat $(PID_MARKET) | sed "/^/s/^/ \> Sync PID: /"
@echo " > Error log: $(STDERR)"
## start-market-api: Start market api in development mode.
start-market-api: stop
@echo " > Starting $(PROJECT_NAME) Sync API"
@-$(GOBIN)/$(MARKET_API)/api -c $(CONFIG_FILE) 2>&1 & echo $$! > $(PID_MARKET_API)
@cat $(PID_MARKET_API) | sed "/^/s/^/ \> Sync PID: /"
@echo " > Error log: $(STDERR)"
## stop: Stop development mode.
stop:
@-touch $(PID_MARKET) $(PID_MARKET_API)
@-kill `cat $(PID_MARKET)` 2> /dev/null || true
@-kill `cat $(PID_MARKET_API)` 2> /dev/null || true
@-rm $(PID_MARKET) $(PID_MARKET_API)
## compile: Compile the project.
compile:
@-touch $(STDERR)
@-rm $(STDERR)
@-$(MAKE) -s go-compile 2> $(STDERR)
@cat $(STDERR) | sed -e '1s/.*/\nError:\n/' | sed 's/make\[.*/ /' | sed "/^/s/^/ /" 1>&2
## exec: Run given command. e.g; make exec run="go test ./..."
exec:
GOBIN=$(GOBIN) $(run)
## clean: Clean build files. Runs `go clean` internally.
clean:
@-rm $(GOBIN)/$(PROJECT_NAME) 2> /dev/null
@-rm -rf mocks
@-$(MAKE) go-clean
## test: Run all unit tests.
test: go-test
## integration: Run all integration tests.
integration: go-integration
## fmt: Run `go fmt` for all go files.
fmt: go-fmt
## govet: Run go vet.
govet: generate-mocks go-vet
## golint: Run golint.
lint: go-lint-install go-lint
## docs: Generate swagger docs.
docs: go-gen-docs
docker-shutdown:
@echo " > Shutdown docker containers..."
@-bash -c "docker rm -f $(DOCKER_LOCAL_DB_IMAGE_NAME) 2> /dev/null"
@-bash -c "docker rm -f $(DOCKER_REDIS_IMAGE_NAME) 2> /dev/null"
start-docker-services: docker-shutdown
docker run -d -p 5432:5432 --name $(DOCKER_LOCAL_DB_IMAGE_NAME) -e POSTGRES_USER=$(DOCKER_LOCAL_DB_USER) -e POSTGRES_PASSWORD=$(DOCKER_LOCAL_DB_PASS) -e POSTGRES_DB=$(DOCKER_LOCAL_DB) postgres
docker run -d -p 6379:6379 --name $(DOCKER_REDIS_IMAGE_NAME) redis
seed-db:
@echo " > Seeding db"
sleep 1
docker cp seed/. $(DOCKER_LOCAL_DB_IMAGE_NAME):/docker-entrypoint-initdb.d/
@echo " > Seeding watchmarket_public_tickers"
docker exec -it $(DOCKER_LOCAL_DB_IMAGE_NAME) psql -U $(DOCKER_LOCAL_DB_USER) -d $(DOCKER_LOCAL_DB) -f /docker-entrypoint-initdb.d/watchmarket_public_tickers.sql > /dev/null 2>&1
@echo " > Seeding watchmarket_public_rates"
docker exec -it $(DOCKER_LOCAL_DB_IMAGE_NAME) psql -U $(DOCKER_LOCAL_DB_USER) -d $(DOCKER_LOCAL_DB) -f /docker-entrypoint-initdb.d/watchmarket_public_rates.sql > /dev/null 2>&1
## install-newman: Install Postman Newman for tests.
install-newman:
ifeq (,$(shell which newman))
@echo " > Installing Postman Newman"
@-npm install -g newman
endif
## newman: Run Postman Newman test, the host parameter is required, and you can specify the name of the test do you wanna run (transaction, token, staking, collection, domain, healthcheck, observer). e.g $ make newman test=staking host=http//localhost
newman: install-newman
@echo " > Running $(test) tests"
ifeq (,$(host))
@echo " > Host parameter is missing. e.g: make newman test=staking host=http://localhost:8420"
@exit 1
endif
ifeq (,$(test))
@bash -c "$(MAKE) newman test=healthcheck host=$(host)"
@bash -c "$(MAKE) newman test=market host=$(host)"
else
@newman run tests/postman/watchmarket.postman_collection.json --folder $(test) -d tests/postman/$(test)_data.json --env-var "host=$(host)"
endif
go-compile: go-get go-build
go-build:
@echo " > Building worker binary..."
GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(MARKET_SERVICE)/worker ./cmd/$(MARKET_SERVICE)
@echo " > Building api binary..."
GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(MARKET_API)/api ./cmd/$(MARKET_API)
go-generate:
@echo " > Generating dependency files..."
GOBIN=$(GOBIN) go generate $(generate)
go-get:
@echo " > Checking if there are any missing dependencies..."
GOBIN=$(GOBIN) go get cmd/... $(get)
go-install:
GOBIN=$(GOBIN) go install $(GOPKG)
go-clean:
@echo " > Cleaning build cache"
GOBIN=$(GOBIN) go clean
go-test:
@echo " > Running unit tests"
GOBIN=$(GOBIN) go test -coverprofile=coverage.txt -cover -race -v ./...
go-integration:
@echo " > Running integration tests"
GOBIN=$(GOBIN) TEST_CONFIG=$(CONFIG_FILE) go test -race -tags=integration -v ./tests/integration/...
go-fmt:
@echo " > Format all go files"
GOBIN=$(GOBIN) gofmt -w ${GOFMT_FILES}
install-swag:
ifeq (,$(wildcard test -f $(GOPATH)/bin/swag))
@echo " > Installing swagger"
@-bash -c "go get github.com/swaggo/swag/cmd/swag"
endif
swag: install-swag
@bash -c "$(GOPATH)/bin/swag init --parseDependency -g ./cmd/api/main.go -o ./docs"
go-vet:
@echo " > Running go vet"
GOBIN=$(GOBIN) go vet ./...
go-lint-install:
ifeq (,$(wildcard test -f bin/golangci-lint))
@echo " > Installing golint"
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s
endif
go-lint:
@echo " > Running golint"
bin/golangci-lint run --timeout=2m
.PHONY: help
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECT_NAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo