Skip to content

Commit ef4ae10

Browse files
committed
update community-single
1 parent da39bcc commit ef4ae10

File tree

98 files changed

+3481
-1281
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+3481
-1281
lines changed

7_community-single/.golangci.yml

+54-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ run:
1212
# default value is empty list, but default dirs are skipped independently
1313
# from this option's value (see skip-dirs-use-default).
1414
skip-dirs:
15-
- pkg
1615
- docs
1716
- api
1817
# which files to skip: they will be analyzed, but issues from them
@@ -39,7 +38,7 @@ linters:
3938
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
4039
disable-all: true
4140
enable:
42-
- golint
41+
- revive
4342
- goimports
4443
- gofmt
4544
- unused
@@ -66,6 +65,59 @@ linters:
6665

6766

6867
linters-settings:
68+
revive:
69+
rules:
70+
- name: argument-limit
71+
arguments: [ 8 ]
72+
- name: atomic
73+
- name: bare-return
74+
- name: blank-imports
75+
- name: bool-literal-in-expr
76+
- name: call-to-gc
77+
- name: confusing-naming
78+
- name: confusing-results
79+
- name: constant-logical-expr
80+
- name: context-as-argument
81+
- name: context-keys-type
82+
- name: deep-exit
83+
- name: defer
84+
- name: dot-imports
85+
- name: duplicated-imports
86+
- name: early-return
87+
- name: empty-block
88+
#- name: empty-lines
89+
- name: error-naming
90+
- name: error-return
91+
- name: error-strings
92+
- name: errorf
93+
- name: function-result-limit
94+
arguments: [ 3 ]
95+
- name: identical-branches
96+
- name: if-return
97+
- name: import-shadowing
98+
- name: increment-decrement
99+
- name: indent-error-flow
100+
- name: modifies-parameter
101+
- name: modifies-value-receiver
102+
- name: package-comments
103+
- name: range
104+
- name: range-val-address
105+
- name: range-val-in-closure
106+
- name: receiver-naming
107+
- name: redefines-builtin-id
108+
- name: string-of-int
109+
- name: struct-tag
110+
- name: superfluous-else
111+
- name: time-naming
112+
- name: unconditional-recursion
113+
- name: unexported-naming
114+
- name: unnecessary-stmt
115+
- name: unreachable-code
116+
- name: unused-parameter
117+
- name: var-declaration
118+
- name: var-naming
119+
- name: waitgroup-by-value
120+
69121
dogsled:
70122
# checks assignments with too many blank identifiers; default is 2
71123
max-blank-identifiers: 2
@@ -126,10 +178,6 @@ linters-settings:
126178
# it's a comma-separated list of prefixes
127179
local-prefixes: community
128180

129-
golint:
130-
# minimal confidence for issues, default is 0.8
131-
min-confidence: 0.8
132-
133181
gomnd:
134182
settings:
135183
mnd:

7_community-single/Makefile

+53-54
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,7 @@ SHELL := /bin/bash
33
PROJECT_NAME := "community"
44
PKG := "$(PROJECT_NAME)"
55
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/ | grep -v /api/)
6-
#GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
7-
8-
9-
.PHONY: install
10-
# installation of dependent tools
11-
install:
12-
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
13-
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
14-
go install github.com/envoyproxy/protoc-gen-validate@latest
15-
go install github.com/srikrsna/protoc-gen-gotag@latest
16-
go install community/cmd/protoc-gen-go-gin@latest
17-
go install community/cmd/protoc-gen-go-rpc-tmpl@latest
18-
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
19-
go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@latest
20-
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
21-
go install github.com/swaggo/swag/cmd/swag@latest
22-
go install github.com/ofabry/go-callvis@latest
23-
go install golang.org/x/pkgsite/cmd/pkgsite@latest
6+
247

258

269
.PHONY: mod
@@ -60,12 +43,6 @@ cover:
6043
go tool cover -html=cover.out
6144

6245

63-
.PHONY: docs
64-
# generate swagger docs, the host address can be changed via parameters, e.g. make docs HOST=192.168.3.37
65-
docs: mod fmt
66-
@bash scripts/swag-docs.sh $(HOST)
67-
68-
6946
.PHONY: graph
7047
# generate interactive visual function dependency graphs
7148
graph:
@@ -75,10 +52,16 @@ graph:
7552
@rm -f main.go community.gv
7653

