Skip to content

Commit

Permalink
Merge pull request #528 from trheyi/main
Browse files Browse the repository at this point in the history
[fix] Refactor URL handling in NewRequestContext and parseArgs functions
  • Loading branch information
trheyi authored Dec 16, 2023
2 parents 0f828ff + bd798b0 commit 8db99c3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
19 changes: 16 additions & 3 deletions sui/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ func NewRequestContext(c *gin.Context) (*Request, int, error) {
return nil, 500, err
}

schema := c.Request.URL.Scheme
if schema == "" {
schema = "http"
}

domain := c.Request.URL.Hostname()
if domain == "" {
domain = strings.Split(c.Request.Host, ":")[0]
}

path := strings.TrimSuffix(c.Request.URL.Path, ".sui")

return &Request{
File: file,
Request: &core.Request{
Expand All @@ -43,10 +55,11 @@ func NewRequestContext(c *gin.Context) (*Request, int, error) {
Headers: url.Values(c.Request.Header),
Params: params,
URL: core.ReqeustURL{
URL: fmt.Sprintf("%s://%s%s", schema, c.Request.Host, path),
Host: c.Request.Host,
Path: c.Request.URL.Path,
Domain: c.Request.URL.Hostname(),
Scheme: c.Request.URL.Scheme,
Path: path,
Domain: domain,
Scheme: schema,
},
},
}, 200, nil
Expand Down
16 changes: 16 additions & 0 deletions sui/core/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func (r *Request) parseArgs(args []interface{}) ([]interface{}, error) {
"header": r.Headers,
"theme": r.Theme,
"locale": r.Locale,
"url": r.URL.Map(),
}).Dot()

for i, arg := range args {
Expand All @@ -232,6 +233,7 @@ func (r *Request) parseArgs(args []interface{}) ([]interface{}, error) {
args[i] = key
if data.Has(key) {
v := data.Get(key)
args[i] = v
if strings.HasPrefix(key, "query.") || strings.HasPrefix(key, "header.") {
switch arg := v.(type) {
case []interface{}:
Expand All @@ -246,26 +248,40 @@ func (r *Request) parseArgs(args []interface{}) ([]interface{}, error) {
}
}
}
break

case []interface{}:
res, err := r.parseArgs(v)
if err != nil {
return nil, err
}
args[i] = res
break

case map[string]interface{}:
res, err := r.parseArgs([]interface{}{v})
if err != nil {
return nil, err
}
args[i] = res[0]
break
}
}

return args, nil
}

// Map URL to map
func (url ReqeustURL) Map() Data {
return map[string]interface{}{
"url": url.URL,
"scheme": url.Scheme,
"domain": url.Domain,
"host": url.Host,
"path": url.Path,
}
}

// SetCache set the cache
func SetCache(file string, html string, data string, global string) *Cache {
Caches[file] = &Cache{
Expand Down
1 change: 1 addition & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type ReqeustURL struct {
Domain string `json:"domain,omitempty"`
Path string `json:"path,omitempty"`
Scheme string `json:"scheme,omitempty"`
URL string `json:"url,omitempty"`
}

// PageConfig is the struct for the page config
Expand Down

0 comments on commit 8db99c3

Please sign in to comment.