--- GetFavicon --- GetFavicon takes a URL string and returns a base64-encoded src URL for an tag. If the favicon is already in cache and “fresh,” it returns it immediately. Otherwise it kicks off a background fetch (if one isn’t already in progress) and returns whatever is in the cache (which
(urlStr string)
| 56 | // Otherwise it kicks off a background fetch (if one isn’t already in progress) |
| 57 | // and returns whatever is in the cache (which may be empty). |
| 58 | func GetFavicon(urlStr string) string { |
| 59 | // Parse the URL and extract the domain. |
| 60 | parsedURL, err := url.Parse(urlStr) |
| 61 | if err != nil { |
| 62 | log.Printf("GetFavicon: invalid URL %q: %v", urlStr, err) |
| 63 | return "" |
| 64 | } |
| 65 | domain := parsedURL.Hostname() |
| 66 | if domain == "" { |
| 67 | log.Printf("GetFavicon: no hostname found in URL %q", urlStr) |
| 68 | return "" |
| 69 | } |
| 70 | |
| 71 | // Try to get from our cache. |
| 72 | item, found := GetFromCache(domain) |
| 73 | if found { |
| 74 | // If the cached entry is not stale, return it. |
| 75 | if time.Since(item.LastFetched) < cacheDuration { |
| 76 | return item.Data |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Either the item was not found or it’s stale: |
| 81 | // Launch an async fetch if one isn’t already running for this domain. |
| 82 | triggerAsyncFetch(domain) |
| 83 | |
| 84 | // Return the cached value (even if stale or empty). |
| 85 | return item.Data |
| 86 | } |
| 87 | |
| 88 | // triggerAsyncFetch starts a goroutine to update the favicon cache |
| 89 | // for the given domain if one isn’t already in progress. |
no test coverage detected