7754

55+
.PHONY: docs
56+
# generate swagger docs, only for ⓵ Web services created based on sql, the host address can be changed via parameters, e.g. make docs HOST=192.168.3.37
57+
docs: mod fmt
58+
@bash scripts/swag-docs.sh $(HOST)
59+
60+
7861
.PHONY: proto
79-
# generate *.pb.go codes from *.proto files
62+
# generate *.go and template code by proto files, if you do not refer to the proto file, the default is all the proto files in the api directory. you can specify the proto file, multiple files are separated by commas, e.g. make proto FILES=api/user/v1/user.proto. only for ⓶ Microservices created based on sql, ⓷ Web services created based on protobuf, ⓸ Microservices created based on protobuf, ⓹ RPC gateway service created based on protobuf
8063
proto: mod fmt
81-
@bash scripts/protoc.sh
64+
@bash scripts/protoc.sh $(FILES)
8265

8366

8467
.PHONY: proto-doc
@@ -91,9 +74,7 @@ proto-doc:
9174
# build community for linux amd64 binary
9275
build:
9376
@echo "building 'community', linux binary file will output to 'cmd/community'"
94-
@cd cmd/community && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct go build
95-
96-
77+
@cd cmd/community && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
9778

9879

9980

@@ -109,12 +90,30 @@ run-nohup:
10990
@bash scripts/run-nohup.sh $(CMD)
11091

11192

112-
.PHONY: docker-image
93+
.PHONY: binary-package
94+
# packaged binary files
95+
binary-package: build
96+
@bash scripts/binary-package.sh
97+
98+
99+
.PHONY: deploy-binary
100+
# deploy binary to remote linux server, e.g. make deploy-binary USER=root PWD=123456 IP=192.168.1.10
101+
deploy-binary: binary-package
102+
@expect scripts/deploy-binary.sh $(USER) $(PWD) $(IP)
103+
104+
105+
.PHONY: image-build-local
113106
# build image for local docker, tag=latest, use binary files to build
114-
docker-image: build
107+
image-build-local: build
115108
@bash scripts/image-build-local.sh
116109

117110

111+
.PHONY: deploy-docker
112+
# deploy service to local docker, if you want to update the service, run the make deploy-docker command again.
113+
deploy-docker: image-build-local
114+
@bash scripts/deploy-docker.sh
115+
116+
118117
.PHONY: image-build
119118
# build image for remote repositories, use binary files to build, e.g. make image-build REPO_HOST=addr TAG=latest
120119
image-build:
@@ -127,12 +126,6 @@ image-build2:
127126
@bash scripts/image-build2.sh $(REPO_HOST) $(TAG)
128127

129128

130-
.PHONY: image-build-rpc-test
131-
# build rpc test image for remote repositories, e.g. make image-build-rpc-test REPO_HOST=addr TAG=latest
132-
image-build-rpc-test:
133-
@bash scripts/image-rpc-test.sh $(REPO_HOST) $(TAG)
134-
135-
136129
.PHONY: image-push
137130
# push docker image to remote repositories, e.g. make image-push REPO_HOST=addr TAG=latest
138131
image-push:
@@ -145,22 +138,28 @@ deploy-k8s:
145138
@bash scripts/deploy-k8s.sh
146139

147140

148-
.PHONY: deploy-docker
149-
# deploy service to local docker, you must first run 'make docker-image' to generate a docker image, if you want to stop the server, pass the parameter stop, e.g. make deploy-docker CMD=stop
150-
deploy-docker:
151-
@bash scripts/deploy-docker.sh $(CMD)
141+
.PHONY: image-build-rpc-test
142+
# build rpc test image for remote repositories, e.g. make image-build-rpc-test REPO_HOST=addr TAG=latest
143+
image-build-rpc-test:
144+
@bash scripts/image-rpc-test.sh $(REPO_HOST) $(TAG)
152145

153146

154-
.PHONY: binary-package
155-
# packaged binary files
156-
binary-package: build
157-
@bash scripts/binary-package.sh
147+
.PHONY: patch
148+
# patch some dependent code, such as types.proto, mysql initialization code. e.g. make patch TYPE=types-pb , make patch TYPE=mysql-init
149+
patch:
150+
@bash scripts/patch.sh $(TYPE)
158151

