Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Mar 24, 2024
1 parent bd7382e commit b6ce149
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 31 deletions.
12 changes: 8 additions & 4 deletions .github/RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Change log

- Upgrade `go.opentelemetry.io/otel` and `golang.org/x/crypto` third-party libraries.
- Adjusting the order of import packages.
- Fix generate code based on postgresql bug [#25](https://github.com/zhufuyi/sponge/issues/25).
- Modify type,go type `time.Time` --> protobuf `string RFC3339`
- Code generation supports multiple types of code repositories, you can choose to use `monolithic application single repository (monolith)`, `microservice multi-repository (multi-repo)`, or `microservice single repository (mono-repo)` according to your project needs.
- Added automated testing scripts for code generation commands.
- Based on protobuf to generate web services, the generated template code and documentation must meet the following conditions:
- rpc cannot be set as stream type.
- rpc must set http related information (router and method).
- RPC stream based on protobuf supports generating corresponding template code and client testing code.
- The generated code based on protobuf supports some common special types, such as Empty, Any, Timestamp, etc.
- Fixed known bugs.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@

</div>

**Sponge** is a basic development framework that integrates `code auto generation`, `Gin and GRPC`, sponge has a rich set of code generation commands, generating different functional code can be combined into a complete service (similar to the way that artificially broken sponge cells can automatically recombine into a new sponge). The code is decoupled and modularly designed, it is easy to build a complete project from development to deployment, just fill in the business logic code on the generated template code, greatly improved development efficiency and reduced development difficulty, the use of Go can also be "low-code development".
**Sponge** is a powerful development framework that integrates `automatic code generation`, `Gin and GRPC`. Sponge has a rich set of code generation commands, and the generated different functional codes can be combined into a complete service (similar to how artificially broken sponge cells can automatically reassemble into a new complete sponge). From development, testing, API documentation to deployment, one-stop project development greatly improves development efficiency and reduces development difficulty, you can also use the go language to develop projects using the "low-code way".

<br>

If you are develop RESTful web service or microservice with a simple CRUD API interface, you don't need to write any Go code can be compiled and deployed to the linux server, docker, k8s, just need to connect to the database (mysql、mongodb、postgresql、tidb、sqlite) to generate a complete service code.
If you are develop web service or microservice with CRUD APIs, you don't need to write any Go code can be compiled and deployed to the linux server, docker, k8s, just need to connect to the database (mysql、mongodb、postgresql、tidb、sqlite) to generate a complete service code.

If you develop generic web or microservices, just focus on the three core parts of `define tables in the database`, `define api interfaces in the proto files`, `fill in business logic code in the generated template files`, and the rest of the go code is automatically generated by sponge.
If you develop generic web or microservices, just focus on the three core parts of `define tables in the database`, `define api description information in the proto files`, `fill in business logic code in the generated template files`, and the rest of the go code is automatically generated by sponge.

<br>

### 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. `SQL` supports databases **mysql**, **mongodb**, **postgresql**, **tidb**, **sqlite**.
Sponge generated service code supports two types of code repositories:

1. **Monolithic application single repository (monolith) or microservice multi-repository (multi-repo)**: Each service code has its own Git repository. Even if all services are placed in the same Git repository, the code between services cannot be reused. This is the default type generated.

2. **Microservice single repository (mono-repo)**: All services are in the same Git repository, and the code between different services can be reused.

<br>

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**, **mongodb**, **postgresql**, **tidb**, **sqlite**.

#### Generate Code Framework

Expand All @@ -47,7 +55,7 @@ sponge is mainly based on `SQL` and `Protobuf` two ways to generate code, each w

#### Generate Service 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's code generation function as a hen, the generated service code is the egg, take the generated web service backend code as an example, egg model profiling diagram:
The sponge code generation process strips away the business logic and non-business logic of the two major parts of the code. The code generation function of Sponge can be seen as a hen, and the service code generated by sponge can be seen as eggs. Currently, it supports the generation of 5 common types of service code, Take the generated web service backend code as an example, egg model profiling diagram:

<p align="center">
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_web-http-pb-anatomy.png">
Expand Down Expand Up @@ -108,7 +116,7 @@ The project code directory structure created by sponge follows the [project-layo
├── configs # Directory for configuration files
├── deployments # Directory for deployment scripts, supporting Bare Metal, Docker and Kubernetes deployments.
├─ docs # Directory for API interface Swagger documentation.
├── internal # Directory for code of private applications and libraries.
├── i(I)nternal # Directory for business logic code, if the first letter is lowercase (internal), it means private code, if the first letter is capitalised (Internal), it means it can be reused by other code.
│ ├── cache # Cache directory wrapped around business logic.
│ ├── config # Directory for Go structure configuration files.
│ ├── dao # Data access directory.
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.
4 changes: 2 additions & 2 deletions assets/install-cn.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

要求使用go 1.16以上版本[https://studygolang.com/dl](https://studygolang.com/dl)
要求使用go 1.20以上版本[https://studygolang.com/dl](https://studygolang.com/dl)

> 注:如果不能科学上网,获取github的库可能会遇到超时失败问题,建议设置为国内代理,执行命令 **go env -w GOPROXY=https://goproxy.cn,direct**
Expand All @@ -15,7 +15,7 @@
为了安装方便,已经把sponge及其依赖的程序打包在一起,下载地址(选择一个下载即可):

- 百度云: [**sponge-install.zip**](https://pan.baidu.com/s/1fiTiMROkiIIzAdj2bk93CA?pwd=prys)
- 百度云[**sponge-install.zip**](https://pan.baidu.com/s/1fiTiMROkiIIzAdj2bk93CA?pwd=prys)
- 蓝奏云:[**sponge安装文件**](https://wwm.lanzoue.com/b049fldpi) 密码:5rq9,共下载4个文件,安装前先看`安装说明.txt`文件。

下载文件后:
Expand Down
2 changes: 1 addition & 1 deletion assets/install-en.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Requirements Go 1.16 and above, [https://go.dev/doc/install](https://go.dev/doc/install)
Requires go1.20 or higher, [https://go.dev/doc/install](https://go.dev/doc/install)

<br>

Expand Down
19 changes: 13 additions & 6 deletions assets/readme-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

<br>

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

<br>

如果开发只有简单CRUD api接口的web或微服务,不需要编写任何go代码就可以编译并部署到linux服务器、docker、k8s上,只需要连接到数据库(mysql、mongodb、postgresql、tidb、sqlite)就可以一键自动生成完整的服务代码。
如果开发只有CRUD api接口的web或微服务,不需要编写任何go代码就可以编译并部署到linux服务器、docker、k8s上,只需要连接到数据库(mysql、mongodb、postgresql、tidb、sqlite)就可以一键自动生成完整的服务代码。

如果开发通用的web或微服务,只需聚焦`在数据库定义表``在proto文件定义api接口``在生成的模板文件填写业务逻辑代码`三个核心部分,其他go代码都由sponge自动生成。
如果开发通用的web或微服务,只需聚焦`在数据库定义表``在proto文件定义api描述信息``在生成的模板文件填写业务逻辑代码`三个核心部分,其他go代码都由sponge自动生成。

<br>

### 生成代码框架

sponge生成的服务代码支持两种类型代码仓库:

1. **单体应用单体仓库(monolith)或微服务多仓库(multi-repo)**:每个服务代码都有自己的git仓库,即使把所有服务放在同一个git仓库下,服务之间代码不可以复用,默认是生成这种类型。
2. **微服务单体仓库(mono-repo)**:在所有服务都在同一个git仓库下,不同服务之间的代码可以复用。

<br>

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

#### 生成代码的框架图
Expand All @@ -34,7 +41,7 @@ sponge主要基于`SQL`和`Protobuf`两种方式生成代码,每种方式拥

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

sponge生成代码过程中剥离了业务逻辑与非业务逻辑两大部分代码,把sponge的生成代码功能看作是母鸡,生成的服务代码就是鸡蛋,以生成的一个web服务后端代码为例,鸡蛋模型剖析图:
sponge生成代码过程中剥离了业务逻辑与非业务逻辑两大部分代码,把sponge的生成代码功能看作是一个母鸡,sponge生成的服务代码看作是鸡蛋,目前支持生成常见的5种类型服务代码,以生成的一个web服务后端代码为例,鸡蛋模型剖析图:

<p align="center">
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/web-http-pb-anatomy.png">
Expand Down Expand Up @@ -94,7 +101,7 @@ sponge包含丰富的组件(按需使用):
├── configs # 配置文件的目录
├── deployments # IaaS、PaaS、系统和容器协调部署的配置和模板目录
├── docs # 设计文档和界面文档目录
├── internal # 私有应用程序和库的代码目录
├── i(I)nternal # 业务逻辑代码目录,如果首字母是小写(internal),表示私有代码,如果首字母大写(Internal)表示可以被其他代码复用。
│ ├── cache # 基于业务包装的缓存目录
│ ├── config # Go结构的配置文件目录
│ ├── dao # 数据访问目录
Expand Down Expand Up @@ -168,7 +175,7 @@ sponge run

### 视频介绍

> sponge v1.5.0版本以后的UI界面的左边菜单栏有一些修改,下面视频演示使用v1.5.0之前版本,左边菜单栏看起来会有些不同
> 视频教程演示使用sponge v1.3.12版本,新版本增加了一些自动化功能、调整了一些UI界面和菜单,建议结合[文档教程](https://go-sponge.com/zh-cn/)使用
- [01 sponge的形成过程](https://www.bilibili.com/video/BV1s14y1F7Fz/)
- [02 sponge的框架介绍](https://www.bilibili.com/video/BV13u4y1F7EU/)
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.
12 changes: 5 additions & 7 deletions internal/dao/userExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"fmt"
"time"

"github.com/zhufuyi/sponge/internal/cache"
"github.com/zhufuyi/sponge/internal/model"
"golang.org/x/sync/singleflight"
"gorm.io/gorm"

cacheBase "github.com/zhufuyi/sponge/pkg/cache"
"github.com/zhufuyi/sponge/pkg/ggorm/query"
"github.com/zhufuyi/sponge/pkg/utils"

"golang.org/x/sync/singleflight"
"gorm.io/gorm"
"github.com/zhufuyi/sponge/internal/cache"
"github.com/zhufuyi/sponge/internal/model"
)

var _ UserExampleDao = (*userExampleDao)(nil)
Expand Down Expand Up @@ -63,9 +63,7 @@ func (d *userExampleDao) deleteCache(ctx context.Context, id uint64) error {

// Create a record, insert the record and the id value is written back to the table
func (d *userExampleDao) Create(ctx context.Context, table *model.UserExample) error {
err := d.db.WithContext(ctx).Create(table).Error
_ = d.deleteCache(ctx, table.ID)
return err
return d.db.WithContext(ctx).Create(table).Error
}

// DeleteByID delete a record by id
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/userExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (h *userExampleHandler) ListByIDs(c *gin.Context) {
// @Tags userExample
// @accept json
// @Produce json
// @Param lastID query int true "last id, default is MaxInt32"
// @Param lastID query int true "last id, default is MaxInt32" default(0)
// @Param limit query int false "size in each page" default(10)
// @Param sort query string false "sort by column name of table, and the "-" sign before column name indicates reverse order" default(-id)
// @Success 200 {object} types.ListUserExamplesRespond{}
Expand Down
Binary file modified pkg/ggorm/test_sqlite.db
Binary file not shown.
68 changes: 68 additions & 0 deletions pkg/gofile/filePath.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,74 @@ func FuzzyMatchFiles(f string) []string {
return files
}

// ListDirs list all sub dirs, not including itself
func ListDirs(specifiedDir string) ([]string, error) {
dir, err := os.ReadDir(specifiedDir)
if err != nil {
return nil, err
}

var dirs []string
for _, fi := range dir {
if fi.IsDir() {
dirs = append(dirs, filepath.Join(specifiedDir, fi.Name()))
tmpDirs, err := ListDirs(filepath.Join(specifiedDir, fi.Name()))
if err != nil {
return nil, err
}
dirs = append(dirs, tmpDirs...)
}
}

return dirs, nil
}

// FilterDirs filter directories that meet the criteria
func FilterDirs(dirs []string, opts Option) []string {
o := defaultOptions()
o.apply(opts)

var filteredDirs []string
for _, dir := range dirs {
files, err := os.ReadDir(dir)
if err != nil {
continue
}

existDir := map[string]struct{}{}
for _, file := range files {
if file.IsDir() {
continue
}
switch o.filter {
case prefix:
if matchPrefix(o.name)(file.Name()) {
if _, ok := existDir[dir]; !ok {
existDir[dir] = struct{}{}
filteredDirs = append(filteredDirs, dir)
}
}
case suffix:
if matchSuffix(o.name)(file.Name()) {
if _, ok := existDir[dir]; !ok {
existDir[dir] = struct{}{}
filteredDirs = append(filteredDirs, dir)
}
}
case contain:
if matchContain(o.name)(file.Name()) {
if _, ok := existDir[dir]; !ok {
existDir[dir] = struct{}{}
filteredDirs = append(filteredDirs, dir)
}
}
}
}
}

return filteredDirs
}

// iterative traversal of documents with filter conditions
func walkDirWithFilter(dirPath string, allFiles *[]string, filter filterFn) error {
files, err := os.ReadDir(dirPath)
Expand Down
13 changes: 13 additions & 0 deletions pkg/gofile/filePath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,16 @@ func TestJoin(t *testing.T) {
path = Join(elements...)
t.Log(path)
}

func TestListDirs(t *testing.T) {
dir := ".."
dirs, err := ListDirs(dir)
if err != nil {
t.Error(err)
return
}

t.Log(FilterDirs(dirs, WithSuffix(".txt")))
t.Log(FilterDirs(dirs, WithPrefix("query")))
t.Log(FilterDirs(dirs, WithContain("auth")))
}
1 change: 0 additions & 1 deletion pkg/grpc/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func (b *bench) Run() error {
runner.WithProtoFile(b.protoFile, b.dependentProtoFilePath),
runner.WithBinaryData(data),
runner.WithInsecure(true),
runner.WithStreamMessageProvider(nil),
// more parameter settings https://github.com/bojand/ghz/blob/master/runner/options.go#L41
// example settings: https://github.com/bojand/ghz/blob/master/runner/options_test.go#L79
}
Expand Down
10 changes: 7 additions & 3 deletions scripts/protoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ function generateBySpecifiedProto(){
echo -e "${highBright}Tip:${markEnd} execute the command ${colorCyan}make run${markEnd} and then visit ${colorCyan}http://localhost:8080/apis/swagger/index.html${markEnd} in your browser."
echo ""
# delete the templates code end

if [ "$suitedMonoRepo" == "true" ]; then
sponge patch adapt-mono-repo
fi
}

# generate pb.go by all proto files
Expand All @@ -196,6 +200,9 @@ generateByAllProto
# generate pb.go by specified proto files
generateBySpecifiedProto

# check and add the special_types.go file
sponge patch add-special-types

# delete unused packages in pb.go
handlePbGoFiles $protoBasePath

Expand All @@ -206,8 +213,5 @@ sponge patch del-omitempty --dir=$protoBasePath --suffix-name=pb.go > /dev/null
sponge patch modify-dup-num --dir=internal/ecode
sponge patch modify-dup-err-code --dir=internal/ecode

go mod tidy
checkResult $?

echo "generated code successfully."
echo ""

0 comments on commit b6ce149

Please sign in to comment.