feat: redesign server startup around Run options

Replace the old RunShutdown and RunTLS style entry points with a single Run(opts...) API for v1. Add focused startup semantics tests, keep TLS and graceful shutdown independent, ensure sibling servers are cleaned up on startup failure, and update docs to match the new option-based startup model.
This commit is contained in:
wjqserver 2026-04-07 17:44:55 +08:00
parent fca9bbd3ef
commit e4d3eed379
13 changed files with 577 additions and 335 deletions

View file

@ -70,42 +70,25 @@ func TestApplyDefaultServerConfig(t *testing.T) {
}
}
func TestRunTLSProtocolInheritance(t *testing.T) {
func TestTLSRunDefaultsProtocolInheritance(t *testing.T) {
engine := New()
// 模拟 RunTLS 中的逻辑: 如果使用默认协议, 则启用 HTTP/2
if engine.useDefaultProtocols {
engine.setProtocols(&ProtocolsConfig{
Http1: true,
Http2: true,
})
}
srv := &http.Server{TLSConfig: &tls.Config{}}
engine.applyDefaultServerConfig(srv)
srv := buildMainServer(engine, runConfig{addr: ":443", mode: runModeHTTPS, tlsConfig: &tls.Config{}})
if !srv.Protocols.HTTP2() {
t.Error("RunTLS simulation: Expected HTTP/2 to be enabled for default config")
t.Error("TLS run defaults: expected HTTP/2 to be enabled for default config")
}
// 模拟用户设置了自定义协议后调用 RunTLS
// 模拟用户设置了自定义协议后进入 TLS 运行模式
engine = New()
engine.SetProtocols(&ProtocolsConfig{
Http1: true,
Http2: false, // 用户明确不想要 HTTP/2
})
if engine.useDefaultProtocols {
engine.setProtocols(&ProtocolsConfig{
Http1: true,
Http2: true,
})
}
srv2 := &http.Server{TLSConfig: &tls.Config{}}
engine.applyDefaultServerConfig(srv2)
srv2 := buildMainServer(engine, runConfig{addr: ":443", mode: runModeHTTPS, tlsConfig: &tls.Config{}})
if srv2.Protocols.HTTP2() {
t.Error("RunTLS simulation: Expected HTTP/2 to be DISABLED if user set custom protocols previously")
t.Error("TLS run defaults: expected HTTP/2 to remain disabled when user set custom protocols")
}
}