fix: add nil guard before entrypoint retrieval; move config from types/

This commit is contained in:
yusing
2026-02-06 12:01:09 +08:00
parent e383cd247a
commit a6fed3f221
17 changed files with 90 additions and 37 deletions

View File

@@ -126,7 +126,11 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
}
}
entrypoint.FromCtx(parent.Context()).AddRoute(s)
ep := entrypoint.FromCtx(parent.Context())
if ep == nil {
return gperr.New("entrypoint not initialized")
}
ep.AddRoute(s)
return nil
}

View File

@@ -163,10 +163,15 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
}
}
ep := entrypoint.FromCtx(parent.Context())
if ep == nil {
return gperr.New("entrypoint not initialized")
}
if r.UseLoadBalance() {
r.addToLoadBalancer(parent)
r.addToLoadBalancer(parent, ep)
} else {
entrypoint.FromCtx(parent.Context()).AddRoute(r)
ep.AddRoute(r)
}
return nil
}
@@ -178,12 +183,11 @@ func (r *ReveseProxyRoute) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var lbLock sync.Mutex
func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent, ep entrypoint.Entrypoint) {
var lb *loadbalancer.LoadBalancer
cfg := r.LoadBalance
lbLock.Lock()
ep := entrypoint.FromCtx(r.task.Context())
l, ok := ep.HTTPRoutes().Get(cfg.Link)
var linked *ReveseProxyRoute
if ok {

View File

@@ -46,7 +46,7 @@ type (
Host string `json:"host,omitempty"`
Port route.Port `json:"port"`
Bind string `json:"bind,omitempty" validate:"omitempty,dive,ip_addr" extensions:"x-nullable"`
Bind string `json:"bind,omitempty" validate:"omitempty,ip_addr" extensions:"x-nullable"`
Root string `json:"root,omitempty"`
SPA bool `json:"spa,omitempty"` // Single-page app mode: serves index for non-existent paths
@@ -199,7 +199,11 @@ func (r *Route) validate() gperr.Error {
if (r.Proxmox == nil || r.Proxmox.Node == "" || r.Proxmox.VMID == nil) && r.Container == nil {
wasNotNil := r.Proxmox != nil
proxmoxProviders := config.WorkingState.Load().Value().Providers.Proxmox
workingState := config.WorkingState.Load()
var proxmoxProviders []*proxmox.Config
if workingState != nil { // nil in tests
proxmoxProviders = workingState.Value().Providers.Proxmox
}
if len(proxmoxProviders) > 0 {
// it's fine if ip is nil
hostname := r.Host

View File

@@ -82,7 +82,11 @@ func (r *StreamRoute) Start(parent task.Parent) gperr.Error {
r.l.Info().Msg("stream closed")
})
entrypoint.FromCtx(parent.Context()).AddRoute(r)
ep := entrypoint.FromCtx(parent.Context())
if ep == nil {
return gperr.New("entrypoint not initialized")
}
ep.AddRoute(r)
return nil
}

View File

@@ -58,8 +58,10 @@ func (s *TCPTCPStream) ListenAndServe(ctx context.Context, preDial, onRead netty
s.listener = acl.WrapTCP(s.listener)
}
if proxyProto := entrypoint.FromCtx(ctx).Config().SupportProxyProtocol; proxyProto {
s.listener = &proxyproto.Listener{Listener: s.listener}
if ep := entrypoint.FromCtx(ctx); ep != nil {
if proxyProto := entrypoint.FromCtx(ctx).SupportProxyProtocol(); proxyProto {
s.listener = &proxyproto.Listener{Listener: s.listener}
}
}
s.preDial = preDial