Skip to content

Commit ccba021

Browse files
authored
Merge pull request glennliao#16 from glennliao/dev
v0.2.0 alpha
2 parents e86841c + 5c43c8e commit ccba021

Some content is hidden

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

54 files changed

+2365
-1262
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: tests
2+
on:
3+
push:
4+
branches: [ "main" ,"dev"]
5+
pull_request:
6+
branches: [ "main", "dev" ]
7+
8+
jobs:
9+
simple-test:
10+
run: cd test && go test -short

.github/workflows/todo.yml

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
name: Todo tests
2-
3-
on:
4-
push:
5-
branches: [ "main" ,"dev"]
6-
pull_request:
7-
branches: [ "main", "dev" ]
8-
9-
jobs:
10-
11-
build:
12-
runs-on: ubuntu-latest
13-
services:
14-
mysql:
15-
image: mysql:5.7.37
16-
env:
17-
MYSQL_ROOT_PASSWORD: 'yourpassword'
18-
MYSQL_DATABASE: 'my_apijson'
19-
ports:
20-
- 3306:3306
21-
options: >-
22-
--health-cmd="mysqladmin ping"
23-
--health-interval=10s
24-
--health-timeout=5s
25-
--health-retries=3
26-
steps:
27-
- uses: actions/checkout@v3
28-
29-
- name: Set up Go
30-
uses: actions/setup-go@v3
31-
with:
32-
go-version: 1.18
33-
34-
35-
36-
- name: Test
37-
run: |
38-
39-
mysql -uroot -h 127.0.0.1 --port 3306 -pyourpassword my_apijson < @demo/todo/doc/todo.sql
40-
41-
cd ./@demo/todo
42-
mv config.yaml.example config.yaml
43-
44-
cd tests
45-
go test -v
1+
#name: Todo tests
2+
#
3+
#on:
4+
# push:
5+
# branches: [ "main" ,"dev"]
6+
# pull_request:
7+
# branches: [ "main", "dev" ]
8+
#
9+
#jobs:
10+
#
11+
# build:
12+
# runs-on: ubuntu-latest
13+
# services:
14+
# mysql:
15+
# image: mysql:5.7.37
16+
# env:
17+
# MYSQL_ROOT_PASSWORD: 'yourpassword'
18+
# MYSQL_DATABASE: 'my_apijson'
19+
# ports:
20+
# - 3306:3306
21+
# options: >-
22+
# --health-cmd="mysqladmin ping"
23+
# --health-interval=10s
24+
# --health-timeout=5s
25+
# --health-retries=3
26+
# steps:
27+
# - uses: actions/checkout@v3
28+
#
29+
# - name: Set up Go
30+
# uses: actions/setup-go@v3
31+
# with:
32+
# go-version: 1.18
33+
#
34+
#
35+
#
36+
# - name: Test
37+
# run: |
38+
#
39+
# mysql -uroot -h 127.0.0.1 --port 3306 -pyourpassword my_apijson < @demo/todo/doc/todo.sql
40+
#
41+
# cd ./@demo/todo
42+
# mv config.yaml.example config.yaml
43+
#
44+
# cd tests
45+
# go test -v

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.idea
22
*.exe
3-
config.yaml
4-
test
3+
*.sqlite3

@doc/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
1. 这东西有什么用?
44
2. 我的接口业务流程很复杂怎么处理?
5-
3. 你知道graphql吗?
5+
3. 知道graphql吗?
66
4. 如何完成文件上传下载、微信支付等操作?
77
5. 我项目中没有使用goframe, 会有gin、beego等框架的版本吗?

@doc/roadmap.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
- [x] 简易单表的post,put,delete操作 (可一次传递多个表, 单表指表表之间无关联)
88
- [x] 查询权限控制(表、行、字段)
99

10-
## [ ] 下水 阶段
10+
## [x] 下水 阶段
1111
- [x] 增加 hook (钩子函数), 用于对接口的的前置、后置处理 (例如修改数据、清理缓存等操作)
1212
- [x] 增加 设置 rowKey 生成 (id生成)
1313
- [x] 新增多个表时, structures 中 update可引用其他边的字段 (例如新建角色,并使用新增的id 设置给权限列表中)
1414
- [x] 批量修改(统一设置模式+单个设置模式)、批量删除
1515
- [x] request的version设置
1616
- [x] 增加 functions , 用于对数据的处理 (字典转换、id转name等操作)
17-
18-
19-
## [ ] 摸石头 阶段
20-
- [ ] 开发、测试环境下记录get请求记录
2117
- [x] 可自定义get中查询节点的实现 (即从缓存或者其他数据源查询数据)
2218
- [x] 可自定义非开放请求的具体实现
2319
- [x] 提供 web UI 用于 _access 和 _request 的管理
2420

