Skip to content

Commit 83ac7ef

Browse files
committed
рефакторинг: переезд на GRPC
1 parent 4c3ee34 commit 83ac7ef

33 files changed

+5040
-647
lines changed

Makefile

Lines changed: 197 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,148 @@
1-
## showonce Makefile:
2-
## show secret once
1+
## template Makefile:
2+
## service example
33
#:
4+
45
SHELL = /bin/sh
5-
GO ?= go
66
CFG ?= .env
7+
PRG = showonce
78
PRG ?= $(shell basename $$PWD)
9+
PRG_DEST ?= $(PRG)
810

9-
SOURCES = $(shell find . -maxdepth 3 -mindepth 1 -path ./var -prune -o -name '*.go')
11+
# -----------------------------------------------------------------------------
12+
# Build config
1013

11-
# do not include docker-compose.yml from dcape
12-
# docker-compose can't build docker image if it included
13-
DCAPE_DC_USED = false
14+
GO ?= go
15+
GOLANG_VERSION = v1.19.7-alpine3.17.2
1416

15-
VERSION ?= $(shell git describe --tags --always)
16-
# Last project tag
17-
RELEASE ?= $(shell git describe --tags --abbrev=0)
17+
SOURCES = $(shell find . -maxdepth 3 -mindepth 1 -path ./var -prune -o -name '*.go')
18+
APP_VERSION ?= $(shell git describe --tags --always)
19+
# Last project tag (used in `make changelog`)
20+
RELEASE ?= $(shell git describe --tags --abbrev=0 --always)
21+
# Repository address (compiled into main.repo)
22+
REPO ?= $(shell git config --get remote.origin.url)
1823

19-
APP_ROOT ?= .
20-
APP_SITE ?= $(PRG).dev.lan
21-
APP_PROTO ?= http
24+
TARGETOS ?= linux
25+
TARGETARCH ?= amd64
26+
LDFLAGS := -s -w -extldflags '-static'
2227

23-
IMAGE ?= $(PRG)
24-
IMAGE_VER ?= latest
28+
OS ?= linux
29+
ARCH ?= amd64
30+
ALLARCH ?= "linux/amd64 linux/386 darwin/amd64 linux/arm linux/arm64"
31+
DIRDIST ?= dist
2532

26-
AS_TYPE ?= gitea
27-
AS_HOST ?= http://gitea:8080
28-
AS_TEAM ?= dcape
29-
AS_CLIENT_ID ?= you_should_get_id_from_as
30-
AS_CLIENT_KEY ?= you_should_get_key_from_as
33+
# Path to golang package docs
34+
GODOC_REPO ?= github.com/!le!kovr/$(PRG)
35+
# App docker image
36+
DOCKER_IMAGE ?= ghcr.io/lekovr/$(PRG)
3137

32-
AS_COOKIE_SIGN_KEY ?= $(shell < /dev/urandom tr -dc A-Za-z0-9 | head -c32; echo)
33-
AS_COOKIE_CRYPT_KEY ?= $(shell < /dev/urandom tr -dc A-Za-z0-9 | head -c32; echo)
38+
# -----------------------------------------------------------------------------
39+
# Docker image config
3440

3541
# Hardcoded in docker-compose.yml service name
3642
DC_SERVICE ?= app
3743

38-
# docker app for change inside containers
39-
DOCKER_BIN ?= docker
40-
41-
# Docker-compose project name (container name prefix)
44+
#- Docker-compose project name (container name prefix)
4245
PROJECT_NAME ?= $(PRG)
4346

44-
# dcape network connect to, must be set in .env
45-
DCAPE_NET ?= dcape_default
47+
# -----------------------------------------------------------------------------
48+
# App config
4649

47-
# ------------------------------------------------------------------------------
48-
# .env template (custom part)
49-
# inserted in .env.sample via 'make config'
50-
define CONFIG_CUSTOM
51-
# ------------------------------------------------------------------------------
52-
# app custom config, generated by make config
50+
#- Docker container port
51+
LISTEN ?= 8080
5352

