Merge pull request #517 from trheyi/main
[add] URL details to request context
This commit is contained in:
commit
b52591a998
4 changed files with 39 additions and 1 deletions
|
|
@ -42,6 +42,12 @@ func NewRequestContext(c *gin.Context) (*Request, int, error) {
|
||||||
Referer: c.Request.Referer(),
|
Referer: c.Request.Referer(),
|
||||||
Headers: url.Values(c.Request.Header),
|
Headers: url.Values(c.Request.Header),
|
||||||
Params: params,
|
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
|
}, 200, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ func TestPageExec(t *testing.T) {
|
||||||
|
|
||||||
page := testPage(t)
|
page := testPage(t)
|
||||||
request := &Request{
|
request := &Request{
|
||||||
|
URL: ReqeustURL{Path: "/test/path"},
|
||||||
Query: map[string][]string{"show": {"yes"}},
|
Query: map[string][]string{"show": {"yes"}},
|
||||||
Locale: "zh-CN",
|
Locale: "zh-CN",
|
||||||
Theme: "dark",
|
Theme: "dark",
|
||||||
|
|
@ -30,6 +31,7 @@ func TestPageExec(t *testing.T) {
|
||||||
res := any.Of(data).Map().Dot()
|
res := any.Of(data).Map().Dot()
|
||||||
assert.Equal(t, "yes", res.Get("array[3][0].query"))
|
assert.Equal(t, "yes", res.Get("array[3][0].query"))
|
||||||
assert.Equal(t, "Article Search", res.Get("articles.data[0].description"))
|
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) {
|
func prepare(t *testing.T) {
|
||||||
|
|
@ -149,7 +151,8 @@ func testPage(t *testing.T) *Page {
|
||||||
{"$images": "scripts.article.Images"},
|
{"$images": "scripts.article.Images"},
|
||||||
{"process": "scripts.article.Thumbs", "args": ["$query.show"], "__exec": true }
|
{"process": "scripts.article.Thumbs", "args": ["$query.show"], "__exec": true }
|
||||||
],
|
],
|
||||||
"input": { "data": "hello world" }
|
"input": { "data": "hello world" },
|
||||||
|
"url": {"path":"$url.path"}
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ func NewRequestMock(mock *PageMock) *Request {
|
||||||
Referer: mock.Referer,
|
Referer: mock.Referer,
|
||||||
Headers: mock.Headers,
|
Headers: mock.Headers,
|
||||||
Params: mock.Params,
|
Params: mock.Params,
|
||||||
|
URL: mock.URL,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,6 +86,24 @@ func (r *Request) execValue(value interface{}) (interface{}, error) {
|
||||||
return "", nil
|
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.") {
|
if strings.HasPrefix(v, "$header.") {
|
||||||
key := strings.TrimLeft(v, "$header.")
|
key := strings.TrimLeft(v, "$header.")
|
||||||
if r.Headers.Has(key) {
|
if r.Headers.Has(key) {
|
||||||
|
|
|
||||||
|
|
@ -155,6 +155,7 @@ type Request struct {
|
||||||
Params map[string]string `json:"params,omitempty"`
|
Params map[string]string `json:"params,omitempty"`
|
||||||
Headers url.Values `json:"headers,omitempty"`
|
Headers url.Values `json:"headers,omitempty"`
|
||||||
Body interface{} `json:"body,omitempty"`
|
Body interface{} `json:"body,omitempty"`
|
||||||
|
URL ReqeustURL `json:"url,omitempty"`
|
||||||
Sid string `json:"sid,omitempty"`
|
Sid string `json:"sid,omitempty"`
|
||||||
Theme string `json:"theme,omitempty"`
|
Theme string `json:"theme,omitempty"`
|
||||||
Locale string `json:"locale,omitempty"`
|
Locale string `json:"locale,omitempty"`
|
||||||
|
|
@ -215,9 +216,18 @@ type PageMock struct {
|
||||||
Params map[string]string `json:"params,omitempty"`
|
Params map[string]string `json:"params,omitempty"`
|
||||||
Headers url.Values `json:"headers,omitempty"`
|
Headers url.Values `json:"headers,omitempty"`
|
||||||
Body interface{} `json:"body,omitempty"`
|
Body interface{} `json:"body,omitempty"`
|
||||||
|
URL ReqeustURL `json:"url,omitempty"`
|
||||||
Sid string `json:"sid,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
|
// PageConfig is the struct for the page config
|
||||||
type PageConfig struct {
|
type PageConfig struct {
|
||||||
PageSetting `json:",omitempty"`
|
PageSetting `json:",omitempty"`
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue