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

@@ -27,7 +27,7 @@ type findRouteFunc func(HTTPRoutes, string) types.HTTPRoute
type Entrypoint struct {
task *task.Task
cfg *entrypoint.Config
cfg *Config
middleware *middleware.Middleware
notFoundHandler http.Handler
@@ -48,9 +48,9 @@ type Entrypoint struct {
var _ entrypoint.Entrypoint = &Entrypoint{}
var emptyCfg entrypoint.Config
var emptyCfg Config
func NewEntrypoint(parent task.Parent, cfg *entrypoint.Config) *Entrypoint {
func NewEntrypoint(parent task.Parent, cfg *Config) *Entrypoint {
if cfg == nil {
cfg = &emptyCfg
}
@@ -91,12 +91,23 @@ func NewEntrypoint(parent task.Parent, cfg *entrypoint.Config) *Entrypoint {
return ep
}
func (ep *Entrypoint) ShortLinkMatcher() *ShortLinkMatcher {
return ep.shortLinkMatcher
func (ep *Entrypoint) SupportProxyProtocol() bool {
return ep.cfg.SupportProxyProtocol
}
func (ep *Entrypoint) Config() *entrypoint.Config {
return ep.cfg
func (ep *Entrypoint) DisablePoolsLog(v bool) {
ep.httpPoolDisableLog.Store(v)
// apply to all running http servers
for _, srv := range ep.servers.Range {
srv.routes.DisableLog(v)
}
// apply to other pools
ep.streamRoutes.DisableLog(v)
ep.excludedRoutes.DisableLog(v)
}
func (ep *Entrypoint) ShortLinkMatcher() *ShortLinkMatcher {
return ep.shortLinkMatcher
}
func (ep *Entrypoint) HTTPRoutes() entrypoint.PoolLike[types.HTTPRoute] {
@@ -111,19 +122,12 @@ func (ep *Entrypoint) ExcludedRoutes() entrypoint.RWPoolLike[types.Route] {
return ep.excludedRoutes
}
func (ep *Entrypoint) GetServer(addr string) (*httpServer, bool) {
func (ep *Entrypoint) GetServer(addr string) (http.Handler, bool) {
return ep.servers.Load(addr)
}
func (ep *Entrypoint) DisablePoolsLog(v bool) {
ep.httpPoolDisableLog.Store(v)
// apply to all running http servers
for _, srv := range ep.servers.Range {
srv.routes.DisableLog(v)
}
// apply to other pools
ep.streamRoutes.DisableLog(v)
ep.excludedRoutes.DisableLog(v)
func (ep *Entrypoint) PrintServers() {
log.Info().Msgf("servers: %v", xsync.ToPlainMap(ep.servers))
}
func (ep *Entrypoint) SetFindRouteDomains(domains []string) {

View File

@@ -11,6 +11,7 @@ import (
"testing"
. "github.com/yusing/godoxy/internal/entrypoint"
entrypoint "github.com/yusing/godoxy/internal/entrypoint/types"
"github.com/yusing/godoxy/internal/route"
routeTypes "github.com/yusing/godoxy/internal/route/types"
"github.com/yusing/godoxy/internal/types"
@@ -47,13 +48,15 @@ func (t noopTransport) RoundTrip(req *http.Request) (*http.Response, error) {
}
func BenchmarkEntrypointReal(b *testing.B) {
var ep Entrypoint
task := task.NewTestTask(b)
ep := NewEntrypoint(task, nil)
req := http.Request{
Method: "GET",
URL: &url.URL{Path: "/", RawPath: "/"},
Host: "test.domain.tld",
}
ep.SetFindRouteDomains([]string{})
entrypoint.SetCtx(task, ep)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1")
@@ -89,7 +92,7 @@ func BenchmarkEntrypointReal(b *testing.B) {
b.Fatal(err)
}
err = r.Start(task.NewTestTask(b))
err = r.Start(task)
if err != nil {
b.Fatal(err)
}
@@ -114,13 +117,15 @@ func BenchmarkEntrypointReal(b *testing.B) {
}
func BenchmarkEntrypoint(b *testing.B) {
var ep Entrypoint
task := task.NewTestTask(b)
ep := NewEntrypoint(task, nil)
req := http.Request{
Method: "GET",
URL: &url.URL{Path: "/", RawPath: "/"},
Host: "test.domain.tld",
}
ep.SetFindRouteDomains([]string{})
entrypoint.SetCtx(task, ep)
r := &route.Route{
Alias: "test",
@@ -139,7 +144,7 @@ func BenchmarkEntrypoint(b *testing.B) {
b.Fatal(err)
}
err = r.Start(task.RootTask("test", false))
err = r.Start(task)
if err != nil {
b.Fatal(err)
}

View File

@@ -74,11 +74,14 @@ func (ep *Entrypoint) AddRoute(r types.Route) {
func (ep *Entrypoint) AddHTTPRoute(route types.HTTPRoute) error {
if port := route.ListenURL().Port(); port == "" || port == "0" {
host := route.ListenURL().Hostname()
var httpAddr, httpsAddr string
if host == "" {
host = common.ProxyHTTPHost
httpAddr = common.ProxyHTTPAddr
httpsAddr = common.ProxyHTTPSAddr
} else {
httpAddr = net.JoinHostPort(host, strconv.Itoa(common.ProxyHTTPPort))
httpsAddr = net.JoinHostPort(host, strconv.Itoa(common.ProxyHTTPSPort))
}
httpAddr := net.JoinHostPort(host, strconv.Itoa(common.ProxyHTTPPort))
httpsAddr := net.JoinHostPort(host, strconv.Itoa(common.ProxyHTTPSPort))
return errors.Join(ep.addHTTPRoute(route, httpAddr, HTTPProtoHTTP), ep.addHTTPRoute(route, httpsAddr, HTTPProtoHTTPS))
}

View File

@@ -165,7 +165,7 @@ func TestEntrypoint_ShortLinkDispatch(t *testing.T) {
ep.ShortLinkMatcher().AddRoute("app")
server := NewHTTPServer(ep)
err := server.Listen("localhost:8080", HTTPProtoHTTP)
err := server.Listen("localhost:0", HTTPProtoHTTP)
require.NoError(t, err)
t.Run("shortlink host", func(t *testing.T) {

View File

@@ -5,7 +5,7 @@ import (
)
type Entrypoint interface {
Config() *Config
SupportProxyProtocol() bool
DisablePoolsLog(v bool)