mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
feat(api): refined list route api
This commit is contained in:
parent
b30c0d7dc0
commit
55bbcae911
8 changed files with 53 additions and 46 deletions
|
@ -10,12 +10,8 @@ import (
|
||||||
|
|
||||||
func ListRouteHandler(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
func ListRouteHandler(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
which := r.PathValue("which")
|
which := r.PathValue("which")
|
||||||
if which == "" || which == "all" {
|
route, ok := routes.Get(which)
|
||||||
gphttp.RespondJSON(w, r, routes.ByAlias())
|
if ok {
|
||||||
return
|
|
||||||
}
|
|
||||||
routesMap := routes.ByAlias()
|
|
||||||
if route, ok := routesMap[which]; ok {
|
|
||||||
gphttp.RespondJSON(w, r, route)
|
gphttp.RespondJSON(w, r, route)
|
||||||
} else {
|
} else {
|
||||||
gphttp.RespondJSON(w, r, nil)
|
gphttp.RespondJSON(w, r, nil)
|
||||||
|
|
|
@ -2,11 +2,22 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/coder/websocket/wsjson"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
|
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
||||||
|
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListRouteProvidersHandler(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
func ListRouteProvidersHandler(cfgInstance config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
gphttp.RespondJSON(w, r, cfg.RouteProviderList())
|
if httpheaders.IsWebsocket(r.Header) {
|
||||||
|
gpwebsocket.Periodic(w, r, 5*time.Second, func(conn *websocket.Conn) error {
|
||||||
|
return wsjson.Write(r.Context(), conn, cfgInstance.RouteProviderList())
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
gphttp.RespondJSON(w, r, cfgInstance.RouteProviderList())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,24 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"slices"
|
||||||
|
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListRoutesHandler(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
func ListRoutesHandler(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
gphttp.RespondJSON(w, r, routes.ByAlias(route.RouteType(r.FormValue("type"))))
|
rts := make([]routes.Route, 0)
|
||||||
|
provider := r.FormValue("provider")
|
||||||
|
if provider == "" {
|
||||||
|
gphttp.RespondJSON(w, r, slices.Collect(routes.Iter))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for r := range routes.Iter {
|
||||||
|
if r.ProviderName() == provider {
|
||||||
|
rts = append(rts, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gphttp.RespondJSON(w, r, rts)
|
||||||
}
|
}
|
||||||
|
|
|
@ -301,7 +301,7 @@ func (cfg *Config) initAutoCert(autocertCfg *autocert.Config) gperr.Error {
|
||||||
|
|
||||||
func (cfg *Config) initProxmox(proxmoxCfg []proxmox.Config) gperr.Error {
|
func (cfg *Config) initProxmox(proxmoxCfg []proxmox.Config) gperr.Error {
|
||||||
proxmox.Clients.Clear()
|
proxmox.Clients.Clear()
|
||||||
var errs = gperr.NewBuilder()
|
errs := gperr.NewBuilder()
|
||||||
for _, cfg := range proxmoxCfg {
|
for _, cfg := range proxmoxCfg {
|
||||||
if err := cfg.Init(); err != nil {
|
if err := cfg.Init(); err != nil {
|
||||||
errs.Add(err.Subject(cfg.URL))
|
errs.Add(err.Subject(cfg.URL))
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/route"
|
"github.com/yusing/go-proxy/internal/route"
|
||||||
"github.com/yusing/go-proxy/internal/route/provider"
|
"github.com/yusing/go-proxy/internal/route/provider"
|
||||||
)
|
)
|
||||||
|
@ -23,10 +24,13 @@ func (cfg *Config) DumpRouteProviders() map[string]*provider.Provider {
|
||||||
return entries
|
return entries
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) RouteProviderList() []string {
|
func (cfg *Config) RouteProviderList() []config.RouteProviderListResponse {
|
||||||
var list []string
|
var list []config.RouteProviderListResponse
|
||||||
cfg.providers.RangeAll(func(_ string, p *provider.Provider) {
|
cfg.providers.RangeAll(func(_ string, p *provider.Provider) {
|
||||||
list = append(list, p.ShortName())
|
list = append(list, config.RouteProviderListResponse{
|
||||||
|
ShortName: p.ShortName(),
|
||||||
|
FullName: p.String(),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,12 +42,15 @@ type (
|
||||||
HomepageConfig struct {
|
HomepageConfig struct {
|
||||||
UseDefaultCategories bool `json:"use_default_categories"`
|
UseDefaultCategories bool `json:"use_default_categories"`
|
||||||
}
|
}
|
||||||
|
RouteProviderListResponse struct {
|
||||||
|
ShortName string `json:"short_name"`
|
||||||
|
FullName string `json:"full_name"`
|
||||||
|
}
|
||||||
ConfigInstance interface {
|
ConfigInstance interface {
|
||||||
Value() *Config
|
Value() *Config
|
||||||
Reload() gperr.Error
|
Reload() gperr.Error
|
||||||
Statistics() map[string]any
|
Statistics() map[string]any
|
||||||
RouteProviderList() []string
|
RouteProviderList() []RouteProviderListResponse
|
||||||
Context() context.Context
|
Context() context.Context
|
||||||
GetAgent(agentAddrOrDockerHost string) (*agent.AgentConfig, bool)
|
GetAgent(agentAddrOrDockerHost string) (*agent.AgentConfig, bool)
|
||||||
VerifyNewAgent(host string, ca agent.PEMPair, client agent.PEMPair) (int, gperr.Error)
|
VerifyNewAgent(host string, ca agent.PEMPair, client agent.PEMPair) (int, gperr.Error)
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/homepage"
|
"github.com/yusing/go-proxy/internal/homepage"
|
||||||
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -69,16 +68,16 @@ func getHealthInfoRaw(r Route) *HealthInfoRaw {
|
||||||
|
|
||||||
func HealthMap() map[string]map[string]string {
|
func HealthMap() map[string]map[string]string {
|
||||||
healthMap := make(map[string]map[string]string, NumRoutes())
|
healthMap := make(map[string]map[string]string, NumRoutes())
|
||||||
for alias, r := range Iter {
|
for r := range Iter {
|
||||||
healthMap[alias] = getHealthInfo(r)
|
healthMap[r.Name()] = getHealthInfo(r)
|
||||||
}
|
}
|
||||||
return healthMap
|
return healthMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func HealthInfo() map[string]*HealthInfoRaw {
|
func HealthInfo() map[string]*HealthInfoRaw {
|
||||||
healthMap := make(map[string]*HealthInfoRaw, NumRoutes())
|
healthMap := make(map[string]*HealthInfoRaw, NumRoutes())
|
||||||
for alias, r := range Iter {
|
for r := range Iter {
|
||||||
healthMap[alias] = getHealthInfoRaw(r)
|
healthMap[r.Name()] = getHealthInfoRaw(r)
|
||||||
}
|
}
|
||||||
return healthMap
|
return healthMap
|
||||||
}
|
}
|
||||||
|
@ -116,29 +115,9 @@ func HomepageConfig(categoryFilter, providerFilter string) homepage.Homepage {
|
||||||
return hp
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func ByProvider() map[string][]Route {
|
func ByProvider() map[string][]Route {
|
||||||
rts := make(map[string][]Route)
|
rts := make(map[string][]Route)
|
||||||
for _, r := range HTTP.Iter {
|
for r := range Iter {
|
||||||
rts[r.ProviderName()] = append(rts[r.ProviderName()], r)
|
rts[r.ProviderName()] = append(rts[r.ProviderName()], r)
|
||||||
}
|
}
|
||||||
return rts
|
return rts
|
||||||
|
|
|
@ -9,13 +9,16 @@ var (
|
||||||
Stream = pool.New[StreamRoute]("stream_routes")
|
Stream = pool.New[StreamRoute]("stream_routes")
|
||||||
)
|
)
|
||||||
|
|
||||||
func Iter(yield func(alias string, r Route) bool) {
|
func Iter(yield func(r Route) bool) {
|
||||||
for k, r := range HTTP.Iter {
|
for _, r := range HTTP.Iter {
|
||||||
if !yield(k, r) {
|
if !yield(r) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for k, r := range Stream.Iter {
|
}
|
||||||
|
|
||||||
|
func IterKV(yield func(alias string, r Route) bool) {
|
||||||
|
for k, r := range HTTP.Iter {
|
||||||
if !yield(k, r) {
|
if !yield(k, r) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue