From 317099142e5f5273b9315318942285d4711095c1 Mon Sep 17 00:00:00 2001 From: Light <47911535+LightAPIs@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:48:27 +0800 Subject: [PATCH] feat: Support for dynamic URL parameters --- internal/pages/home/application.go | 12 ++++-- internal/pages/home/bookmark.go | 10 ++++- internal/pages/home/home.go | 3 ++ internal/state/url.go | 62 ++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 internal/state/url.go diff --git a/internal/pages/home/application.go b/internal/pages/home/application.go index 9e4765e..b1da482 100644 --- a/internal/pages/home/application.go +++ b/internal/pages/home/application.go @@ -13,19 +13,25 @@ import ( func GenerateApplicationsTemplate(filter string) template.HTML { options := FlareData.GetAllSettingsOptions() appsData := FlareData.LoadFavoriteBookmarks() + tpl := "" + + var parseApps []FlareModel.Bookmark + for _, app := range appsData.Items { + app.URL = FlareState.ParseDynamicUrl(app.URL) + parseApps = append(parseApps, app) + } var apps []FlareModel.Bookmark - tpl := "" if filter != "" { filterLower := strings.ToLower(filter) - for _, bookmark := range appsData.Items { + for _, bookmark := range parseApps { if strings.Contains(strings.ToLower(bookmark.Name), filterLower) || strings.Contains(strings.ToLower(bookmark.URL), filterLower) || strings.Contains(strings.ToLower(bookmark.Desc), filterLower) { apps = append(apps, bookmark) } } } else { - apps = appsData.Items + apps = parseApps } for _, app := range apps { diff --git a/internal/pages/home/bookmark.go b/internal/pages/home/bookmark.go index c8c9b15..6ce955b 100644 --- a/internal/pages/home/bookmark.go +++ b/internal/pages/home/bookmark.go @@ -15,18 +15,24 @@ func GenerateBookmarkTemplate(filter string) template.HTML { bookmarksData := FlareData.LoadNormalBookmarks() tpl := "" + var parseBookmarks []FlareModel.Bookmark + for _, bookmark := range bookmarksData.Items { + bookmark.URL = FlareState.ParseDynamicUrl(bookmark.URL) + parseBookmarks = append(parseBookmarks, bookmark) + } + var bookmarks []FlareModel.Bookmark if filter != "" { filterLower := strings.ToLower(filter) - for _, bookmark := range bookmarksData.Items { + for _, bookmark := range parseBookmarks { if strings.Contains(strings.ToLower(bookmark.Name), filterLower) || strings.Contains(strings.ToLower(bookmark.URL), filterLower) { bookmarks = append(bookmarks, bookmark) } } } else { - bookmarks = bookmarksData.Items + bookmarks = parseBookmarks } if len(bookmarksData.Categories) > 0 { diff --git a/internal/pages/home/home.go b/internal/pages/home/home.go index 8795ab5..60f24d9 100644 --- a/internal/pages/home/home.go +++ b/internal/pages/home/home.go @@ -226,6 +226,7 @@ func getGreeting(greeting string) string { func pageBookmark(c *gin.Context) { options := FlareData.GetAllSettingsOptions() + FlareState.ParseRequestURL(c.Request) c.HTML( http.StatusOK, @@ -257,6 +258,7 @@ func pageBookmark(c *gin.Context) { func pageApplication(c *gin.Context) { options := FlareData.GetAllSettingsOptions() + FlareState.ParseRequestURL(c.Request) c.HTML( http.StatusOK, @@ -287,6 +289,7 @@ func pageApplication(c *gin.Context) { func render(c *gin.Context, filter string) { options := FlareData.GetAllSettingsOptions() + FlareState.ParseRequestURL(c.Request) hasKeyword := false searchKeyword := "" diff --git a/internal/state/url.go b/internal/state/url.go new file mode 100644 index 0000000..9cab0b4 --- /dev/null +++ b/internal/state/url.go @@ -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 +}