From 9eccf9b802c56866d92105ee54a1077d7400eb96 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 11 Dec 2023 10:04:08 +0800 Subject: [PATCH] [add] URL details to request context --- sui/api/request.go | 6 ++++++ sui/core/page_test.go | 5 ++++- sui/core/request.go | 19 +++++++++++++++++++ sui/core/types.go | 10 ++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/sui/api/request.go b/sui/api/request.go index 38fb0dea3..f26d2dc88 100644 --- a/sui/api/request.go +++ b/sui/api/request.go @@ -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 } diff --git a/sui/core/page_test.go b/sui/core/page_test.go index 38e124a7d..e8c1dbf59 100644 --- a/sui/core/page_test.go +++ b/sui/core/page_test.go @@ -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"} } `, }, diff --git a/sui/core/request.go b/sui/core/request.go index 5de658502..c1d3b5886 100644 --- a/sui/core/request.go +++ b/sui/core/request.go @@ -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) { diff --git a/sui/core/types.go b/sui/core/types.go index b5fd56ccb..480f47a9e 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -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"`