159152

160-
.PHONY: deploy-binary
161-
# deploy binary, e.g. make deploy-binary USER=root PWD=123456 IP=192.168.1.10
162-
deploy-binary: binary-package
163-
@expect scripts/deploy-binary.sh $(USER) $(PWD) $(IP)
153+
.PHONY: copy-proto
154+
# copy proto file from the rpc server directory, multiple directories separated by commas. e.g. make copy-proto SERVER=yourServerDir
155+
copy-proto:
156+
@sponge patch copy-proto --server-dir=$(SERVER)
157+
158+
159+
.PHONY: update-config
160+
# update internal/config code base on yaml file
161+
update-config:
162+
@sponge config --server-dir=.
164163

165164

166165
.PHONY: clean
@@ -169,10 +168,10 @@ clean:
169168
@rm -vrf cmd/community/community
170169
@rm -vrf cover.out
171170
@rm -vrf main.go community.gv
172-
@rm -vrf internal/ecode/*go.gen.*
173-
@rm -vrf internal/routers/*go.gen.*
174-
@rm -vrf internal/handler/*go.gen.*
175-
@rm -vrf internal/service/*go.gen.*
171+
@rm -vrf internal/ecode/*.go.gen*
172+
@rm -vrf internal/routers/*.go.gen*
173+
@rm -vrf internal/handler/*.go.gen*
174+
@rm -vrf internal/service/*.go.gen*
176175
@rm -rf community-binary.tar.gz
177176
@echo "clean finished"
178177

@@ -184,7 +183,7 @@ help:
184183
@echo ' make [target]'
185184
@echo ''
186185
@echo 'Targets:'
187-
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
186+
@awk '/^[a-zA-Z\-_0-9]+:/ { \
188187
helpMessage = match(lastLine, /^# (.*)/); \
189188
if (helpMessage) { \
190189
helpCommand = substr($$1, 0, index($$1, ":")-1); \

7_community-single/README.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
<br>
44

5+
**Import [sql](https://github.com/zhufuyi/sponge_examples/blob/main/7_community-single/test/sql/community.sql) into mysql before you start.**
6+
7+
<br>
8+
59
### Community-single Introduction
610

711
Community-single is a minimalist version of a community backend service that mainly includes user registration, login, follow-up and other functions, as well as the creation of content (text, images, videos), publishing, commenting, liking, and collecting. These functions are common in various community platforms, video platforms, live broadcast platforms and can be used as a reference for learning.
@@ -20,13 +24,12 @@ The development process relies on the sponge tool and requires sponge to be inst
2024

2125
### Generate project code
2226

23-
After defining the data tables and API interfaces, generate web service project code based on the proto file in sponge's interface. Enter sponge's UI interface, click on the left menu bar 【protobuf】--> 【Web type】-->【Create web project】, fill in relevant parameters to generate web project code as shown below:
27+
After defining the data tables and API interfaces, generate web service project code based on the proto file in sponge's interface. Enter sponge's UI interface, click on the left menu bar 【Protobuf】→【Create web project】, fill in relevant parameters to generate web project code as shown below:
2428

2529
![community-single-web](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_community-single-web.png)
2630

2731
Unzip the code and change the folder name (e.g., community-single). A service only needs to generate code once. In this way, a basic framework for building a web service is completed with one click. Next, you can write business logic code within the web service framework.
2832

29-
<br>
3033
<br>
3134

3235
### Writing Business Logic Code
@@ -65,7 +68,7 @@ The code generated by the `make proto` command is used to connect web framework
6568

6669
The previously generated web service framework code and part of the business logic code generated according to the proto file do not include operations on MySQL tables. Therefore, it is necessary to generate dao (data access object) code according to MySQL tables. Dao code includes **CRUD** code for tables, cache code, and model code.
6770

68-
Enter the sponge UI interface, click on 【Public】--> 【Generate dao CRUD code】 in the left menu bar, fill in relevant parameters to generate dao code, as shown below:
71+
Enter the sponge UI interface, click on 【Public】 【Generate dao CRUD code】 in the left menu bar, fill in relevant parameters to generate dao code, as shown below:
6972

7073
![community-single-dao](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_community-single-dao.png)
7174

@@ -75,16 +78,16 @@ There are three parts to specifying dao codes generated by MySQL tables.
7578

7679
(1) **Generate model code**, in the `internal/model` directory, this is generated go structure code corresponding to gorm.
7780

78-
(2) **Generate cache code**, in files under the `internal/cache` directory, during the process of writing business logic codes, caches may be used to improve performance. Sometimes default caches (CRUD) for tables cannot meet requirements and additional cache codes need to be added. Sponge supports one-click generation of cache codes. Click on 【Public】--> 【Generate cache codes】 in the left menu bar, fill in parameters to generate codes, then move the unzipped internal directory to community-single directory and directly call cache interfaces in business logic.
81+
(2) **Generate cache code**, in files under the `internal/cache` directory, during the process of writing business logic codes, caches may be used to improve performance. Sometimes default caches (CRUD) for tables cannot meet requirements and additional cache codes need to be added. Sponge supports one-click generation of cache codes. Click on 【Public】 【Generate cache codes】 in the left menu bar, fill in parameters to generate codes, then move the unzipped internal directory to community-single directory and directly call cache interfaces in business logic.
7982

8083
(3) **Generate dao codes**, in files under `internal/dao` directory, during writing business logic codes, it will involve operating on MySQL tables. Sometimes default operations (CRUD) cannot meet requirements. At this time, custom functions and implementation codes for operating on MySQL tables need to be manually written. For example comment.go and post.go contain a small number of manually defined function methods for operating on msyql tables.
8184

8285
<br>
8386

8487
During development process sometimes MySQL tables will be modified or added. Codes generated based on MySQL tables need to be synchronized into project codes and handled in two situations:
8588

86-
- After modifying MySQL table update codes: Just generate new model codes based on modified table and replace old model codes. Click on 【Public】--> 【Generate model codes】 in left menu bar, fill in parameters, select changed mysql table then move unzipped internal directory to community-single directory and confirm replacement.
87-
- After adding new mysql table: Just generate new dao codes based on added table and add them into project directory. Click on 【Public】--> 【Generate dao codes】 in left menu bar, fill in parameters, select added mysql table then move unzipped internal directory into community-single directory.
89+
- After modifying MySQL table update codes: Just generate new model codes based on modified table and replace old model codes. Click on 【Public】 【Generate model codes】 in left menu bar, fill in parameters, select changed mysql table then move unzipped internal directory to community-single directory and confirm replacement.
90+
- After adding new mysql table: Just generate new dao codes based on added table and add them into project directory. Click on 【Public】 【Generate dao codes】 in left menu bar, fill in parameters, select added mysql table then move unzipped internal directory into community-single directory.
8891

8992
<br>
9093
<br>
@@ -343,4 +346,4 @@ This is a practical project using sponge from development to deployment. The spe
343346

344347
It seems that there are many processes, but only 1, 2, 6 are three core business processes that require manual coding. The code or scripts involved in other processes are generated by sponge. Using sponge to separate non-business logic code from business logic code allows developers to focus on core business logic code when developing projects while also making project code standardized and uniform so that different programmers can quickly get started. Combined with programming assistance tools such as Copilot or Codeium to write code, project development becomes more efficient and easy.
345348

346-
community-single is a monolithic web service. As the demand increases and the functionality becomes more and more complex, code maintenance and development become difficult. It can be split into multiple microservices. The process of splitting a web monolithic service into microservices only changes the eggshell (web framework changed to gRPC framework) and albumen (http handler-related code changed to rpc service-related code), while the yolk (core business logic code) remains unchanged. The core business logic code can be seamlessly ported to microservice code.
349+
community-single is a monolithic web service. As the demand increases and the functionality becomes more and more complex, code maintenance and development become difficult. It can be split into multiple microservices. The process of splitting a web monolithic service into microservices only changes the eggshell (web framework changed to grpc framework) and albumen (http handler-related code changed to grpc service-related code), while the yolk (core business logic code) remains unchanged. The core business logic code can be seamlessly ported to microservice code.

7_community-single/api/community/v1/collect_router.pb.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)