21+
## [ ] 摸石头 阶段
22+
- [ ] 开发、测试环境下记录get请求记录
23+
- [ ] 多实例
24+
- [ ] 错误提示
25+
2526
## [ ] xxx 阶段
2627
- [ ] get请求的复杂度计算, 超过复杂度则拒绝执行(树节点计算)
2728
- [ ] 完善 apijson 规范的兼容实现
29+
- [ ] 提供一份较好的文档
2830

2931

3032

@@ -41,7 +43,6 @@
4143
- [x] MUST
4244
- [x] REFUSE
4345
- [x] 分页返回total@
44-
4546
- [x] 可用的权限方案
4647
- [x] get只有access中定义的才能访问
4748
- [x] 非get操作则必须与request指定一致才可请求

@doc/v0.2.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# v0.2
2+
3+
> 框架使用上会与apijson(java)不完全一致, 但是与前端的json请求响应规则 (即apijson的语法规则)会尽可能兼容
4+
5+
1. 由全局配置改成apijson实例,可创建不同实例对应不同的内容
6+
2. 代码内部 传递的accessName 都为 _access 配置中的alias
7+
3. access/request配置可从配置文件中获取

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# apijson-go [下水阶段]
1+
# apijson-go [摸石头阶段]
22
基于 go + goframe 实现的 apijson
33

44
> ~~暂仍处于前期开发探索中, 请仅使用在 个人探索项目~~
5-
6-
> 目前处于 【下水阶段】, 欢迎测试、issue、建议、pr
5+
> ~~目前处于 【下水阶段】, 欢迎测试、issue、建议、pr~~
6+
> 目前处于 【摸石头阶段】, 欢迎测试、issue、建议、pr, 请勿使用在非个人的线上项目
77
88
[RoadMap 阶段规划](./@doc/roadmap.md)
99

action/action.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package action
22

