github.com/imyousuf/webhook-broker@v0.1.2/config/api.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"database/sql"
     6  	"database/sql/driver"
     7  	"encoding/json"
     8  	"net/url"
     9  	"strings"
    10  	"time"
    11  )
    12  
    13  // RelationalDatabaseConfig represents DB configuration related behaviors
    14  type RelationalDatabaseConfig interface {
    15  	GetDBDialect() DBDialect
    16  	GetDBConnectionURL() string
    17  	GetDBConnectionMaxIdleTime() time.Duration
    18  	GetDBConnectionMaxLifetime() time.Duration
    19  	GetMaxIdleDBConnections() uint16
    20  	GetMaxOpenDBConnections() uint16
    21  }
    22  
    23  // HTTPConfig represents the HTTP configuration related behaviors
    24  type HTTPConfig interface {
    25  	GetHTTPListeningAddr() string
    26  	GetHTTPReadTimeout() time.Duration
    27  	GetHTTPWriteTimeout() time.Duration
    28  }
    29  
    30  // LogConfig represents the interface for log related configuration
    31  type LogConfig interface {
    32  	GetLogLevel() LogLevel
    33  	IsLoggerConfigAvailable() bool
    34  	GetLogFilename() string
    35  	GetMaxLogFileSize() uint
    36  	GetMaxLogBackups() uint
    37  	GetMaxAgeForALogFile() uint
    38  	IsCompressionEnabledOnLogBackups() bool
    39  }
    40  
    41  // SeedProducer represents the pre configured producer via configuration
    42  type SeedProducer struct {
    43  	// ID is the ID of the data
    44  	ID string
    45  	// Name is name of the data
    46  	Name string
    47  	// Token is the pre configured token of the data
    48  	Token string
    49  }
    50  
    51  // SeedChannel represents pre configured channel via configuration
    52  type SeedChannel SeedProducer
    53  
    54  // SeedConsumer represents pre configured consumer via configuration
    55  type SeedConsumer struct {
    56  	SeedProducer
    57  	// CallbackURL represents the URl to call back
    58  	CallbackURL *url.URL
    59  	// Channels represents which channel this consumer listens to
    60  	Channel string
    61  }
    62  
    63  // SeedData represents data specified in configuration to ensure is present when app starts up
    64  type SeedData struct {
    65  	DataHash  string
    66  	Producers []SeedProducer
    67  	Channels  []SeedChannel
    68  	Consumers []SeedConsumer
    69  }
    70  
    71  // Scan de-serializes SeedData for reading from DB
    72  func (u *SeedData) Scan(value interface{}) (err error) {
    73  	if stringVal, ok := value.(string); ok {
    74  		err = json.NewDecoder(strings.NewReader(stringVal)).Decode(u)
    75  	} else if sqlRawBytes, ok := value.(sql.RawBytes); ok {
    76  		err = json.NewDecoder(strings.NewReader(string(sqlRawBytes))).Decode(u)
    77  	} else if rawBytes, ok := value.([]byte); ok {
    78  		err = json.NewDecoder(strings.NewReader(string(rawBytes))).Decode(u)
    79  	}
    80  	return err
    81  }
    82  
    83  // Value serializes SeedData to write to DB
    84  func (u SeedData) Value() (driver.Value, error) {
    85  	var buf bytes.Buffer
    86  	err := json.NewEncoder(&buf).Encode(u)
    87  	return buf.Bytes(), err
    88  }
    89  
    90  // SeedDataConfig provides the interface for working with SeedData in configuration
    91  type SeedDataConfig interface {
    92  	GetSeedData() SeedData
    93  }
    94  
    95  // ConsumerConnectionConfig provides the interface for working with consumer connection related configuration
    96  type ConsumerConnectionConfig interface {
    97  	GetTokenRequestHeaderName() string
    98  	GetUserAgent() string
    99  	GetConnectionTimeout() time.Duration
   100  }
   101  
   102  // BrokerConfig provides the interface for configuring the broker
   103  type BrokerConfig interface {
   104  	GetMaxMessageQueueSize() uint
   105  	GetMaxWorkers() uint
   106  	IsPriorityDispatcherEnabled() bool
   107  	GetRetriggerBaseEndpoint() string
   108  	GetMaxRetry() uint8
   109  	GetRationalDelay() time.Duration
   110  	GetRetryBackoffDelays() []time.Duration
   111  	IsRecoveryWorkersEnabled() bool
   112  }