exegroup/修改HTTPListenAndServe支持tls并删除HTTPServer;

develop exegroup/v0.6.0
Ge Song 1 year ago
parent c82211c957
commit c2bf543f0a

@ -15,51 +15,73 @@ const (
) )
type Option interface { type Option interface {
apply(server *http.Server) apply(cfg *config)
} }
func WithPort(port int) Option { func WithPort(port int) Option {
return optionFunc(func(server *http.Server) { return optionFunc(func(cfg *config) {
server.Addr = net.JoinHostPort("", strconv.Itoa(port)) cfg.server.Addr = net.JoinHostPort("", strconv.Itoa(port))
}) })
} }
func WithHandler(handler http.Handler) Option { func WithHandler(handler http.Handler) Option {
return optionFunc(func(server *http.Server) { return optionFunc(func(cfg *config) {
server.Handler = handler cfg.server.Handler = handler
})
}
func WithServer(server *http.Server) Option {
return optionFunc(func(cfg *config) {
cfg.server = server
})
}
func WithStartFn(fn func(server *http.Server) error) Option {
return optionFunc(func(cfg *config) {
cfg.startFn = fn
}) })
} }
// WithServerOption 使用 fn 配置 [http.Server]; // WithServerOption 使用 fn 配置 [http.Server];
func WithServerOption(fn func(server *http.Server)) Option { func WithServerOption(fn func(server *http.Server)) Option {
return optionFunc(fn) return optionFunc(func(cfg *config) {
fn(cfg.server)
})
} }
type optionFunc func(server *http.Server) type config struct {
server *http.Server
func (fn optionFunc) apply(server *http.Server) { startFn func(server *http.Server) error
fn(server)
} }
// HTTPListenAndServe 创建 [http.Server] 并提供启动和停止函数; func newDefaultConfig() *config {
// opts 按照顺序应用到 [http.Server] 上. return &config{
func HTTPListenAndServe(opts ...Option) (func(ctx context.Context) error, func(ctx context.Context)) { server: &http.Server{
server := &http.Server{
Addr: DefaultAddr, Addr: DefaultAddr,
ReadHeaderTimeout: DefaultReadHeaderTimeout, ReadHeaderTimeout: DefaultReadHeaderTimeout,
},
startFn: func(server *http.Server) error {
return server.ListenAndServe()
},
} }
for _, opt := range opts {
opt.apply(server)
}
return HTTPServer(server)
} }
// HTTPServer 提供 [http.Server] 的启动和停止函数; type optionFunc func(cfg *config)
func HTTPServer(server *http.Server) (func(ctx context.Context) error, func(ctx context.Context)) {
func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}
// HTTPListenAndServe 创建 [http.Server] 并提供启动和停止函数;
func HTTPListenAndServe(opts ...Option) (func(ctx context.Context) error, func(ctx context.Context)) {
cfg := newDefaultConfig()
for _, opt := range opts {
opt.apply(cfg)
}
inShutdown := &atomic.Bool{} inShutdown := &atomic.Bool{}
c := make(chan error, 1) c := make(chan error, 1)
goFunc := func(_ context.Context) error { goFunc := func(_ context.Context) error {
err := server.ListenAndServe() err := cfg.startFn(cfg.server)
if inShutdown.Load() { if inShutdown.Load() {
err = <-c err = <-c
} }
@ -67,7 +89,7 @@ func HTTPServer(server *http.Server) (func(ctx context.Context) error, func(ctx
} }
stopFunc := func(ctx context.Context) { stopFunc := func(ctx context.Context) {
inShutdown.Store(true) inShutdown.Store(true)
c <- server.Shutdown(ctx) c <- cfg.server.Shutdown(ctx)
} }
return goFunc, stopFunc return goFunc, stopFunc
} }

Loading…
Cancel
Save