Merge pull request #517 from trheyi/main

[add] URL details to request context
This commit is contained in:
Max 2023-12-11 10:04:54 +08:00 committed by GitHub
commit b52591a998
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 1 deletions

View file

@ -42,6 +42,12 @@ func NewRequestContext(c *gin.Context) (*Request, int, error) {
Referer: c.Request.Referer(),
Headers: url.Values(c.Request.Header),
Params: params,
URL: core.ReqeustURL{
Host: c.Request.Host,
Path: c.Request.URL.Path,
Domain: c.Request.URL.Hostname(),
Scheme: c.Request.URL.Scheme,
},
},
}, 200, nil
}

View file

@ -15,6 +15,7 @@ func TestPageExec(t *testing.T) {
page := testPage(t)
request := &Request{
URL: ReqeustURL{Path: "/test/path"},
Query: map[string][]string{"show": {"yes"}},
Locale: "zh-CN",
Theme: "dark",
@ -30,6 +31,7 @@ func TestPageExec(t *testing.T) {
res := any.Of(data).Map().Dot()
assert.Equal(t, "yes", res.Get("array[3][0].query"))
assert.Equal(t, "Article Search", res.Get("articles.data[0].description"))
assert.Equal(t, "/test/path", res.Get("url.path"))
}
func prepare(t *testing.T) {
@ -149,7 +151,8 @@ func testPage(t *testing.T) *Page {
{"$images": "scripts.article.Images"},
{"process": "scripts.article.Thumbs", "args": ["$query.show"], "__exec": true }
],
"input": { "data": "hello world" }
"input": { "data": "hello world" },
"url": {"path":"$url.path"}
}
`,
},

View file

@ -32,6 +32,7 @@ func NewRequestMock(mock *PageMock) *Request {
Referer: mock.Referer,
Headers: mock.Headers,
Params: mock.Params,
URL: mock.URL,
}
}
@ -85,6 +86,24 @@ func (r *Request) execValue(value interface{}) (interface{}, error) {
return "", nil
}
if strings.HasPrefix(v, "$url.") {
key := strings.TrimLeft(v, "$url.")
switch key {
case "path":
return r.URL.Path, nil
case "host":
return r.URL.Host, nil
case "domain":
return r.URL.Domain, nil
case "scheme":
return r.URL.Scheme, nil
}
return "", nil
}
if strings.HasPrefix(v, "$header.") {
key := strings.TrimLeft(v, "$header.")
if r.Headers.Has(key) {

View file

@ -155,6 +155,7 @@ type Request struct {
Params map[string]string `json:"params,omitempty"`
Headers url.Values `json:"headers,omitempty"`
Body interface{} `json:"body,omitempty"`
URL ReqeustURL `json:"url,omitempty"`
Sid string `json:"sid,omitempty"`
Theme string `json:"theme,omitempty"`
Locale string `json:"locale,omitempty"`
@ -215,9 +216,18 @@ type PageMock struct {
Params map[string]string `json:"params,omitempty"`
Headers url.Values `json:"headers,omitempty"`
Body interface{} `json:"body,omitempty"`
URL ReqeustURL `json:"url,omitempty"`
Sid string `json:"sid,omitempty"`
}
// ReqeustURL is the struct for the request
type ReqeustURL struct {
Host string `json:"host,omitempty"`
Domain string `json:"domain,omitempty"`
Path string `json:"path,omitempty"`
Scheme string `json:"scheme,omitempty"`
}
// PageConfig is the struct for the page config
type PageConfig struct {
PageSetting `json:",omitempty"`