54-
APP_PROTO=$(APP_PROTO)
53+
#
54+
APP_PROTO ?= http
5555

56-
AS_TYPE=$(AS_TYPE)
57-
AS_HOST=$(AS_HOST)
58-
AS_TEAM=$(AS_TEAM)
59-
AS_CLIENT_ID=$(AS_CLIENT_ID)
60-
AS_CLIENT_KEY=$(AS_CLIENT_KEY)
61-
AS_COOKIE_SIGN_KEY=$(AS_COOKIE_SIGN_KEY)
62-
AS_COOKIE_CRYPT_KEY=$(AS_COOKIE_CRYPT_KEY)
56+
#- Auth service type
57+
AS_TYPE ?= gitea
58+
#- Auth service URL
59+
AS_HOST ?= https://git.vivo.sb
60+
#- Auth service org
61+
AS_TEAM ?= dcape
62+
#- Auth service client_id
63+
AS_CLIENT_ID ?= you_should_get_id_from_as
64+
#- Auth service client key
65+
AS_CLIENT_KEY ?= you_should_get_key_from_as
6366

64-
# dcape network connect to, must be set in .env
65-
DCAPE_NET=$(DCAPE_NET)
67+
#- Auth service cookie sign key
68+
AS_COOKIE_SIGN_KEY ?= $(shell < /dev/urandom tr -dc A-Za-z0-9 | head -c32; echo)
69+
#- Auth service cookie crypt key
70+
AS_COOKIE_CRYPT_KEY ?= $(shell < /dev/urandom tr -dc A-Za-z0-9 | head -c32; echo)
6671

67-
endef
72+
# ------------------------------------------------------------------------------
73+
# Find and include DCAPE/apps/drone/dcape-app/Makefile
74+
DCAPE_COMPOSE ?= dcape-compose
75+
DCAPE_ROOT ?= $(shell docker inspect -f "{{.Config.Labels.dcape_root}}" $(DCAPE_COMPOSE))
76+
77+
ifeq ($(shell test -e $(DCAPE_ROOT)/Makefile.app && echo -n yes),yes)
78+
include $(DCAPE_ROOT)/Makefile.app
79+
endif
80+
81+
-include $(CFG)
82+
export
6883

69-
.PHONY: dep build run lint test clean
84+
.PHONY: build build-standalone run fmt lint ci-lint vet test cov-html cov-func cov-total cov-clean changelog
85+
.PHONY: buildall dist clean docker docker-multi use-own-hub godoc ghcr
7086

7187
# ------------------------------------------------------------------------------
7288
## Compile operations
7389
#:
7490

75-
$(PRG): $(SOURCES)
76-
$(GO) build -ldflags "-X main.version=$(VERSION)" ./cmd/$(PRG)
91+
## Build app
92+
build: $(PRG_DEST)
7793

94+
$(PRG_DEST): $(SOURCES)
95+
GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
96+
$(GO) build -v -o $@ -ldflags \
97+
"${LDFLAGS}-X main.version=$(APP_VERSION) -X main.repo=$(REPO)" \
98+
./cmd/$(PRG)
99+
100+
## Build like docker image from scratch
101+
build-standalone: CGO_ENABLED=0
102+
build-standalone: test
103+
build-standalone: $(PRG_DEST)
104+
105+
## Build & run app
78106
run: $(PRG)
79-
@echo Open http://$(APP_SITE):8080
80-
./$(PRG) --root static --log.debug --as.cookie_name showonce
107+
./$(PRG) --log.debug --root=static
81108

82109
## Format go sources
83110
fmt:
84111
$(GO) fmt ./...
85112

86-
## Run vet
87-
vet:
88-
$(GO) vet ./...
89-
90-
## Run linter
113+
## Run lint
91114
lint:
92-
golint ./...
115+
@which golint > /dev/null || go install golang.org/x/lint/golint@latest
116+
@golint ./...
93117

