config

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package config is responsible parsing the configuration file, and then, it is used by other parts of application.

Index

Constants

This section is empty.

Variables

View Source
var (
	// LevelToName map logging.Level to string represent
	LevelToName = map[logging.Level]string{
		logging.DEBUG:    "DEBUG",
		logging.INFO:     "INFO",
		logging.NOTICE:   "NOTICE",
		logging.WARNING:  "WARNING",
		logging.ERROR:    "ERROR",
		logging.CRITICAL: "CRITICAL",
	}
	// NameToLevel map string represent of log level to logging.Level
	NameToLevel = map[string]logging.Level{
		"DEBUG":    logging.DEBUG,
		"INFO":     logging.INFO,
		"NOTICE":   logging.NOTICE,
		"WARNING":  logging.WARNING,
		"ERROR":    logging.ERROR,
		"CRITICAL": logging.CRITICAL,
	}
)

Functions

func ParseConfig

func ParseConfig(configText []byte, config *Configurator) error

ParseConfig parse config content to specify configurator. If config is nil, default global Config will be used

func ParseConfigFile

func ParseConfigFile(file string, config *Configurator) error

ParseConfigFile is used to parse configuration from yaml file to specify configurator

Types

type Chunk

type Chunk struct {
	RootPath string `yaml:"rootPath,omitempty"`
}

Chunk represent config for chunk

type Configurator

type Configurator struct {
	Database `yaml:"database,omitempty"`
	Log      `yaml:"log,omitempty"`
	HTTP     `yaml:"http,omitempty"`
	Chunk    `yaml:"chunk,omitempty"`
}

Configurator combine every child config

var DefaultConfig *Configurator

DefaultConfig define a default configurator

type ConsoleLog

type ConsoleLog struct {
	// Enable represent enable or disable log
	Enable bool `yaml:"enable,omitempty"`

	// Level represent stdout log level
	Level string `yaml:"level,omitempty"`

	// Format represent log format for stdout
	Format string `yaml:"format,omitempty"`
}

ConsoleLog is used to config output handle for stdout

type Database

type Database struct {

	// driver can be sqlite3, mysql or postgres
	Driver string `yaml:"driver,omitempty"`

	// mysql and postgres database config
	// database server host
	Host string `yaml:"host,omitempty"`

	// database user
	User string `yaml:"user,omitempty"`

	// database password
	Password string `yaml:"password,omitempty"`

	// database name
	DBName string `yaml:"dbName,omitempty"`

	// database server port
	Port uint32 `yaml:"port,omitempty"`

	// sqlite3 db file
	DBFile string `yaml:"dbFile,omitempty"`
}

Database represent database config

func (Database) DSN

func (d Database) DSN() (string, error)

DSN generate dsn based on db driver

type FileLog

type FileLog struct {
	// Enable represent enable or disable log
	Enable bool `yaml:"enable,omitempty"`

	// Level represent file log level
	Level string `yaml:"level,omitempty"`

	// Format represent log format for file
	Format string `yaml:"format,omitempty"`

	// Path is used to config log output destination
	Path string `yaml:"path,omitempty"`

	// MaxBytesPerFile is used to config max capacity of single log file, unit: bytes
	MaxBytesPerFile uint64 `yaml:"maxBytesPerFile,omitempty"`
}

FileLog is used to config output handler for file

type HTTP

type HTTP struct {

	// APIPrefix represent api prefix for each route.
	// example: /api/v1
	APIPrefix string `yaml:"apiPrefix,omitempty"`

	// AccessLogFile represent http access log file. if it's empty,
	// log will not be written to accessLogFile
	AccessLogFile string `yaml:"accessLogFile,omitempty"`

	// LimitRateByIPEnable represents whether enable limit rate middleware
	// default: false
	LimitRateByIPEnable bool `yaml:"limitRateByIPEnable,omitempty"`

	// LimitRateInterval represent limit interval, unit: ms, default: 1000ms, that is 1s
	LimitRateByIPInterval int64 `yaml:"limitRateByIPInterval,omitempty"`

	// LimitRateByIPMaxNum represent max request limit per LimitRateByIPInterval
	// default: 100
	LimitRateByIPMaxNum uint `yaml:"limitRateByIPMaxNum,omitempty"`

	CORSEnable           bool     `yaml:"corsEnable,omitempty"`
	CORSAllowAllOrigins  bool     `yaml:"corsAllowAllOrigins,omitempty"`
	CORSAllowOrigins     []string `yaml:"corsAllowOrigins,omitempty"`
	CORSAllowMethods     []string `yaml:"corsAllowMethods,omitempty"`
	CORSAllowHeaders     []string `yaml:"corsAllowHeaders,omitempty"`
	CORSExposeHeaders    []string `yaml:"corsExposeHeaders,omitempty"`
	CORSAllowCredentials bool     `yaml:"corsAllowCredentials,omitempty"`
	// CORSMaxAge indicates how long (in seconds) the results of a preflight request
	// can be cached
	CORSMaxAge int64 `yaml:"corsMaxAge,omitempty"`
}

HTTP define config format for http service

type Log

type Log struct {
	Console ConsoleLog `yaml:"console,omitempty"`
	File    FileLog    `yaml:"file,omitempty"`
}

Log includes configuration item for log component

Jump to

Keyboard shortcuts

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