mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-23 20:54:02 +02:00
refactor: update homepage item handling and improve JSON marshaling
This commit is contained in:
parent
a7da8ffb90
commit
fbb07011f1
4 changed files with 52 additions and 56 deletions
|
@ -1,13 +1,16 @@
|
||||||
package homepage
|
package homepage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
//nolint:recvcheck
|
Homepage map[string]Category
|
||||||
Categories map[string]Category
|
Category []*Item
|
||||||
Category []*Item
|
|
||||||
|
|
||||||
ItemConfig struct {
|
ItemConfig struct {
|
||||||
Show bool `json:"show"`
|
Show bool `json:"show"`
|
||||||
|
@ -17,18 +20,13 @@ type (
|
||||||
Description string `json:"description" aliases:"desc"`
|
Description string `json:"description" aliases:"desc"`
|
||||||
SortOrder int `json:"sort_order"`
|
SortOrder int `json:"sort_order"`
|
||||||
WidgetConfig map[string]any `json:"widget_config" aliases:"widget"`
|
WidgetConfig map[string]any `json:"widget_config" aliases:"widget"`
|
||||||
URL string `json:"url"` // alias + domain
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Item struct {
|
Item struct {
|
||||||
*ItemConfig
|
*ItemConfig
|
||||||
|
|
||||||
Alias string `json:"alias"` // proxy alias
|
Alias string
|
||||||
SourceType string `json:"source_type"`
|
Provider string
|
||||||
AltURL string `json:"alt_url"` // original proxy target
|
|
||||||
Provider string `json:"provider"`
|
|
||||||
|
|
||||||
IsUnset bool `json:"-"`
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,33 +38,38 @@ func init() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewItem(alias string) *Item {
|
func (cfg *ItemConfig) GetOverride(alias string) *ItemConfig {
|
||||||
return &Item{
|
return overrideConfigInstance.GetOverride(alias, cfg)
|
||||||
ItemConfig: &ItemConfig{
|
}
|
||||||
Show: true,
|
|
||||||
},
|
func (item *Item) MarshalJSON() ([]byte, error) {
|
||||||
Alias: alias,
|
var url *string
|
||||||
IsUnset: true,
|
if !strings.ContainsRune(item.Alias, '.') {
|
||||||
|
godoxyCfg := config.GetInstance().Value()
|
||||||
|
// use first domain as base domain
|
||||||
|
domains := godoxyCfg.MatchDomains
|
||||||
|
if len(domains) > 0 {
|
||||||
|
url = new(string)
|
||||||
|
*url = item.Alias + domains[0]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
url = &item.Alias
|
||||||
}
|
}
|
||||||
|
return json.Marshal(map[string]any{
|
||||||
|
"show": item.Show,
|
||||||
|
"alias": item.Alias,
|
||||||
|
"provider": item.Provider,
|
||||||
|
"url": url,
|
||||||
|
"name": item.Name,
|
||||||
|
"icon": item.Icon,
|
||||||
|
"category": item.Category,
|
||||||
|
"description": item.Description,
|
||||||
|
"sort_order": item.SortOrder,
|
||||||
|
"widget_config": item.WidgetConfig,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHomePageConfig() Categories {
|
func (c Homepage) Add(item *Item) {
|
||||||
return Categories(make(map[string]Category))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (item *Item) IsEmpty() bool {
|
|
||||||
return item == nil || item.IsUnset || item.ItemConfig == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (item *Item) GetOverride() *Item {
|
|
||||||
return overrideConfigInstance.GetOverride(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Categories) Clear() {
|
|
||||||
*c = make(Categories)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Categories) Add(item *Item) {
|
|
||||||
if c[item.Category] == nil {
|
if c[item.Category] == nil {
|
||||||
c[item.Category] = make(Category, 0)
|
c[item.Category] = make(Category, 0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ func TestOverrideItem(t *testing.T) {
|
||||||
Category: "App",
|
Category: "App",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
override := &ItemConfig{
|
want := &ItemConfig{
|
||||||
Show: true,
|
Show: true,
|
||||||
Name: "Bar",
|
Name: "Bar",
|
||||||
Category: "Test",
|
Category: "Test",
|
||||||
|
@ -30,7 +30,7 @@ func TestOverrideItem(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
overrides := GetOverrideConfig()
|
overrides := GetOverrideConfig()
|
||||||
overrides.OverrideItem(a.Alias, override)
|
overrides.OverrideItem(a.Alias, want)
|
||||||
overridden := a.GetOverride()
|
got := a.GetOverride(a.Alias)
|
||||||
ExpectDeepEqual(t, overridden.ItemConfig, override)
|
ExpectDeepEqual(t, got, want)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal"
|
"github.com/yusing/go-proxy/internal"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -31,7 +31,7 @@ const (
|
||||||
IconSourceSelfhSt
|
IconSourceSelfhSt
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrInvalidIconURL = E.New("invalid icon url")
|
var ErrInvalidIconURL = gperr.New("invalid icon url")
|
||||||
|
|
||||||
func NewSelfhStIconURL(reference, format string) *IconURL {
|
func NewSelfhStIconURL(reference, format string) *IconURL {
|
||||||
return &IconURL{
|
return &IconURL{
|
||||||
|
|
|
@ -64,24 +64,17 @@ func (c *OverrideConfig) OverrideItems(items map[string]*ItemConfig) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *OverrideConfig) GetOverride(item *Item) *Item {
|
func (c *OverrideConfig) GetOverride(alias string, item *ItemConfig) *ItemConfig {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
itemOverride, hasOverride := c.ItemOverrides[item.Alias]
|
itemOverride, hasOverride := c.ItemOverrides[alias]
|
||||||
if hasOverride {
|
if hasOverride {
|
||||||
clone := *item
|
return itemOverride
|
||||||
clone.ItemConfig = itemOverride
|
|
||||||
clone.IsUnset = false
|
|
||||||
item = &clone
|
|
||||||
}
|
}
|
||||||
if show, ok := c.ItemVisibility[item.Alias]; ok {
|
if show, ok := c.ItemVisibility[alias]; ok {
|
||||||
if !hasOverride {
|
clone := *item
|
||||||
clone := *item
|
clone.Show = show
|
||||||
clone.Show = show
|
return &clone
|
||||||
item = &clone
|
|
||||||
} else {
|
|
||||||
item.Show = show
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
@ -92,7 +85,7 @@ func (c *OverrideConfig) SetCategoryOrder(key string, value int) {
|
||||||
c.CategoryOrder[key] = value
|
c.CategoryOrder[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *OverrideConfig) UnhideItems(keys ...string) {
|
func (c *OverrideConfig) UnhideItems(keys []string) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
|
@ -100,7 +93,7 @@ func (c *OverrideConfig) UnhideItems(keys ...string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *OverrideConfig) HideItems(keys ...string) {
|
func (c *OverrideConfig) HideItems(keys []string) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
|
|
Loading…
Add table
Reference in a new issue