94-
## Run more linters
95-
lint-more:
96-
golangci-lint run ./...
118+
## Run golangci-lint
119+
ci-lint:
120+
@golangci-lint run ./...
97121

98-
## Run tests
99-
test: coverage.out
122+
## Run vet
123+
vet:
124+
@$(GO) vet ./...
100125

101-
## Run tests and fill coverage.out
102-
cov: coverage.out
126+
## Run tests
127+
test: lint vet coverage.out
103128

104129
# internal target
105130
coverage.out: $(SOURCES)
106-
GIN_MODE=release $(GO) test -test.v -test.race -coverprofile=$@ -covermode=atomic ./...
131+
#GIN_MODE=release $(GO) test -test.v -test.race -coverprofile=$@ -covermode=atomic ./...
132+
GIN_MODE=release $(GO) test -tags test -race -covermode=atomic -coverprofile=$@ ./...
107133

108134
## Open coverage report in browser
109135
cov-html: cov
110136
$(GO) tool cover -html=coverage.out
111137

138+
## Show code coverage per func
139+
cov-func: coverage.out
140+
$(GO) tool cover -func coverage.out
141+
142+
## Show total code coverage
143+
cov-total: coverage.out
144+
@$(GO) tool cover -func coverage.out | grep total: | awk '{print $$3}'
145+
112146
## Clean coverage report
113147
cov-clean:
114148
rm -f coverage.*
@@ -120,34 +154,115 @@ changelog:
120154
@git log $(RELEASE)..@ --pretty=format:"* %s"
121155

122156
# ------------------------------------------------------------------------------
123-
## Docker operations
157+
## GRPC operations
158+
#:
159+
160+
BUF_IMG ?= ghcr.io/apisite/gogens
161+
#BUF_IMG ?= buf
162+
163+
## Generate files
164+
buf-gen:
165+
docker run --rm -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) --debug generate --template buf.gen.yaml --path proto
166+
167+
## Run buf command
168+
buf-cmd:
169+
docker run --rm -it -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) $(CMD)
170+
171+
## Run sh command
172+
buf-sh:
173+
docker run --rm -it --entrypoint /bin/sh -v `pwd`:/mnt/pwd -w /mnt/pwd $(BUF_IMG) $(CMD)
174+
175+
.PHONY: buf.lock
176+
177+
## Fetch buf.lock
178+
buf.lock:
179+
@id=$$(docker create $(BUF_IMG)) ; \
180+
docker cp $$id:/app/$@ $@ ; \
181+
docker rm -v $$id
182+
183+
## Generate JS API
184+
js: static/js/api.js
185+
186+
static/js/api.js: zgen/ts/proto/service.pb.ts
187+
docker run --rm -v `pwd`:/mnt/pwd -w /mnt/pwd --entrypoint /go/bin/esbuild $(BUF_IMG) \
188+
zgen/ts/proto/service.pb.ts --bundle --outfile=/mnt/pwd/static/js/api.js --global-name=AppAPI
189+
190+
grpc-test:
191+
docker run fullstorydev/grpcurl localhost:$(LISTEN) list
192+
193+
# ------------------------------------------------------------------------------
194+
## Prepare distros
124195
#:
125196

