Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Feb 16, 2024
1 parent 95c77b0 commit 0ca5eaf
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .github/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
- Added generate k8s configmap script command.
- The generated code version matches the local sponge version.
- Fixed redundant createdAt and updatedAt fields in service and handler.
- Add encryption and decryption library.
- Add scheduled task library.
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,64 @@

<br>

If you are developing a RESTful web service or microservice with a simple CRUD API interface, you don't need to write a single line of golang code to compile and deploy to servers, dockers, k8s, and the complete service code is generated by sponge.
If you are developing a RESTful web service or microservice with a simple CRUD API interface, you don't need to write a single line of Go code to compile and deploy to servers, dockers, k8s, and the complete service code is generated by sponge.

If you develop a generic RESTful web service or microservice, you need to manually write code in addition to defining the data table, defining the api interface in the proto file, and filling in the specific business logic code in the generated template file. Other golang codes are generated by sponge.
If you develop a generic RESTful web service or microservice, you need to manually write code in addition to defining the data table, defining the api interface in the proto file, and filling in the specific business logic code in the generated template file. Other Go codes are generated by sponge.

<br>

### sponge generates the code framework
### Sponge Generates the Code Framework

sponge is mainly based on **SQL** and **Protobuf** two ways to generate code, each way has to generate code for different functions.
sponge is mainly based on `SQL` and `Protobuf` two ways to generate code, each way has to generate code for different functions. `SQL` supports databases **mysql**, **postgresql**, **tidb**, **sqlite**.

**Generate code framework:**
#### Generate Code Framework

<p align="center">
<img width="1500px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/sponge-framework.png">
</p>

<br>

**Generate code framework corresponding UI interface:**
#### Generate Code Framework Corresponding UI Interface

<p align="center">
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/en_sponge-ui.png">
</p>

<br>

### Services framework
#### Generate Code for Egg Model

The sponge code generation process strips away the business logic and non-business logic of the two major parts of the code, sponge generated a complete web service back-end code as a complete egg:

- `Eggshell` represents the web service framework code, `yolk` and `Albumen` both represent the business logic code.
- `Yolk` is the core of business logic. For example, defining mysql tables, defining api interfaces, and writing specific logic codes all belong to yolk (code that needs to be written manually).
- `Albumen` is a bridge connecting the core code of business logic and the web framework code. For example, the registration route code, handler code, parameter verification code, error code, and swagger document generated according to the proto file belong to the albumen (code that automatically generated without manual writing).

Sponge is essentially a microservice framework that includes code generation and deployment capabilities. The microservice framework is shown in the following figure, which is a typical microservice hierarchical structure, with high performance, high scalability, contains commonly used service governance features, you can easily replace or add their own service governance features.
Here is egg model profiling diagram for `⓷Create web service based on protobuf`:

<p align="center">
<img width="1000px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/microservices-framework.png">
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_web-http-pb-anatomy.png">
</p>

