mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
improve initialization flow
This commit is contained in:
parent
9731d28ec3
commit
01432fa778
9 changed files with 82 additions and 81 deletions
22
cmd/main.go
22
cmd/main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal"
|
"github.com/yusing/go-proxy/internal"
|
||||||
"github.com/yusing/go-proxy/internal/api/v1/auth"
|
"github.com/yusing/go-proxy/internal/api/v1/auth"
|
||||||
|
@ -22,6 +23,18 @@ import (
|
||||||
|
|
||||||
var rawLogger = log.New(os.Stdout, "", 0)
|
var rawLogger = log.New(os.Stdout, "", 0)
|
||||||
|
|
||||||
|
func parallel(fns ...func()) {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for _, fn := range fns {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
fn()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
initProfiling()
|
initProfiling()
|
||||||
args := pkg.GetArgs(common.MainServerCommandValidator{})
|
args := pkg.GetArgs(common.MainServerCommandValidator{})
|
||||||
|
@ -64,6 +77,11 @@ func main() {
|
||||||
if args.Command == common.CommandStart {
|
if args.Command == common.CommandStart {
|
||||||
logging.Info().Msgf("GoDoxy version %s", pkg.GetVersion())
|
logging.Info().Msgf("GoDoxy version %s", pkg.GetVersion())
|
||||||
logging.Trace().Msg("trace enabled")
|
logging.Trace().Msg("trace enabled")
|
||||||
|
parallel(
|
||||||
|
internal.InitIconListCache,
|
||||||
|
homepage.InitOverridesConfig,
|
||||||
|
favicon.InitIconCache,
|
||||||
|
)
|
||||||
// logging.AddHook(notif.GetDispatcher())
|
// logging.AddHook(notif.GetDispatcher())
|
||||||
} else {
|
} else {
|
||||||
logging.DiscardLogger()
|
logging.DiscardLogger()
|
||||||
|
@ -109,10 +127,6 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go internal.InitIconListCache()
|
|
||||||
go homepage.InitOverridesConfig()
|
|
||||||
go favicon.InitIconCache()
|
|
||||||
|
|
||||||
cfg.Start(&config.StartServersOptions{
|
cfg.Start(&config.StartServersOptions{
|
||||||
Proxy: true,
|
Proxy: true,
|
||||||
})
|
})
|
||||||
|
|
|
@ -36,7 +36,7 @@ func InitIconCache() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Error().Err(err).Msg("failed to load icon cache")
|
logging.Error().Err(err).Msg("failed to load icon cache")
|
||||||
} else {
|
} else {
|
||||||
logging.Info().Msgf("icon cache loaded (%d icons)", len(iconCache))
|
logging.Info().Int("count", len(iconCache)).Msg("icon cache loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -56,7 +56,7 @@ func List(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
case ListMatchDomains:
|
case ListMatchDomains:
|
||||||
gphttp.RespondJSON(w, r, cfg.Value().MatchDomains)
|
gphttp.RespondJSON(w, r, cfg.Value().MatchDomains)
|
||||||
case ListHomepageConfig:
|
case ListHomepageConfig:
|
||||||
gphttp.RespondJSON(w, r, routequery.HomepageConfig(cfg.Value().Homepage.UseDefaultCategories, r.FormValue("category"), r.FormValue("provider")))
|
gphttp.RespondJSON(w, r, routequery.HomepageConfig(r.FormValue("category"), r.FormValue("provider")))
|
||||||
case ListRouteProviders:
|
case ListRouteProviders:
|
||||||
gphttp.RespondJSON(w, r, cfg.RouteProviderList())
|
gphttp.RespondJSON(w, r, cfg.RouteProviderList())
|
||||||
case ListHomepageCategories:
|
case ListHomepageCategories:
|
||||||
|
|
|
@ -24,8 +24,6 @@ type (
|
||||||
*ItemConfig
|
*ItemConfig
|
||||||
|
|
||||||
Alias string `json:"alias"` // proxy alias
|
Alias string `json:"alias"` // proxy alias
|
||||||
SourceType string `json:"source_type"`
|
|
||||||
AltURL string `json:"alt_url"` // original proxy target
|
|
||||||
Provider string `json:"provider"`
|
Provider string `json:"provider"`
|
||||||
|
|
||||||
IsUnset bool `json:"-"`
|
IsUnset bool `json:"-"`
|
||||||
|
|
|
@ -32,7 +32,9 @@ func InitOverridesConfig() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Error().Err(err).Msg("failed to load homepage overrides config")
|
logging.Error().Err(err).Msg("failed to load homepage overrides config")
|
||||||
} else {
|
} else {
|
||||||
logging.Info().Msgf("homepage overrides config loaded, %d items", len(overrideConfigInstance.ItemOverrides))
|
logging.Info().
|
||||||
|
Int("count", len(overrideConfigInstance.ItemOverrides)).
|
||||||
|
Msg("homepage overrides config loaded")
|
||||||
}
|
}
|
||||||
task.OnProgramExit("save_homepage_json_config", func() {
|
task.OnProgramExit("save_homepage_json_config", func() {
|
||||||
if len(overrideConfigInstance.ItemOverrides) == 0 {
|
if len(overrideConfigInstance.ItemOverrides) == 0 {
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitHubContents struct { //! keep this, may reuse in future
|
type GitHubContents struct { //! keep this, may reuse in future
|
||||||
|
@ -66,10 +65,10 @@ func InitIconListCache() {
|
||||||
logging.Error().Err(err).Msg("failed to load icon list cache config")
|
logging.Error().Err(err).Msg("failed to load icon list cache config")
|
||||||
} else if stats, err := os.Stat(common.IconListCachePath); err == nil {
|
} else if stats, err := os.Stat(common.IconListCachePath); err == nil {
|
||||||
lastUpdate = stats.ModTime()
|
lastUpdate = stats.ModTime()
|
||||||
logging.Info().Msgf("icon list cache loaded (%d icons, %d display names), last updated at %s",
|
logging.Info().
|
||||||
len(iconsCache.IconList),
|
Int("icons", len(iconsCache.IconList)).
|
||||||
len(iconsCache.DisplayNames),
|
Int("display_names", len(iconsCache.DisplayNames)).
|
||||||
strutils.FormatTime(lastUpdate))
|
Msg("icon list cache loaded")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,12 +85,15 @@ func ListAvailableIcons() (*Cache, error) {
|
||||||
iconsCahceMu.Lock()
|
iconsCahceMu.Lock()
|
||||||
defer iconsCahceMu.Unlock()
|
defer iconsCahceMu.Unlock()
|
||||||
|
|
||||||
|
logging.Info().Msg("updating icon data")
|
||||||
icons, err := fetchIconData()
|
icons, err := fetchIconData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
logging.Info().
|
||||||
logging.Info().Msg("icons list updated")
|
Int("icons", len(icons.IconList)).
|
||||||
|
Int("display_names", len(icons.DisplayNames)).
|
||||||
|
Msg("icons list updated")
|
||||||
|
|
||||||
iconsCache = icons
|
iconsCache = icons
|
||||||
lastUpdate = time.Now()
|
lastUpdate = time.Now()
|
||||||
|
|
|
@ -87,6 +87,7 @@ func (p *Provider) MarshalText() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) startRoute(parent task.Parent, r *route.Route) gperr.Error {
|
func (p *Provider) startRoute(parent task.Parent, r *route.Route) gperr.Error {
|
||||||
|
r.FinalizeHomepageConfig()
|
||||||
err := r.Start(parent)
|
err := r.Start(parent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
delete(p.routes, r.Alias)
|
delete(p.routes, r.Alias)
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
|
|
||||||
dockertypes "github.com/docker/docker/api/types"
|
dockertypes "github.com/docker/docker/api/types"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
"github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
||||||
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
|
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
|
||||||
"github.com/yusing/go-proxy/internal/route/rules"
|
"github.com/yusing/go-proxy/internal/route/rules"
|
||||||
|
@ -356,13 +357,27 @@ func (r *Route) Finalize() {
|
||||||
cont.StopMethod = common.StopMethodDefault
|
cont.StopMethod = common.StopMethodDefault
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if r.Homepage.IsEmpty() {
|
func (r *Route) FinalizeHomepageConfig() {
|
||||||
r.Homepage = homepage.NewItem(r.Alias)
|
if r.Alias == "" {
|
||||||
|
panic("alias is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Homepage.Name == "" {
|
isDocker := r.Container != nil
|
||||||
|
|
||||||
|
hp := r.Homepage
|
||||||
|
if hp.IsEmpty() {
|
||||||
|
hp = homepage.NewItem(r.Alias)
|
||||||
|
}
|
||||||
|
hp = hp.GetOverride()
|
||||||
|
hp.Alias = r.Alias
|
||||||
|
hp.Provider = r.Provider
|
||||||
|
|
||||||
|
r.Homepage = hp
|
||||||
|
|
||||||
var key string
|
var key string
|
||||||
|
if hp.Name == "" {
|
||||||
if r.Container != nil {
|
if r.Container != nil {
|
||||||
key = r.Container.ImageName
|
key = r.Container.ImageName
|
||||||
} else {
|
} else {
|
||||||
|
@ -370,16 +385,40 @@ func (r *Route) Finalize() {
|
||||||
}
|
}
|
||||||
displayName, ok := internal.GetDisplayName(key)
|
displayName, ok := internal.GetDisplayName(key)
|
||||||
if ok {
|
if ok {
|
||||||
r.Homepage.Name = displayName
|
hp.Name = displayName
|
||||||
} else {
|
} else {
|
||||||
r.Homepage.Name = strutils.Title(
|
hp.Name = strutils.Title(
|
||||||
strings.ReplaceAll(
|
strings.ReplaceAll(
|
||||||
strings.ReplaceAll(r.Alias, "-", " "),
|
strings.ReplaceAll(key, "-", " "),
|
||||||
"_", " ",
|
"_", " ",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if hp.Category == "" {
|
||||||
|
if config.GetInstance().Value().Homepage.UseDefaultCategories {
|
||||||
|
if isDocker {
|
||||||
|
key = r.Container.ImageName
|
||||||
|
} else {
|
||||||
|
key = strings.ToLower(r.Alias)
|
||||||
|
}
|
||||||
|
if category, ok := homepage.PredefinedCategories[key]; ok {
|
||||||
|
hp.Category = category
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hp.Category == "" {
|
||||||
|
switch {
|
||||||
|
case r.UseLoadBalance():
|
||||||
|
hp.Category = "Load-balanced"
|
||||||
|
case isDocker:
|
||||||
|
hp.Category = "Docker"
|
||||||
|
default:
|
||||||
|
hp.Category = "Others"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func lowestPort(ports map[int]dockertypes.Port) (res int) {
|
func lowestPort(ports map[int]dockertypes.Port) (res int) {
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
package routequery
|
package routequery
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/homepage"
|
"github.com/yusing/go-proxy/internal/homepage"
|
||||||
provider "github.com/yusing/go-proxy/internal/route/provider/types"
|
|
||||||
"github.com/yusing/go-proxy/internal/route/routes"
|
"github.com/yusing/go-proxy/internal/route/routes"
|
||||||
route "github.com/yusing/go-proxy/internal/route/types"
|
route "github.com/yusing/go-proxy/internal/route/types"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||||
|
@ -79,70 +77,17 @@ func HomepageCategories() []string {
|
||||||
return categories
|
return categories
|
||||||
}
|
}
|
||||||
|
|
||||||
func HomepageConfig(useDefaultCategories bool, categoryFilter, providerFilter string) homepage.Categories {
|
func HomepageConfig(categoryFilter, providerFilter string) homepage.Categories {
|
||||||
hpCfg := homepage.NewHomePageConfig()
|
hpCfg := homepage.NewHomePageConfig()
|
||||||
|
|
||||||
routes.GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
|
routes.GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
|
||||||
item := r.HomepageConfig()
|
item := r.HomepageConfig()
|
||||||
|
|
||||||
if override := item.GetOverride(); override != item {
|
|
||||||
if providerFilter != "" && override.Provider != providerFilter ||
|
|
||||||
categoryFilter != "" && override.Category != categoryFilter {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hpCfg.Add(override)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
item.Alias = alias
|
|
||||||
item.Provider = r.ProviderName()
|
|
||||||
|
|
||||||
if providerFilter != "" && item.Provider != providerFilter {
|
if providerFilter != "" && item.Provider != providerFilter {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if useDefaultCategories {
|
|
||||||
container := r.ContainerInfo()
|
|
||||||
if container != nil && item.Category == "" {
|
|
||||||
if category, ok := homepage.PredefinedCategories[container.ImageName]; ok {
|
|
||||||
item.Category = category
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if item.Category == "" {
|
|
||||||
if category, ok := homepage.PredefinedCategories[strings.ToLower(alias)]; ok {
|
|
||||||
item.Category = category
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if categoryFilter != "" && item.Category != categoryFilter {
|
if categoryFilter != "" && item.Category != categoryFilter {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
|
||||||
case r.IsDocker():
|
|
||||||
if item.Category == "" {
|
|
||||||
item.Category = "Docker"
|
|
||||||
}
|
|
||||||
if r.IsAgent() {
|
|
||||||
item.SourceType = string(provider.ProviderTypeAgent)
|
|
||||||
} else {
|
|
||||||
item.SourceType = string(provider.ProviderTypeDocker)
|
|
||||||
}
|
|
||||||
case r.UseLoadBalance():
|
|
||||||
if item.Category == "" {
|
|
||||||
item.Category = "Load-balanced"
|
|
||||||
}
|
|
||||||
item.SourceType = "loadbalancer"
|
|
||||||
default:
|
|
||||||
if item.Category == "" {
|
|
||||||
item.Category = "Others"
|
|
||||||
}
|
|
||||||
item.SourceType = string(provider.ProviderTypeFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
item.AltURL = r.TargetURL().String()
|
|
||||||
hpCfg.Add(item)
|
hpCfg.Add(item)
|
||||||
})
|
})
|
||||||
return hpCfg
|
return hpCfg
|
||||||
|
|
Loading…
Add table
Reference in a new issue