197+
## build app for all platforms
198+
buildall: lint vet
199+
@echo "*** $@ ***" ; \
200+
for a in "$(ALLARCH)" ; do \
201+
echo "** $${a%/*} $${a#*/}" ; \
202+
P=$(PRG)-$${a%/*}_$${a#*/} ; \
203+
$(MAKE) -s build-standalone TARGETOS=$${a%/*} TARGETARCH=$${a#*/} PRG_DEST=$$P ; \
204+
done
205+
206+
## create disro files
207+
dist: clean buildall
208+
@echo "*** $@ ***"
209+
@[ -d $(DIRDIST) ] || mkdir $(DIRDIST)
210+
@sha256sum $(PRG)-* > $(DIRDIST)/SHA256SUMS ; \
211+
for a in "$(ALLARCH)" ; do \
212+
echo "** $${a%/*} $${a#*/}" ; \
213+
P=$(PRG)-$${a%/*}_$${a#*/} ; \
214+
zip "$(DIRDIST)/$$P.zip" "$$P" README.md README.ru.md screenshot.png; \
215+
rm "$$P" ; \
216+
done
217+
218+
## clean generated files
219+
clean:
220+
@echo "*** $@ ***" ; \
221+
for a in "$(ALLARCH)" ; do \
222+
P=$(PRG)_$${a%/*}_$${a#*/} ; \
223+
[ -f $$P ] && rm $$P || true ; \
224+
done
225+
@[ -d $(DIRDIST) ] && rm -rf $(DIRDIST) || true
226+
@[ -f $(PRG) ] && rm -f $(PRG) || true
227+
@[ ! -f coverage.out ] || rm coverage.out
228+
229+
# ------------------------------------------------------------------------------
230+
## Docker build operations
231+
#:
232+
233+
# build docker image directly
126234
docker: $(PRG)
127235
docker build -t $(PRG) .
128236

129-
# ------------------------------------------------------------------------------
237+
ALLARCH_DOCKER ?= "linux/arm/v7,linux/arm64"
130238

239+
# build multiarch docker images via buildx
240+
docker-multi:
241+
time docker buildx build --platform $(ALLARCH_DOCKER) -t $(DOCKER_IMAGE):$(APP_VERSION) --push .
131242

132-
## Build docker image
133-
docker-build: CMD="build --no-cache --force-rm $(DC_SERVICE)"
134-
docker-build: dc
243+
OWN_HUB ?= it.elfire.ru
135244

136-
## Remove docker image & temp files
137-
docker-clean:
138-
[ "$$($(DOCKER_BIN) images -q $(DC_IMAGE) 2> /dev/null)" = "" ] || $(DOCKER_BIN) rmi $(DC_IMAGE)
245+
buildkit.toml:
246+
@echo [registry."$(OWN_HUB)"] > $@
247+
@echo ca=["/etc/docker/certs.d/$(OWN_HUB)/ca.crt"] >> $@
139248

249+
use-own-hub: buildkit.toml
250+
@docker buildx create --use --config $<
140251

141-
clean: ## Remove previous builds
142-
@rm -f $(PRG)
252+
# ------------------------------------------------------------------------------
253+
## Other
254+
#:
143255

256+
## update docs at pkg.go.dev
257+
godoc:
258+
vf=$(APP_VERSION) ; v=$${vf%%-*} ; echo "Update for $$v..." ; \
259+
curl 'https://proxy.golang.org/$(GODOC_REPO)/@v/'$$v'.info'
260+
261+
## update latest docker image tag at ghcr.io
262+
ghcr:
263+
v=$(APP_VERSION) ; echo "Update for $$v..." ; \
264+
docker pull $(DOCKER_IMAGE):$$v && \
265+
docker tag $(DOCKER_IMAGE):$$v $(DOCKER_IMAGE):latest && \
266+
docker push $(DOCKER_IMAGE):latest
144267

145268
# ------------------------------------------------------------------------------
146-
# Find and include DCAPE/apps/drone/dcape-app/Makefile
147-
DCAPE_COMPOSE ?= dcape-compose
148-
DCAPE_MAKEFILE ?= $(shell docker inspect -f "{{.Config.Labels.dcape_app_makefile}}" $(DCAPE_COMPOSE))
149-
ifeq ($(shell test -e $(DCAPE_MAKEFILE) && echo -n yes),yes)
150-
include $(DCAPE_MAKEFILE)
151-
else
152-
include /opt/dcape-app/Makefile
153-
endif

0 commit comments

Comments
 (0)