|
|
package nnet
|
|
|
|
|
|
import (
|
|
|
"git.noahlan.cn/northlan/nnet/component"
|
|
|
"git.noahlan.cn/northlan/nnet/env"
|
|
|
"git.noahlan.cn/northlan/nnet/log"
|
|
|
"git.noahlan.cn/northlan/nnet/pipeline"
|
|
|
"net/http"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type Option func(options *Options)
|
|
|
type WSOption func(opts *WSOptions)
|
|
|
|
|
|
func WithLogger(logger log.Logger) Option {
|
|
|
return func(_ *Options) {
|
|
|
log.SetLogger(logger)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithPipeline(pipeline pipeline.Pipeline) Option {
|
|
|
return func(options *Options) {
|
|
|
options.Pipeline = pipeline
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithComponents(components *component.Components) Option {
|
|
|
return func(options *Options) {
|
|
|
options.Components = components
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithHeartbeatInterval(d time.Duration) Option {
|
|
|
return func(options *Options) {
|
|
|
options.HeartbeatInterval = d
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// WithTimerPrecision 设置Timer精度
|
|
|
// 注:精度需大于1ms, 并且不能在运行时更改
|
|
|
// 默认精度是 time.Second
|
|
|
func WithTimerPrecision(precision time.Duration) Option {
|
|
|
if precision < time.Millisecond {
|
|
|
panic("time precision can not less than a Millisecond")
|
|
|
}
|
|
|
return func(_ *Options) {
|
|
|
env.TimerPrecision = precision
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// WithWebsocket 开启Websocket, 参数是websocket的相关参数 nnet.WSOption
|
|
|
func WithWebsocket(wsOpts ...WSOption) Option {
|
|
|
return func(options *Options) {
|
|
|
for _, opt := range wsOpts {
|
|
|
opt(&options.WS)
|
|
|
}
|
|
|
options.WS.IsWebsocket = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// WithWSPath 设置websocket的path
|
|
|
func WithWSPath(path string) WSOption {
|
|
|
return func(opts *WSOptions) {
|
|
|
opts.WebsocketPath = path
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// WithWSTLSConfig 设置websocket的证书和密钥
|
|
|
func WithWSTLSConfig(certificate, key string) WSOption {
|
|
|
return func(opts *WSOptions) {
|
|
|
opts.TLSCertificate = certificate
|
|
|
opts.TLSKey = key
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithWSCheckOriginFunc(fn func(*http.Request) bool) WSOption {
|
|
|
return func(opts *WSOptions) {
|
|
|
if fn != nil {
|
|
|
opts.CheckOrigin = fn
|
|
|
}
|
|
|
}
|
|
|
}
|