refactor(state): replace Entrypoint method with ShortLinkMatcher interface

- Cleaned up agent go.mod by removing unused indirect dependencies.
This commit is contained in:
yusing
2026-01-04 12:43:05 +08:00
parent 72e53773b0
commit da8e03258d
4 changed files with 15 additions and 10 deletions

View File

@@ -140,8 +140,8 @@ func (state *state) EntrypointHandler() http.Handler {
return &state.entrypoint
}
func (state *state) Entrypoint() *entrypoint.Entrypoint {
return &state.entrypoint
func (state *state) ShortLinkMatcher() config.ShortLinkMatcher {
return state.entrypoint.ShortLinkMatcher()
}
// AutoCertProvider returns the autocert provider.

View File

@@ -6,7 +6,6 @@ import (
"iter"
"net/http"
"github.com/yusing/godoxy/internal/entrypoint"
"github.com/yusing/godoxy/internal/types"
"github.com/yusing/goutils/server"
"github.com/yusing/goutils/synk"
@@ -23,7 +22,7 @@ type State interface {
Value() *Config
EntrypointHandler() http.Handler
Entrypoint() *entrypoint.Entrypoint
ShortLinkMatcher() ShortLinkMatcher
AutoCertProvider() server.CertProvider
LoadOrStoreProvider(key string, value types.RouteProvider) (actual types.RouteProvider, loaded bool)
@@ -35,6 +34,12 @@ type State interface {
FlushTmpLog()
}
type ShortLinkMatcher interface {
AddRoute(alias string)
DelRoute(alias string)
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
// could be nil before first call on Load
var ActiveState synk.Value[State]

View File

@@ -126,12 +126,12 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
routes.HTTP.Add(s)
if state := config.WorkingState.Load(); state != nil {
state.Entrypoint().ShortLinkMatcher().AddRoute(s.Alias)
state.ShortLinkMatcher().AddRoute(s.Alias)
}
s.task.OnFinished("remove_route_from_http", func() {
routes.HTTP.Del(s)
if state := config.WorkingState.Load(); state != nil {
state.Entrypoint().ShortLinkMatcher().DelRoute(s.Alias)
state.ShortLinkMatcher().DelRoute(s.Alias)
}
})
return nil

View File

@@ -168,12 +168,12 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
} else {
routes.HTTP.Add(r)
if state := config.WorkingState.Load(); state != nil {
state.Entrypoint().ShortLinkMatcher().AddRoute(r.Alias)
state.ShortLinkMatcher().AddRoute(r.Alias)
}
r.task.OnCancel("remove_route", func() {
routes.HTTP.Del(r)
if state := config.WorkingState.Load(); state != nil {
state.Entrypoint().ShortLinkMatcher().DelRoute(r.Alias)
state.ShortLinkMatcher().DelRoute(r.Alias)
}
})
}
@@ -216,12 +216,12 @@ func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
linked.SetHealthMonitor(lb)
routes.HTTP.AddKey(cfg.Link, linked)
if state := config.WorkingState.Load(); state != nil {
state.Entrypoint().ShortLinkMatcher().AddRoute(cfg.Link)
state.ShortLinkMatcher().AddRoute(cfg.Link)
}
r.task.OnFinished("remove_loadbalancer_route", func() {
routes.HTTP.DelKey(cfg.Link)
if state := config.WorkingState.Load(); state != nil {
state.Entrypoint().ShortLinkMatcher().DelRoute(cfg.Link)
state.ShortLinkMatcher().DelRoute(cfg.Link)
}
})
lbLock.Unlock()