-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from LightAPIs/dynamic
feat: Support for dynamic URL parameters
- Loading branch information
Showing
4 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package state | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type _Dynamic_URL struct { | ||
Host string | ||
Hostname string | ||
Href string | ||
Origin string | ||
Pathname string | ||
Port string | ||
Protocol string | ||
} | ||
|
||
var RequestURL _Dynamic_URL | ||
|
||
func getPort(host string, defaultPort string) (hostname string, port string) { | ||
hostname = host | ||
port = defaultPort | ||
reg, _ := regexp.Compile(`([\w+\.-]+):(\d+)$`) | ||
portMatch := reg.FindStringSubmatch(host) | ||
if portMatch != nil { | ||
hostname = portMatch[1] | ||
port = portMatch[2] | ||
} | ||
return | ||
} | ||
|
||
func ParseRequestURL(r *http.Request) { | ||
scheme := "http:" | ||
defaultPort := "80" | ||
if r.TLS != nil { | ||
scheme = "https:" | ||
defaultPort = "443" | ||
} | ||
host := r.Host | ||
hostname, port := getPort(host, defaultPort) | ||
|
||
RequestURL.Host = host | ||
RequestURL.Hostname = hostname | ||
RequestURL.Href = strings.Join([]string{scheme, "//", host, r.RequestURI}, "") | ||
RequestURL.Origin = strings.Join([]string{scheme, "//", host}, "") | ||
RequestURL.Pathname = r.URL.Path | ||
RequestURL.Port = port | ||
RequestURL.Protocol = scheme | ||
} | ||
|
||
func ParseDynamicUrl(url string) string { | ||
result := url | ||
result = strings.ReplaceAll(result, "{host}", RequestURL.Host) | ||
result = strings.ReplaceAll(result, "{hostname}", RequestURL.Hostname) | ||
result = strings.ReplaceAll(result, "{href}", RequestURL.Href) | ||
result = strings.ReplaceAll(result, "{origin}", RequestURL.Origin) | ||
result = strings.ReplaceAll(result, "{pathname}", RequestURL.Pathname) | ||
result = strings.ReplaceAll(result, "{port}", RequestURL.Port) | ||
result = strings.ReplaceAll(result, "{protocol}", RequestURL.Protocol) | ||
return result | ||
} |