confs

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

This package contains popular things configuration and new methods for them

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEsClient added in v0.9.0

func NewEsClient(conf *EsConf, customizer EsConfigCustomizer) *elasticsearch.Client

func NewFastHttpClient

func NewFastHttpClient(hcConf *FastHttpClientConf, customizer FastHttpClientCustomizer) *fasthttp.Client
Example
hcConf := &FastHttpClientConf{}
conf.Load(hcConf, "")
customizer := func(hc *fasthttp.Client) {
	// do something
}
hc := NewFastHttpClient(hcConf, customizer)
hc.Get(make([]byte, 0), "https://baidu.com")
Output:

func NewFasthttpRoutingPprofHandler added in v0.10.0

func NewFasthttpRoutingPprofHandler(conf *PprofConf) routing.Handler

create a fasthttp routing handler for pprof handler

Example
pprofConf := &PprofConf{
	Username:             "abc",
	Password:             "xyz",
	BlockProfileRate:     1,
	MutexProfileFraction: 1,
}
router := routing.New()
router.Any("/debug/pprof/*", NewFasthttpRoutingPprofHandler(pprofConf))
Output:

func NewGinPprofBasicAuthFilter added in v0.10.0

func NewGinPprofBasicAuthFilter(conf *PprofConf) gin.HandlerFunc

create a gin basic auth filter for pprof endpoint

Example
pprofConf := &PprofConf{
	Username:             "abc",
	Password:             "xyz",
	BlockProfileRate:     1,
	MutexProfileFraction: 1,
}
r := gin.Default()
pprofGroup := r.Group("debug/pprof", NewGinPprofBasicAuthFilter(pprofConf))
pprof.RouteRegister(pprofGroup, "")
Output:

func NewMySqlDb

func NewMySqlDb(conf *MysqlConf, customizer MysqlConfigCustomizer) *sql.DB
Example
mysqlConf := &MysqlConf{}
conf.Load(mysqlConf, "")
customizer := func(mc *mysql.Config) {
	mc.Params["autocommit"] = "true"
	mc.Params["charset"] = "utf8"
}
mySqlDb := NewMySqlDb(mysqlConf, customizer)
mySqlDb.Close()
Output:

func NewRedisClient

func NewRedisClient(rc *RedisConf, customizer RedisOptionCustomizer) *redis.Client
Example
redisConf := &RedisConf{}
conf.Load(redisConf, "")
customizer := func(ropt *redis.Options) {
	ropt.MaxRetries = 2
}
redisClient := NewRedisClient(redisConf, customizer)
redisClient.Close()
Output:

Types

type EsConf added in v0.9.0

type EsConf struct {
	Addrs               string
	Username            string
	Password            string
	MaxConnsPerHost     int
	MaxIdleConnsPerHost int
}

func (*EsConf) String added in v0.9.0

func (m *EsConf) String() string

type EsConfigCustomizer added in v0.9.0

type EsConfigCustomizer func(mc *elasticsearch.Config)

type FastHttpClientConf

type FastHttpClientConf struct {
	CertChain     string // Path to cert when communicate client auth enabled origin server, including intermediate certs. x.509 pem encoded.
	PrivateKey    string // Path to private key when communicate client auth enabled origin server, including intermediate certs. PKCS #1 pem encoded.
	SslTrustMode  string // Trust mode when verifying server certificates. Available modes are OS: use host root CA set. INSECURE: do not verify. CUSTOM: verify by custom cert.
	SslTrustCerts string // Path to certificates that clients use when verifying server certificates, only useful when client-ssl-trust-mode set to CUSTOM. X.509 PEM encoded

	ReadTimeout         time.Duration
	WriteTimeout        time.Duration
	MaxConnDuration     time.Duration
	MaxConnsPerHost     int
	MaxIdleConnDuration time.Duration
	MaxConnWaitTimeout  time.Duration
}

Config keys:

