You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet-old/nnet/options.go

83 lines
1.8 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
}
}
}