33
import (
44
"context"
5-
"github.com/glennliao/apijson-go/config/db"
5+
"github.com/glennliao/apijson-go/config"
66
"github.com/glennliao/apijson-go/consts"
7+
"github.com/glennliao/apijson-go/model"
78
"github.com/gogf/gf/v2/database/gdb"
89
"github.com/gogf/gf/v2/errors/gerror"
910
"github.com/gogf/gf/v2/frame/g"
@@ -14,20 +15,36 @@ import (
1415
// Action 非get查询的request表中的请求
1516
type Action struct {
1617
ctx context.Context
17-
tagRequest *db.Request
18+
tagRequest *config.Request
1819
method string
1920

20-
req g.Map
21+
req model.Map
2122

2223
err error
2324

2425
children map[string]*Node
2526
keyNode map[string]*Node
27+
28+
// 关闭 access 权限验证, 默认否
29+
NoAccessVerify bool
30+
// 关闭 request 验证开关, 默认否
31+
NoRequestVerify bool
32+
33+
//Access *config.Access
34+
35+
// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
36+
DbFieldStyle config.FieldStyle
37+
38+
// jsonFieldStyle 数据库返回的字段
39+
JsonFieldStyle config.FieldStyle
40+
41+
Functions *config.Functions
42+
actionConfig *config.ActionConfig
2643
}
2744

28-
func New(ctx context.Context, method string, req g.Map) *Action {
45+
func New(ctx context.Context, actionConfig *config.ActionConfig, method string, req model.Map) *Action {
2946

30-
request, err := checkTag(req, method)
47+
request, err := checkTag(req, method, actionConfig)
3148
if err != nil {
3249
panic(err)
3350
}
@@ -36,12 +53,13 @@ func New(ctx context.Context, method string, req g.Map) *Action {
3653
delete(req, "version")
3754

3855
a := &Action{
39-
ctx: ctx,
40-
tagRequest: request,
41-
method: method,
42-
req: req,
43-
children: map[string]*Node{},
44-
keyNode: map[string]*Node{},
56+
ctx: ctx,
57+
tagRequest: request,
58+
method: method,
59+
req: req,
60+
children: map[string]*Node{},
61+
keyNode: map[string]*Node{},
62+
actionConfig: actionConfig,
4563
}
4664
return a
4765
}
@@ -63,16 +81,19 @@ func (a *Action) parse() error {
6381
}
6482
}
6583

66-
var list []g.Map
67-
_v, ok := v.(g.Map)
84+
var list []model.Map
85+
_v, ok := v.(model.Map)
6886
if ok { // 将所有node都假设成列表, 如果单个则看成一个元素的批量
69-
list = []g.Map{_v}
87+
list = []model.Map{_v}
7088
} else {
71-
list = gconv.SliceMap(v)
89+
for _, m := range gconv.Maps(v) {
90+
list = append(list, m)
91+
}
7292
}
7393

7494
node := newNode(key, list, structure, a.tagRequest.Executor[key])
7595
node.ctx = a.ctx
96+
node.action = a
7697
a.keyNode[key] = &node
7798
node.keyNode = a.keyNode
7899
err := node.parse(a.ctx, a.method)
@@ -86,18 +107,18 @@ func (a *Action) parse() error {
86107
return nil
87108
}
88109

89-
func (a *Action) Result() (g.Map, error) {
110+
func (a *Action) Result() (model.Map, error) {
90111

91112
err := a.parse()
92113
if err != nil {
93114
return nil, err
94115
}
95116

96-
ret := g.Map{}
117+
ret := model.Map{}
97118

98119
for _, k := range a.tagRequest.ExecQueue {
99120
node := a.children[k]
100-
err = EmitHook(a.ctx, BeforeExec, node, a.method)
121+
err = EmitHook(a.ctx, BeforeNodeExec, node, a.method)
101122
if err != nil {
102123
return nil, err
103124
}
@@ -112,9 +133,8 @@ func (a *Action) Result() (g.Map, error) {
112133
}
113134
}
114135

115-
err = g.DB().Transaction(a.ctx, func(ctx context.Context, tx *gdb.TX) error {
136+
err = g.DB().Transaction(a.ctx, func(ctx context.Context, tx gdb.TX) error {
116137
for _, k := range a.tagRequest.ExecQueue {
117-
118138
node := a.children[k]
119139
ret[k], err = node.execute(ctx, a.method)
120140
if err != nil {
@@ -130,7 +150,7 @@ func (a *Action) Result() (g.Map, error) {
130150

131151
for _, k := range a.tagRequest.ExecQueue {
132152
node := a.children[k]
133-
err = EmitHook(a.ctx, AfterExec, node, a.method)
153+
err = EmitHook(a.ctx, AfterNodeExec, node, a.method)
134154
if err != nil {
135155
return nil, err
136156
}
@@ -139,7 +159,7 @@ func (a *Action) Result() (g.Map, error) {
139159
return ret, err
140160
}
141161

142-
func checkTag(req g.Map, method string) (*db.Request, error) {
162+
func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.Request, error) {
143163
_tag, ok := req["tag"]
144164
if !ok {
145165
return nil, gerror.New("tag 缺失")
@@ -148,7 +168,7 @@ func checkTag(req g.Map, method string) (*db.Request, error) {
148168
tag := gconv.String(_tag)
149169
version := req["version"]
150170

151-
request, err := db.GetRequest(tag, method, gconv.String(version))
171+
request, err := requestCfg.GetRequest(tag, method, gconv.String(version))
152172
if err != nil {
153173
return nil, err
154174
}

action/hook.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package action
33
import "context"
44

55
const (
6-
BeforeExec = iota
7-
AfterExec
8-
BeforeDo
9-
AfterDo
6+
BeforeNodeExec = iota
7+
AfterNodeExec
8+
BeforeExecutorDo
9+
AfterExecutorDo
1010
)
1111

1212
type Hook struct {
1313
For string //
1414
// Exec 事务外
15-
BeforeExec func(ctx context.Context, n *Node, method string) error
16-
AfterExec func(ctx context.Context, n *Node, method string) error
15+
BeforeNodeExec func(ctx context.Context, n *Node, method string) error
16+
AfterNodeExec func(ctx context.Context, n *Node, method string) error
1717

1818
// Do 事务内
19-
BeforeDo func(ctx context.Context, n *Node, method string) error
20-
AfterDo func(ctx context.Context, n *Node, method string) error
19+
BeforeExecutorDo func(ctx context.Context, n *Node, method string) error
20+
AfterExecutorDo func(ctx context.Context, n *Node, method string) error
2121
}
2222

2323
var hooksMap = map[string][]Hook{}
@@ -33,14 +33,14 @@ func EmitHook(ctx context.Context, hookAt int, node *Node, method string) error
3333

3434
var handler func(ctx context.Context, n *Node, method string) error
3535
switch hookAt {
36-
case BeforeExec:
37-
handler = hook.BeforeExec
38-
case AfterExec:
39-
handler = hook.AfterExec
40-
case BeforeDo:
41-
handler = hook.BeforeDo
42-
case AfterDo:
43-
handler = hook.AfterDo
36+
case BeforeNodeExec:
37+
handler = hook.BeforeNodeExec
38+
case AfterNodeExec:
39+
handler = hook.AfterNodeExec
40+
case BeforeExecutorDo:
41+
handler = hook.BeforeExecutorDo
42+
case AfterExecutorDo:
43+
handler = hook.AfterExecutorDo
4444
}
4545

4646
if handler != nil {

0 commit comments

Comments
 (0)