| Environment            |  Flag                   |  Description                              |
|------------------------|-------------------------|-------------------------------------------|
| CERT_CHAIN             | -cert-chain             | Path to cert when communicate client auth |
|                        |                         | enabled origin server, including          |
|                        |                         | intermediate certs. x.509 pem encoded.    |
| PRIVATE_KEY            | -private-key            | Path to private key when communicate      |
|                        |                         | client auth enabled origin server,        |
|                        |                         | including intermediate certs.             |
|                        |                         | PKCS #1 pem encoded.                      |
| SSL_TRUST_MODE         | -ssl-trust-mode         | Trust mode when verifying server          |
|                        |                         | certificates. Available modes are:        |
|                        |                         | OS: use host root CA set.                 |
|                        |                         | INSECURE: do not verify.                  |
|                        |                         | CUSTOM: verify by custom cert.            |
| SSL_TRUST_CERTS        | -ssl-trust-certs        | Path to certificates that clients use     |
|                        |                         | when verifying server certificates, only  |
|                        |                         | useful when client-ssl-trust-mode set     |
|                        |                         | to CUSTOM. X.509 PEM encoded              |
| READ_TIMEOUT           | -read-timeout           |                                           |
| WRITE_TIMEOUT          | -write-timeout          |                                           |
| MAX_CONN_DURATION      | -max-conn-duration      |                                           |
| MAX_CONNS_PER_HOST     | -max-conns-per-host     |                                           |
| MAX_IDLE_CONN_DURATION | -max-idle-conn-duration |                                           |
| MAX_CONN_WAIT_TIMEOUT  | -max-conn-wait-timeout  |                                           |

Note: if FastHttpClientConf is nested in another struct, add corresponding prefix.

func (*FastHttpClientConf) String

func (c *FastHttpClientConf) String() string

type FastHttpClientCustomizer

type FastHttpClientCustomizer func(hc *fasthttp.Client)

type MysqlConf

type MysqlConf struct {
	Host     string // MySQL host
	Port     int    // MySQL port
	Username string // MySQL username
	Password string // MySQL password
	Database string // MySQL database

	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration

	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	Timeout      time.Duration

	Loc       string // Location for time.Time values
	ParseTime bool   // Parse time values to time.Time

	Params string
}

Config keys:

| Environment       |  Flag              |  Description                                               |
|-------------------|--------------------|------------------------------------------------------------|
| HOST              | -host              |                                                            |
| PORT              | -port              |                                                            |
| USERNAME          | -username          |                                                            |
| PASSWORD          | -password          |                                                            |
| DATABASE          | -database          |                                                            |
| MAX_OPEN_CONNS    | -max-open-conns    | Maximum number of open connections to the database.        |
|                   |                    | If == 0 means unlimited                                    |
| MAX_IDLE_CONNS    | -max-idle-conns    | Maximum number of connections in the idle connection pool. |
|                   |                    | If == 0 no idle connections are retained                   |
| CONN_MAX_LIFETIME | -conn-max-lifetime | Maximum amount of time a connection may be reused.         |
|                   |                    | If == 0, connections are reused forever.                   |
| LOC               | -loc               | Sets the location for time.Time values                     |
|                   |                    | (when using parseTime=true)                                |
|                   |                    | "Local" sets the system's location.                        |
|                   |                    | See time.LoadLocation for details.                         |
| PARSE_TIME        | -parse-time        | parseTime=true changes the output type of DATE and         |
|                   |                    | DATETIME values to time.Time instead of []byte / string    |
| READ_TIMEOUT      | -read-timeout      | I/O read timeout                                           |
| WRITE_TIMEOUT     | -write-timeout     | I/O write timeout                                          |
| TIMEOUT           | -timeout           | Timeout for establishing connections, aka dial timeout.    |
| PARAMS            |                    | Connection parameters, eg, foo=1&bar=%20                   |
|                   |                    | 1. All string values should be quoted with '               |
|                   |                    | 2. All value in PARAMS should be url.QueryEscaped          |
|                   |                    | more details:                                              |
|                   |                    | https://github.com/go-sql-driver/mysql#system-variables    |

Note: if MysqlConf is nested in another struct, add corresponding prefix.

more details: https://github.com/go-sql-driver/mysql

more details:

func (*MysqlConf) String

func (m *MysqlConf) String() string

type MysqlConfigCustomizer

type MysqlConfigCustomizer func(mc *mysql.Config)

type PprofConf added in v0.10.0

type PprofConf struct {
	Username             string // /debug/pprof/* basic auth username
	Password             string // /debug/pprof/* basic auth password
	BlockProfileRate     int
	MutexProfileFraction int
}

func (*PprofConf) String added in v0.10.0

func (p *PprofConf) String() string

type RedisConf

type RedisConf struct {
	Host     string // Redis host
	Port     int    // Redis port
	Password string // Redis password
	Pool     int    // Redis pool size
	MinIdle  int    // Redis min idle
}

Config keys:

| Environment   |  Flag     |  Description             |
|---------------|-----------|--------------------------|
| HOST          | -host     |                          |
| PORT          | -port     |                          |
| PASSWORD      | -password |                          |
| POOL          | -pool     | Connection pool size     |
| MIN_IDLE      | -min-idle | Minimal idle connections |

Note: if RedisConf is nested in another struct, add corresponding prefix.

func (*RedisConf) String

func (r *RedisConf) String() string

type RedisOptionCustomizer

type RedisOptionCustomizer func(ropt *redis.Options)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL