You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
24
7
25
8
26
9
.PHONY: mod
@@ -60,12 +43,6 @@ cover:
60
43
go tool cover -html=cover.out
61
44
62
45
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
-
69
46
.PHONY: graph
70
47
# generate interactive visual function dependency graphs
71
48
graph:
@@ -75,10 +52,16 @@ graph:
75
52
@rm -f main.go community.gv
76
53
77
54
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
+
78
61
.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
80
63
proto: mod fmt
81
-
@bash scripts/protoc.sh
64
+
@bash scripts/protoc.sh$(FILES)
82
65
83
66
84
67
.PHONY: proto-doc
@@ -91,9 +74,7 @@ proto-doc:
91
74
# build community for linux amd64 binary
92
75
build:
93
76
@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
97
78
98
79
99
80
@@ -109,12 +90,30 @@ run-nohup:
109
90
@bash scripts/run-nohup.sh $(CMD)
110
91
111
92
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
# push docker image to remote repositories, e.g. make image-push REPO_HOST=addr TAG=latest
138
131
image-push:
@@ -145,22 +138,28 @@ deploy-k8s:
145
138
@bash scripts/deploy-k8s.sh
146
139
147
140
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
Copy file name to clipboardExpand all lines: 7_community-single/README.md
+10-7
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,10 @@
2
2
3
3
<br>
4
4
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
+
5
9
### Community-single Introduction
6
10
7
11
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
20
24
21
25
### Generate project code
22
26
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:
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.
28
32
29
-
<br>
30
33
<br>
31
34
32
35
### Writing Business Logic Code
@@ -65,7 +68,7 @@ The code generated by the `make proto` command is used to connect web framework
65
68
66
69
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.
67
70
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:
@@ -75,16 +78,16 @@ There are three parts to specifying dao codes generated by MySQL tables.
75
78
76
79
(1) **Generate model code**, in the `internal/model` directory, this is generated go structure code corresponding to gorm.
77
80
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.
79
82
80
83
(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.
81
84
82
85
<br>
83
86
84
87
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:
85
88
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.
88
91
89
92
<br>
90
93
<br>
@@ -343,4 +346,4 @@ This is a practical project using sponge from development to deployment. The spe
343
346
344
347
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.
345
348
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.
0 commit comments