Skip to content

Commit

Permalink
Merge pull request #33 from LightAPIs/dynamic
Browse files Browse the repository at this point in the history
feat: Support for dynamic URL parameters
  • Loading branch information
soulteary authored Dec 6, 2023
2 parents f415dfe + 3170991 commit cb8bab2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
12 changes: 9 additions & 3 deletions internal/pages/home/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions internal/pages/home/bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions internal/pages/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 := ""
Expand Down
62 changes: 62 additions & 0 deletions internal/state/url.go
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
}

0 comments on commit cb8bab2

Please sign in to comment.