Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] URL details to request context #517

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sui/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion sui/core/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down Expand Up @@ -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"}
}
`,
},
Expand Down
19 changes: 19 additions & 0 deletions sui/core/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewRequestMock(mock *PageMock) *Request {
Referer: mock.Referer,
Headers: mock.Headers,
Params: mock.Params,
URL: mock.URL,
}
}

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down