Skip to content

Commit ad14614

Browse files
重构部分 block 逻辑,调整 js 执行器的 kv 流程
1 parent 66005cc commit ad14614

File tree

9 files changed

+34
-31
lines changed

9 files changed

+34
-31
lines changed

examples/hello_world/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport"
66
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
77
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8-
<title>js 验证</title>
8+
<title>Hello World</title>
99
</head>
1010
<body>
1111
<p>Hello World</p>

global-types/globals.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// goja.d.ts
1+
// global.d.ts
22

33

44
declare global {
@@ -25,6 +25,7 @@ declare global {
2525
put(key: string, value: string): Promise<void>;
2626
}
2727

28+
// @ts-ignore
2829
const event: EventSystem;
2930

3031
// Request 相关类型
@@ -103,8 +104,8 @@ declare global {
103104
}
104105

105106
interface KVSystem {
106-
repo(group: string): KVOps;
107-
org(group: string): KVOps;
107+
repo(...group: string[]): KVOps;
108+
org(...group: string[]): KVOps;
108109
}
109110

110111
const kv: KVSystem;
@@ -118,6 +119,7 @@ declare global {
118119
debug(...args: any[]): void;
119120
}
120121

122+
// @ts-ignore
121123
const console: Console;
122124
}
123125

pkg/core/domain.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ import (
1212
type PageDomain struct {
1313
*ServerMeta
1414

15-
alias *DomainAlias
1615
baseDomain string
1716
defaultBranch string
1817
}
1918

20-
func NewPageDomain(meta *ServerMeta, alias *DomainAlias, baseDomain, defaultBranch string) *PageDomain {
19+
func NewPageDomain(meta *ServerMeta, baseDomain, defaultBranch string) *PageDomain {
2120
return &PageDomain{
2221
baseDomain: baseDomain,
2322
defaultBranch: defaultBranch,
2423
ServerMeta: meta,
25-
alias: alias,
2624
}
2725
}
2826

@@ -40,7 +38,7 @@ func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch s
4038
}
4139
pathArr := strings.Split(strings.TrimPrefix(path, "/"), "/")
4240
if !strings.HasSuffix(domain, "."+p.baseDomain) {
43-
alias, err := p.alias.Query(ctx, domain) // 确定 alias 是否存在内容
41+
alias, err := p.Alias.Query(ctx, domain) // 确定 alias 是否存在内容
4442
if err != nil {
4543
zap.L().Warn("unknown domain", zap.String("base", p.baseDomain), zap.String("domain", domain), zap.Error(err))
4644
return nil, os.ErrNotExist
@@ -83,10 +81,6 @@ func (p *PageDomain) returnMeta(ctx context.Context, owner, repo, branch string,
8381
result.Owner = owner
8482
result.Repo = repo
8583
result.Path = strings.Join(path, "/")
86-
// todo: 优化保存逻辑 ,减少写入
87-
if err = p.alias.Bind(ctx, meta.Alias, result.Owner, result.Repo, branch); err != nil {
88-
zap.L().Warn("alias binding error.", zap.Error(err))
89-
return nil, err
90-
}
84+
9185
return result, nil
9286
}

pkg/core/meta.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
type ServerMeta struct {
2525
Backend
2626
Domain string
27+
Alias *DomainAlias
2728

2829
client *http.Client
2930
cache *tools.KVCache[PageMetaContent]
@@ -70,10 +71,11 @@ func (m *PageMetaContent) String() string {
7071
return string(marshal)
7172
}
7273

73-
func NewServerMeta(client *http.Client, backend Backend, domain string, cache kv.KV, ttl time.Duration) *ServerMeta {
74+
func NewServerMeta(client *http.Client, backend Backend, domain string, alias *DomainAlias, cache kv.KV, ttl time.Duration) *ServerMeta {
7475
return &ServerMeta{
7576
Backend: backend,
7677
Domain: domain,
78+
Alias: alias,
7779
client: client,
7880
cache: tools.NewCache[PageMetaContent](cache, "meta", ttl),
7981
locker: utils.NewLocker(),
@@ -144,7 +146,11 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
144146
_ = s.cache.Store(ctx, key, *rel)
145147
return nil, err
146148
}
147-
149+
// todo: 优化保存逻辑 ,减少写入
150+
if err = s.Alias.Bind(ctx, rel.Alias, owner, repo, branch); err != nil {
151+
zap.L().Warn("alias binding error.", zap.Error(err))
152+
return nil, err
153+
}
148154
_ = s.cache.Store(ctx, key, *rel)
149155
return rel, nil
150156
}

pkg/filters/block.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package filters
22

33
import (
44
"net/http"
5+
"os"
56

67
"gopkg.d7z.net/gitea-pages/pkg/core"
78
)
@@ -16,7 +17,7 @@ func FilterInstBlock(_ core.Params) (core.FilterInstance, error) {
1617
return nil, err
1718
}
1819
if param.Code == 0 {
19-
param.Code = http.StatusForbidden
20+
return nil, os.ErrNotExist
2021
}
2122
if param.Message == "" {
2223
param.Message = http.StatusText(param.Code)

pkg/filters/goja/goja.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
// todo: 新增超时配置
20+
2021
func FilterInstGoJa(gl core.Params) (core.FilterInstance, error) {
2122
var global struct {
2223
Timeout time.Duration `json:"timeout"`
@@ -50,7 +51,7 @@ func FilterInstGoJa(gl core.Params) (core.FilterInstance, error) {
5051

5152
debug := NewDebug(global.EnableDebug && param.Debug && request.URL.Query().
5253
Get("debug") == "true", request, w)
53-
program, err := goja.Compile("main.js", js, false)
54+
program, err := goja.Compile(param.Exec, js, false)
5455
if err != nil {
5556
return debug.Flush(err)
5657
}

pkg/filters/goja/var_kv.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package goja
22

33
import (
44
"os"
5-
"strings"
65

76
"github.com/dop251/goja"
87
"github.com/pkg/errors"
@@ -12,22 +11,21 @@ import (
1211

1312
func KVInject(ctx core.FilterContext, jsCtx *goja.Runtime) error {
1413
return jsCtx.GlobalObject().Set("kv", map[string]interface{}{
15-
"repo": func(group string) (goja.Value, error) {
16-
return kvResult(ctx.RepoDB)(ctx, jsCtx, group)
14+
"repo": func(group ...string) (goja.Value, error) {
15+
return kvResult(ctx.RepoDB)(ctx, jsCtx, group...)
1716
},
18-
"org": func(group string) (goja.Value, error) {
19-
return kvResult(ctx.OrgDB)(ctx, jsCtx, group)
17+
"org": func(group ...string) (goja.Value, error) {
18+
return kvResult(ctx.OrgDB)(ctx, jsCtx, group...)
2019
},
2120
})
2221
}
2322

24-
func kvResult(db kv.CursorPagedKV) func(ctx core.FilterContext, jsCtx *goja.Runtime, group string) (goja.Value, error) {
25-
return func(ctx core.FilterContext, jsCtx *goja.Runtime, group string) (goja.Value, error) {
26-
group = strings.TrimSpace(group)
27-
if group == "" {
23+
func kvResult(db kv.CursorPagedKV) func(ctx core.FilterContext, jsCtx *goja.Runtime, group ...string) (goja.Value, error) {
24+
return func(ctx core.FilterContext, jsCtx *goja.Runtime, group ...string) (goja.Value, error) {
25+
if len(group) == 0 {
2826
return goja.Undefined(), errors.New("invalid group")
2927
}
30-
db := db.Child(group).(kv.CursorPagedKV)
28+
db := db.Child(group...).(kv.CursorPagedKV)
3129
return jsCtx.ToValue(map[string]interface{}{
3230
"get": func(key string) (goja.Value, error) {
3331
get, err := db.Get(ctx, key)

pkg/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ func NewPageServer(
5454
errorHandler func(w http.ResponseWriter, r *http.Request, err error),
5555
filterConfig map[string]map[string]any,
5656
) (*Server, error) {
57-
svcMeta := core.NewServerMeta(client, backend, domain, cacheMeta, cacheMetaTTL)
58-
pageMeta := core.NewPageDomain(svcMeta, core.NewDomainAlias(db.Child("config", "alias")), domain, defaultBranch)
57+
alias := core.NewDomainAlias(db.Child("config", "alias"))
58+
svcMeta := core.NewServerMeta(client, backend, domain, alias, cacheMeta, cacheMetaTTL)
59+
pageMeta := core.NewPageDomain(svcMeta, domain, defaultBranch)
5960
globCache, err := lru.New[string, glob.Glob](512)
6061
if err != nil {
6162
return nil, err

tests/filter_block_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Test_filter_block(t *testing.T) {
2323
routes:
2424
- path: "bad.html"
2525
block:
26-
code:
26+
code: 403
2727
`)
2828
data, _, err = server.OpenFile("https://org1.example.com/repo1/")
2929
assert.NoError(t, err)
@@ -32,5 +32,5 @@ routes:
3232
assert.Equal(t, 403, resp.StatusCode)
3333
// 默认排除的内容
3434
_, resp, _ = server.OpenFile("https://org1.example.com/repo1/.pages.yaml")
35-
assert.Equal(t, 403, resp.StatusCode)
35+
assert.Equal(t, 404, resp.StatusCode)
3636
}

0 commit comments

Comments
 (0)