1
- # # showonce Makefile:
2
- # # show secret once
1
+ # # template Makefile:
2
+ # # service example
3
3
# :
4
+
4
5
SHELL = /bin/sh
5
- GO ?= go
6
6
CFG ?= .env
7
+ PRG = showonce
7
8
PRG ?= $(shell basename $$PWD)
9
+ PRG_DEST ?= $(PRG )
8
10
9
- SOURCES = $(shell find . -maxdepth 3 -mindepth 1 -path ./var -prune -o -name '* .go')
11
+ # -----------------------------------------------------------------------------
12
+ # Build config
10
13
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
14
16
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)
18
23
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'
22
27
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
25
32
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 )
31
37
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
34
40
35
41
# Hardcoded in docker-compose.yml service name
36
42
DC_SERVICE ?= app
37
43
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)
42
45
PROJECT_NAME ?= $(PRG )
43
46
44
- # dcape network connect to, must be set in .env
45
- DCAPE_NET ?= dcape_default
47
+ # -----------------------------------------------------------------------------
48
+ # App config
46
49
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
53
52
54
- APP_PROTO=$(APP_PROTO )
53
+ #
54
+ APP_PROTO ?= http
55
55
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
63
66
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)
66
71
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
68
83
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
70
86
71
87
# ------------------------------------------------------------------------------
72
88
# # Compile operations
73
89
# :
74
90
75
- $( PRG ) : $( SOURCES )
76
- $( GO ) build -ldflags " -X main.version= $( VERSION ) " ./cmd/ $( PRG )
91
+ # # Build app
92
+ build : $( PRG_DEST )
77
93
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
78
106
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
81
108
82
109
# # Format go sources
83
110
fmt :
84
111
$(GO ) fmt ./...
85
112
86
- # # Run vet
87
- vet :
88
- $(GO ) vet ./...
89
-
90
- # # Run linter
113
+ # # Run lint
91
114
lint :
92
- golint ./...
115
+ @which golint > /dev/null || go install golang.org/x/lint/golint@latest
116
+ @golint ./...
93
117
94
- # # Run more linters
95
- lint-more :
96
- golangci-lint run ./...
118
+ # # Run golangci-lint
119
+ ci-lint :
120
+ @ golangci-lint run ./...
97
121
98
- # # Run tests
99
- test : coverage.out
122
+ # # Run vet
123
+ vet :
124
+ @$(GO ) vet ./...
100
125
101
- # # Run tests and fill coverage.out
102
- cov : coverage.out
126
+ # # Run tests
127
+ test : lint vet coverage.out
103
128
104
129
# internal target
105
130
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=$@ ./...
107
133
108
134
# # Open coverage report in browser
109
135
cov-html : cov
110
136
$(GO ) tool cover -html=coverage.out
111
137
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
+
112
146
# # Clean coverage report
113
147
cov-clean :
114
148
rm -f coverage.*
@@ -120,34 +154,115 @@ changelog:
120
154
@git log $(RELEASE ) ..@ --pretty=format:" * %s"
121
155
122
156
# ------------------------------------------------------------------------------
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
124
195
# :
125
196
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
126
234
docker : $(PRG )
127
235
docker build -t $(PRG ) .
128
236
129
- # ------------------------------------------------------------------------------
237
+ ALLARCH_DOCKER ?= "linux/arm/v7,linux/arm64"
130
238
239
+ # build multiarch docker images via buildx
240
+ docker-multi :
241
+ time docker buildx build --platform $(ALLARCH_DOCKER ) -t $(DOCKER_IMAGE ) :$(APP_VERSION ) --push .
131
242
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
135
244
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 " ] >> $@
139
248
249
+ use-own-hub : buildkit.toml
250
+ @docker buildx create --use --config $<
140
251
141
- clean : # # Remove previous builds
142
- @rm -f $(PRG )
252
+ # ------------------------------------------------------------------------------
253
+ # # Other
254
+ # :
143
255
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
144
267
145
268
# ------------------------------------------------------------------------------
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