func setDefaults(o Options) Options { if o.MetricsClient == nil { o.MetricsClient = metrics.NewNop() } if o.TimeProvider == nil { o.TimeProvider = &timetools.RealTime{} } if o.Router == nil { o.Router = route.NewMux() } return o }
// New returns a new Service. Config can not be nil. func New(config *Config) (*Service, error) { var err error var keyBytes *[SecretKeyLength]byte var metricsClient metrics.Client // read in key from keypath or if not given, try getting them from key bytes. if config.KeyPath != "" { keyBytes, err = readKeyFromDisk(config.KeyPath) if err != nil { return nil, err } } else { if config.KeyBytes == nil { return nil, fmt.Errorf("No key bytes provided.") } keyBytes = config.KeyBytes } // setup metrics service if config.EmitStats { // get hostname of box hostname, err := os.Hostname() if err != nil { return nil, fmt.Errorf("failed to obtain hostname: %v", err) } // build lemma prefix prefix := "lemma." + strings.Replace(hostname, ".", "_", -1) if config.StatsdPrefix != "" { prefix += "." + config.StatsdPrefix } // build metrics client hostport := fmt.Sprintf("%v:%v", config.StatsdHost, config.StatsdPort) metricsClient, err = metrics.NewWithOptions(hostport, prefix, metrics.Options{UseBuffering: true}) if err != nil { return nil, err } } else { // if you don't want to emit stats, use the nop client metricsClient = metrics.NewNop() } return &Service{ secretKey: keyBytes, metricsClient: metricsClient, }, nil }
func setDefaults(o Options) Options { if o.MetricsClient == nil { o.MetricsClient = metrics.NewNop() } if o.TimeProvider == nil { o.TimeProvider = &timetools.RealTime{} } if o.Router == nil { o.Router = route.NewMux() } if o.IncomingConnectionTracker == nil { o.IncomingConnectionTracker = newDefaultConnTracker() } return o }
// Returns a new Service. Provides control over time and random providers. func NewWithProviders(config *Config, timeProvider timetools.TimeProvider, randomProvider random.RandomProvider) (*Service, error) { // config is required! if config == nil { return nil, fmt.Errorf("config is required.") } // set defaults if not set if config.NonceCacheCapacity < 1 { config.NonceCacheCapacity = CacheCapacity } if config.NonceCacheTimeout < 1 { config.NonceCacheTimeout = CacheTimeout } if config.NonceHeaderName == "" { config.NonceHeaderName = XMailgunNonce } if config.TimestampHeaderName == "" { config.TimestampHeaderName = XMailgunTimestamp } if config.SignatureHeaderName == "" { config.SignatureHeaderName = XMailgunSignature } if config.SignatureVersionHeaderName == "" { config.SignatureVersionHeaderName = XMailgunSignatureVersion } // setup metrics service metricsClient := metrics.NewNop() if config.EmitStats { // get hostname of box hostname, err := os.Hostname() if err != nil { return nil, fmt.Errorf("failed to obtain hostname: %v", err) } // build lemma prefix prefix := "lemma." + strings.Replace(hostname, ".", "_", -1) if config.StatsdPrefix != "" { prefix += "." + config.StatsdPrefix } // build metrics client hostport := fmt.Sprintf("%v:%v", config.StatsdHost, config.StatsdPort) metricsClient, err = metrics.NewWithOptions(hostport, prefix, metrics.Options{UseBuffering: true}) if err != nil { return nil, err } } // read key from disk, if no key is read that's okay it might be passed in keyBytes, err := readKeyFromDisk(config.Keypath) // setup nonce cache ncache, err := NewNonceCache(config.NonceCacheCapacity, config.NonceCacheTimeout, timeProvider) if err != nil { return nil, err } // return service return &Service{ config: config, nonceCache: ncache, secretKey: keyBytes, timeProvider: timeProvider, randomProvider: randomProvider, metricsClient: metricsClient, }, nil }