server side favicon retrieving and caching

This commit is contained in:
yusing
2025-01-12 10:30:37 +08:00
parent 0ce7f29976
commit c7c6a097f0
9 changed files with 343 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
package homepage
import "strings"
type (
//nolint:recvcheck
Config map[string]Category
@@ -8,7 +10,7 @@ type (
Item struct {
Show bool `json:"show"`
Name string `json:"name"` // display name
Icon string `json:"icon"`
Icon *IconURL `json:"icon"`
URL string `json:"url"` // alias + domain
Category string `json:"category"`
Description string `json:"description" aliases:"desc"`
@@ -22,7 +24,7 @@ type (
func (item *Item) IsEmpty() bool {
return item == nil || (item.Name == "" &&
item.Icon == "" &&
item.Icon == nil &&
item.URL == "" &&
item.Category == "" &&
item.Description == "" &&
@@ -37,6 +39,13 @@ func (c *Config) Clear() {
*c = make(Config)
}
var cleanName = strings.NewReplacer(
" ", "-",
"_", "-",
"(", "",
")", "",
)
func (c Config) Add(item *Item) {
if c[item.Category] == nil {
c[item.Category] = make(Category, 0)

View File

@@ -0,0 +1,46 @@
package homepage
import (
"strings"
E "github.com/yusing/go-proxy/internal/error"
)
type IconURL struct {
Value string `json:"value"`
IsRelative bool `json:"is_relative"`
}
const DashboardIconBaseURL = "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/"
var ErrInvalidIconURL = E.New("invalid icon url")
// Parse implements strutils.Parser.
func (u *IconURL) Parse(v string) error {
if v == "" {
return ErrInvalidIconURL
}
slashIndex := strings.Index(v, "/")
if slashIndex == -1 {
return ErrInvalidIconURL
}
beforeSlash := v[:slashIndex]
switch beforeSlash {
case "http:", "https:":
u.Value = v
return nil
case "@target":
u.Value = v[slashIndex:]
u.IsRelative = true
return nil
case "png", "svg": // walkXCode Icons
u.Value = DashboardIconBaseURL + v
return nil
default:
return ErrInvalidIconURL
}
}
func (u *IconURL) String() string {
return u.Value
}