API configuration

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

The api configuration controls various functions in the api service, including but not limited to the service listening address, port, environment configuration, log configuration, etc. Let’s take a simple configuration to see what the common configurations in the api do.

Configuration instructions

Through the yaml configuration, we will find that there are many parameters that we are not aligned with config. This is because many of the config definitions are labeled with optional or default. For optional options, you can choose according to your own Need to determine whether it needs to be set. For the default tag, if you think the default value is enough, you don’t need to set it. Generally, the value in default basically does not need to be modified and can be considered as a best practice value.

Config

  1. type Config struct{
  2. rest.RestConf // rest api configuration
  3. Auth struct { // jwt authentication configuration
  4. AccessSecret string // jwt key
  5. AccessExpire int64 // jwt expire, unit: second
  6. }
  7. Mysql struct { // database configuration, in addition to mysql, there may be other databases such as mongo
  8. DataSource string // mysql datasource, which satisfies the format of user:password@tcp(ip:port)db?queries
  9. }
  10. CacheRedis cache.CacheConf // redis cache
  11. UserRpc zrpc.RpcClientConf // rpc client configuration
  12. }

rest.RestConf

The basic configuration of api service, including monitoring address, monitoring port, certificate configuration, current limit, fusing parameters, timeout parameters and other controls, expand it, we can see:

  1. service.ServiceConf // service configuration
  2. Host string `json:",default=0.0.0.0"` // http listening ip, default 0.0.0.0
  3. Port int // http listening port, required
  4. CertFile string `json:",optional"` // https certificate file, optional
  5. KeyFile string `json:",optional"` // https private key file, optional
  6. Verbose bool `json:",optional"` // whether to print detailed http request log
  7. MaxConns int `json:",default=10000"` // http can accept the maximum number of requests at the same time (current limit), the default is 10000
  8. MaxBytes int64 `json:",default=1048576,range=[0:8388608]"` // http can accept the maximum Content Length of the request, the default is 1048576, and the set value cannot be between 0 and 8388608
  9. // milliseconds
  10. Timeout int64 `json:",default=3000"` // timeout duration control, unit: milliseconds, default 3000
  11. CpuThreshold int64 `json:",default=900,range=[0:1000]"` // CPU load reduction threshold, the default is 900, the allowable setting range is 0 to 1000
  12. Signature SignatureConf `json:",optional"` // signature configuration

service.ServiceConf

  1. type ServiceConf struct {
  2. Name string // service name
  3. Log logx.LogConf // log configuration
  4. Mode string `json:",default=pro,options=dev|test|pre|pro"` // service environment, dev-development environment, test-test environment, pre-pre-release environment, pro-formal environment
  5. MetricsUrl string `json:",optional"` // index report interface address, this address needs to support post json
  6. Prometheus prometheus.Config `json:",optional"` // prometheus configuration
  7. }

logx.LogConf

  1. type LogConf struct {
  2. ServiceName string `json:",optional"` // service name
  3. Mode string `json:",default=console,options=console|file|volume"` // Log mode, console-output to console, file-output to the current server (container) file, volume-output docker hangs in the file
  4. Path string `json:",default=logs"` // Log storage path
  5. Level string `json:",default=info,options=info|error|severe"` // Log level
  6. Compress bool `json:",optional"` // whether to enable gzip compression
  7. KeepDays int `json:",optional"` // log retention days
  8. StackCooldownMillis int `json:",default=100"` // log write interval
  9. }

prometheus.Config

  1. type Config struct {
  2. Host string `json:",optional"` // prometheus monitor host
  3. Port int `json:",default=9101"` // prometheus listening port
  4. Path string `json:",default=/metrics"` // report address
  5. }

SignatureConf

  1. SignatureConf struct {
  2. Strict bool `json:",default=false"` // Whether it is Strict mode, if it is, Private Keys is required
  3. Expiry time.Duration `json:",default=1h"` // Validity period, default is 1 hour
  4. PrivateKeys []PrivateKeyConf // Signing key related configuration
  5. }

PrivateKeyConf

  1. PrivateKeyConf struct {
  2. Fingerprint string // Fingerprint configuration
  3. KeyFile string // Key configuration
  4. }

cache.CacheConf

  1. ClusterConf []NodeConf
  2. NodeConf struct {
  3. redis.RedisConf
  4. Weight int `json:",default=100"` // Weights
  5. }

redis.RedisConf

  1. RedisConf struct {
  2. Host string // redis address
  3. Type string `json:",default=node,options=node|cluster"` // redis type
  4. Pass string `json:",optional"` // redis password
  5. }