GoDoxy/internal/route/routes/query.go
Yuzerion 04f806239d
refactor: organize code (#90)
* fix: improved sync.Pool handling

* refactor: ping if-flow and remove timeout

* refactor: enhance favicon fetching with context support and improve cache management

- Added context support to favicon fetching functions to handle timeouts and cancellations.
- Improved cache entry structure to include content type and utilize atomic values for last access time.
- Implemented maximum cache size and entry limits to optimize memory usage.
- Updated error handling for HTTP requests and refined the logic for managing redirects.

* fix: log formatting

* feat(pool): add checkExists method to debug build to detect unexpected behavior

* chore: cont. 0866feb

* refactor: unify route handling by consolidating route query methods with Pool

- Replaced direct calls to routequery with a new routes package for better organization and maintainability.
- Updated various components to utilize the new routes methods for fetching health information, homepage configurations, and route aliases.
- Enhanced the overall structure of the routing logic to improve clarity and reduce redundancy.

* chore: uncomment icon list cache code

* refactor: update task management code

- Rename needFinish to waitFinish
- Fixed some tasks not being waited they should be
- Adjusted mutex usage in the directory watcher to utilize read-write locks for improved concurrency management.

* refactor: enhance idlewatcher logging and exit handling

* fix(server): ensure HTTP handler is set only if initialized

* refactor(accesslog): replace JSON log entry struct with zerolog for improved logging efficiency, updated test

* refactor: remove test run code

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-04-17 15:30:05 +08:00

113 lines
2.5 KiB
Go

package routes
import (
"time"
"github.com/yusing/go-proxy/internal/homepage"
route "github.com/yusing/go-proxy/internal/route/types"
"github.com/yusing/go-proxy/internal/watcher/health"
)
func getHealthInfo(r Route) map[string]string {
mon := r.HealthMonitor()
if mon == nil {
return map[string]string{
"status": "unknown",
"uptime": "n/a",
"latency": "n/a",
}
}
return map[string]string{
"status": mon.Status().String(),
"uptime": mon.Uptime().Round(time.Second).String(),
"latency": mon.Latency().Round(time.Microsecond).String(),
}
}
type HealthInfoRaw struct {
Status health.Status `json:"status,string"`
Latency time.Duration `json:"latency"`
}
func getHealthInfoRaw(r Route) *HealthInfoRaw {
mon := r.HealthMonitor()
if mon == nil {
return &HealthInfoRaw{
Status: health.StatusUnknown,
Latency: time.Duration(0),
}
}
return &HealthInfoRaw{
Status: mon.Status(),
Latency: mon.Latency(),
}
}
func HealthMap() map[string]map[string]string {
healthMap := make(map[string]map[string]string, NumRoutes())
for alias, r := range Iter {
healthMap[alias] = getHealthInfo(r)
}
return healthMap
}
func HealthInfo() map[string]*HealthInfoRaw {
healthMap := make(map[string]*HealthInfoRaw, NumRoutes())
for alias, r := range Iter {
healthMap[alias] = getHealthInfoRaw(r)
}
return healthMap
}
func HomepageCategories() []string {
check := make(map[string]struct{})
categories := make([]string, 0)
for _, r := range HTTP.Iter {
item := r.HomepageConfig()
if item == nil || item.Category == "" {
continue
}
if _, ok := check[item.Category]; ok {
continue
}
check[item.Category] = struct{}{}
categories = append(categories, item.Category)
}
return categories
}
func HomepageConfig(categoryFilter, providerFilter string) homepage.Homepage {
hp := make(homepage.Homepage)
for _, r := range HTTP.Iter {
if providerFilter != "" && r.ProviderName() != providerFilter {
continue
}
item := r.HomepageItem()
if categoryFilter != "" && item.Category != categoryFilter {
continue
}
hp.Add(item)
}
return hp
}
func ByAlias(typeFilter ...route.RouteType) map[string]Route {
rts := make(map[string]Route)
if len(typeFilter) == 0 || typeFilter[0] == "" {
typeFilter = []route.RouteType{route.RouteTypeHTTP, route.RouteTypeStream}
}
for _, t := range typeFilter {
switch t {
case route.RouteTypeHTTP:
for alias, r := range HTTP.Iter {
rts[alias] = r
}
case route.RouteTypeStream:
for alias, r := range Stream.Iter {
rts[alias] = r
}
}
}
return rts
}