<br>
In addition to the egg model of web service backend code, there are egg models of grpc service code and grpc gateway service code described in [sponge documentation](https://go-sponge.com/learn-about-sponge?id=%f0%9f%8f%b7project-code-egg-model).

### Egg model of a web service code generated by sponge
<br>

The sponge separates the two major parts of code during the process of generating web service code. It isolates the business logic from the non-business logic. For example, consider the entire web service code as an egg. The eggshell represents the web service framework code, while both the albumen and yolk represent the business logic code. The yolk is the core of the business logic (manually written code). It includes defining MySQL tables, defining API interfaces, and writing specific logic code.On the other hand, the albumen acts as a bridge connecting the core business logic code to the web framework code (automatically generated, no manual writing needed). This includes the registration of route code generated from proto files, handler method function code, parameter validation code, error codes, Swagger documentation, and more.
### Services framework

Egg model profiling diagram for `⓷Web services created based on protobuf`:
Sponge is essentially a microservice framework that includes code generation capabilities. The microservice framework is shown in the following figure, which is a typical microservice hierarchical structure, with high performance, high scalability, contains commonly used service governance features, you can easily replace or add their own service governance features.

<p align="center">
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_web-http-pb-anatomy.png">
<img width="1000px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/microservices-framework.png">
</p>

This is the egg model for web service code, and there are egg models for grpc service code, and grpc gateway service code described in [sponge documentation](https://go-sponge.com/learn-about-sponge?id=%f0%9f%8f%b7project-code-egg-model).

<br>

### Key Features

- User-friendly UI for code generation commands.
- Automatic merging of template code for "low-code development" of API interfaces.
- Not only generate code template examples, but also automatically merge the new template code to achieve api interface "low-code development".
- Modular and decoupled code design with rich functional components readily available, you can also easily use their own components.
- Web framework [gin](https://github.com/gin-gonic/gin)
- RPC framework [grpc](https://github.com/grpc/grpc-go)
Expand Down
Binary file modified assets/en_sponge-ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 20 additions & 18 deletions assets/readme-cn.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@



[sponge](https://github.com/zhufuyi/sponge) 是一个集成了 `自动生成代码``Gin和GRPC` 的基础开发框架。sponge拥有丰富的生成代码命令,生成不同的功能代码可以组合成完整的服务(类似人为打散的海绵细胞可以自动重组成一个新的海绵)。代码解耦模块化设计,很容易构建出从开发到部署的完整工程项目,只需在生成的模板代码上填充具体业务逻辑代码,极大的提高了开发效率和降低了开发难度,使用go也可以"低代码开发"。

<br>

如果开发只有简单CRUD api接口的web或微服务,不需要编写一行golang代码就可以编译并部署到服务器、docker、k8s上,完整的服务代码由sponge一键生成。
如果开发只有简单CRUD api接口的web或微服务,不需要编写一行go代码就可以编译并部署到服务器、docker、k8s上,完整的服务代码由sponge一键生成。

如果开发通用的web或微服务,除了定义数据表、在proto文件定义api接口、在生成的模板文件填写具体业务逻辑代码这三个部分需要人工编写代码,其他golang代码都由sponge生成
如果开发通用的web或微服务,除了`定义数据库表``在proto文件定义api接口``在生成的模板文件填写业务逻辑代码`这三个部分需要人工编写,其他go代码都由sponge生成

<br>

### 生成代码框架

sponge主要基于**SQL****Protobuf**两种方式生成代码,每种方式拥有生成不同功能代码
sponge主要基于`SQL``Protobuf`两种方式生成代码,每种方式拥有生成不同用途的代码。其中`SQL`支持数据库**mysql****postgresql****tidb****sqlite**

**生成代码的框架图**
#### 生成代码的框架图

<p align="center">
<img width="1500px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/sponge-framework.png">
</p>

<br>

**生成代码框架对应的UI界面**
#### 生成代码框架对应的UI界面

<p align="center">
<img width="1500px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/sponge-ui.png">
</p>

<br>

### 微服务框架
#### 生成代码的鸡蛋模型

sponge生成代码过程中剥离了业务逻辑与非业务逻辑两大部分代码,把sponge生成的一个完整web服务后端代码看作是一个完整的鸡蛋:

sponge本质是一个包含了生成代码和部署功能的微服务框架,微服务框架如下图所示,这是典型的微服务分层结构,具有高性能,高扩展性,包含了常用的服务治理功能,可以很方便替换或添加自己的服务治理功能。
- `蛋壳`表示web服务框架代码,`蛋白``蛋黄`都表示业务逻辑代码。
- `蛋黄`是业务逻辑的核心,例如定义mysql表、定义api接口、编写具体逻辑代码都属于蛋黄部分(需要人工编写的代码)。
- `蛋白`是业务逻辑核心代码与web框架代码连接的桥梁,例如根据proto文件生成的注册路由代码、handler代码、dao代码、参数校验代码、错误码、swagger文档等都属于蛋白部分(自动生成,不需要人工编写)。

下面是`⓷基于protobuf创建的web服务`代码的鸡蛋模型剖析图:

<p align="center">
<img width="1000px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/microservices-framework.png">
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/web-http-pb-anatomy.png">
</p>

<br>
除了web服务后端代码鸡蛋模型,还有grpc服务代码、grpc网关服务代码的鸡蛋模型,在[sponge文档](https://go-sponge.com/zh-cn/learn-about-sponge?id=%f0%9f%8f%b7%e9%a1%b9%e7%9b%ae%e4%bb%a3%e7%a0%81%e9%b8%a1%e8%9b%8b%e6%a8%a1%e5%9e%8b)中有介绍。

### sponge生成的一个web服务代码的鸡蛋模型
<br>

sponge生成代码过程中剥离了业务逻辑与非业务逻辑两大部分代码。例如把一个完整web服务代码看作一个鸡蛋,蛋壳表示web服务框架代码,蛋白和蛋黄都表示业务逻辑代码,蛋黄是业务逻辑的核心(需要人工编写的代码),例如定义mysql表、定义api接口、编写具体逻辑代码都属于蛋黄部分。蛋白是业务逻辑核心代码与web框架代码连接的桥梁(自动生成,不需要人工编写),例如根据proto文件生成的注册路由代码、handler方法函数代码、参数校验代码、错误码、swagger文档等都属于蛋白部分。
### 微服务框架

`⓷基于protobuf创建的web服务`代码的鸡蛋模型剖析图:
sponge本质是一个包含了生成代码功能的微服务框架,微服务框架如下图所示,这是典型的微服务分层结构,具有高性能,高扩展性,包含了常用的服务治理功能,可以很方便替换或添加自己的服务治理功能。

<p align="center">
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/web-http-pb-anatomy.png">
<img width="1000px" src="https://raw.githubusercontent.com/zhufuyi/sponge/main/assets/microservices-framework.png">
</p>

这是web服务代码鸡蛋模型,还有grpc服务代码、grpc网关服务代码的鸡蛋模型在[sponge文档](https://go-sponge.com/zh-cn/learn-about-sponge?id=%f0%9f%8f%b7%e9%a1%b9%e7%9b%ae%e4%bb%a3%e7%a0%81%e9%b8%a1%e8%9b%8b%e6%a8%a1%e5%9e%8b)中有介绍。

<br>

### 主要功能

sponge包含丰富的组件(按需使用):

- 生成代码命令UI界面化,简单易用。
- 自动合并新增模板代码,实现api接口"低代码开发"。
- 不仅生成代码模板示例,而且自动合并新增模板代码,实现api接口"低代码开发"。
- 代码解耦模块化设计,丰富功能组件开箱即用,也可以很方便使用自己的组件。
- Web 框架 [gin](https://github.com/gin-gonic/gin)
- RPC 框架 [grpc](https://github.com/grpc/grpc-go)
Expand Down
Binary file modified assets/sponge-ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.13.0
github.com/rabbitmq/amqp091-go v1.9.0
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil/v3 v3.23.8
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.12.0
Expand All @@ -47,6 +48,7 @@ require (
go.opentelemetry.io/otel/sdk v1.9.0
go.opentelemetry.io/otel/trace v1.9.0
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.14.0
golang.org/x/sync v0.1.0
google.golang.org/genproto v0.0.0-20230223222841-637eb2293923
google.golang.org/grpc v1.53.0
Expand Down Expand Up @@ -170,7 +172,6 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.13.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc3Aoo=
github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down Expand Up @@ -1175,15 +1177,11 @@ gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0=
gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg=
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/plugin/dbresolver v1.4.7 h1:ZwtwmJQxTx9us7o6zEHFvH1q4OeEo1pooU7efmnunJA=
gorm.io/plugin/dbresolver v1.4.7/go.mod h1:l4Cn87EHLEYuqUncpEeTC2tTJQkjngPSD+lo8hIvcT0=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
8 changes: 4 additions & 4 deletions internal/dao/userExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ func (d *userExampleDao) GetByID(ctx context.Context, id uint64) (*model.UserExa
return record, err
}

// get from cache or mysql
// get from cache or database
record, err := d.cache.Get(ctx, id)
if err == nil {
return record, nil
}

if errors.Is(err, model.ErrCacheNotFound) {
// for the same id, prevent high concurrent simultaneous access to mysql
// for the same id, prevent high concurrent simultaneous access to database
val, err, _ := d.sfg.Do(utils.Uint64ToStr(id), func() (interface{}, error) { //nolint
table := &model.UserExample{}
err = d.db.WithContext(ctx).Where("id = ?", id).First(table).Error
Expand Down Expand Up @@ -249,7 +249,7 @@ func (d *userExampleDao) GetByIDs(ctx context.Context, ids []uint64) (map[uint64
return itemMap, nil
}

// get form cache or mysql
// get form cache or database
itemMap, err := d.cache.MultiGet(ctx, ids)
if err != nil {
return nil, err
Expand All @@ -266,7 +266,7 @@ func (d *userExampleDao) GetByIDs(ctx context.Context, ids []uint64) (map[uint64

// get missed data
if len(missedIDs) > 0 {
// find the id of an active placeholder, i.e. an id that does not exist in mysql
// find the id of an active placeholder, i.e. an id that does not exist in database
var realMissedIDs []uint64
for _, id := range missedIDs {
_, err = d.cache.Get(ctx, id)
Expand Down
3 changes: 1 addition & 2 deletions internal/handler/userExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ func (h *userExampleHandler) ListByIDs(c *gin.Context) {
err := c.ShouldBindJSON(form)
if err != nil {
logger.Warn("ShouldBindJSON error: ", logger.Err(err), middleware.GCtxRequestIDField(c))
response.Error(c, ecode.InvalidParams.WithOutMsg("参数错误"), "详细错误信息")
response.Output(c, ecode.Unauthorized.WithOutMsg("错误简单描述").ToHTTPCode(), "详细错误信息")
response.Error(c, ecode.InvalidParams)
return
}

Expand Down

0 comments on commit 0ca5eaf

Please sign in to comment.