conf

package
v0.0.0-...-65546df Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = App{
	Server: Server{
		Address:      "127.0.0.1:8080",
		BasePath:     "/api",
		ReadTimeout:  duration.Minute,
		WriteTimeout: duration.Minute,
		IdleTimeout:  5 * duration.Minute,
		MultipartMax: 50 << 20,
		Pprof:        false,
		Swagger:      true,
	},
	Log: Log{
		Filename: "/etc/ginx-internal/internal.log",
		Prompt:   "[ginx-internal]",
		Level:    slog.LevelInfo,
		Format:   logx.TextFormat,
		Source:   false,
		Color:    false,
	},
	DB: DB{
		Driver:             "unset",
		Address:            "127.0.0.1:3306",
		User:               "user",
		Password:           "password",
		Database:           "database",
		Params:             "",
		MaxIdleConnections: 10,
		MaxOpenConnections: 100,
		MaxLifeTime:        duration.Hour,
		MaxIdleTime:        10 * duration.Minute,
	},
	Redis: Redis{
		Address:      "127.0.0.1:6379",
		Password:     "password",
		WriteTimeout: duration.Minute,
		ReadTimeout:  duration.Minute,
	},
	Email: Email{
		Host:     "",
		Port:     0,
		Username: "",
		Password: "",
		MQ: EmailMq{
			Topic:     "email",
			BatchSize: 20,
			Group:     "email-group",
			Consumers: []string{"consumerA"},
		},
		Code: VerifyCode{
			TTL:      5 * duration.Minute,
			RetryTTL: duration.Minute,
		},
	},
	Jwt: Jwt{
		Issuer: "lobby",
		Access: AccessToken{
			Expire: 4 * duration.Hour,
			Delay:  10 * duration.Minute,
			Key:    "01J6EA2G4FSF9ABC218VFJ2B3C",
		},
		Refresh: RefreshToken{
			Expire: 144 * duration.Hour,
			Key:    "01J6EA3FKDDHTT9Q8Z5YKWHVCE",
		},
	},
}

DefaultConfig is the default configuration for application

Functions

func WriteTo

func WriteTo(filename string, app App) error

WriteTo save the app configuration to specified file

Types

type AccessToken

type AccessToken struct {
	Expire duration.Duration `toml:"expire" comment:"duration to expire access token"`
	Delay  duration.Duration `toml:"delay" comment:"delay duration after expiration"`
	Key    string            `toml:"key" comment:"access token signing key"`
}

type App

type App struct {
	Server Server   `toml:"server" comment:"http server configuration"`
	Log    Log      `toml:"log" comment:"server log configuration"`
	DB     DB       `toml:"db" comment:"database connection configuration"`
	Redis  Redis    `toml:"redis" comment:"redis connection configuration"`
	Email  Email    `toml:"email" comment:"email smtp client configuration"`
	Jwt    Jwt      `toml:"jwt" comment:"jwt secret configuration"`
	Meta   MetaInfo `toml:"-"`
}

App is configuration for the whole application

func ReadFrom

func ReadFrom(filename string) (App, error)

ReadFrom read app configuration from specified file

func Revise

func Revise(cfg App) (App, error)

Revise check the given configuration, if field value is zero then it will be overwritten by same filed value of DefaultConfig

type DB

type DB struct {
	Driver             string            `toml:"driver" comment:"sqlite | mysql | postgresql"`
	Address            string            `toml:"address" comment:"db internal host"`
	User               string            `toml:"user" comment:"db username"`
	Password           string            `toml:"password" comment:"db password"`
	Database           string            `toml:"database" comment:"database name"`
	Params             string            `toml:"param" comment:"connection params"`
	MaxIdleConnections int               `toml:"maxIdleConnections" comment:"max idle connections limit"`
	MaxOpenConnections int               `toml:"maxOpenConnections" comment:"max opening connections limit"`
	MaxLifeTime        duration.Duration `toml:"maxLifeTime" comment:"max connection lifetime"`
	MaxIdleTime        duration.Duration `toml:"maxIdleTime" comment:"max connection idle time"`
}

DB is configuration for database

type Email

type Email struct {
	Host     string     `toml:"host" comment:"smtp internal host"`
	SSL      bool       `toml:"ssl" comment:"use ssl port"`
	Port     int        `toml:"port" comment:"smtp internal port"`
	Username string     `toml:"username" comment:"smtp user name"`
	Password string     `toml:"password" comment:"password to authenticate"`
	Template string     `toml:"template" comment:"custom email template dir"`
	MQ       EmailMq    `toml:"-"`
	Code     VerifyCode `toml:"code" comment:"email verification code configuration"`
}

type EmailMq

type EmailMq struct {
	Topic     string   `toml:"topic" comment:"email mq topic"`
	BatchSize int64    `toml:"batchSize" comment:"max batch size of per reading"`
	Group     string   `toml:"group" comment:"consumer group"`
	Consumers []string `toml:"consumers" comment:"how many consumer in groups, must >=1."`
}

type Jwt

type Jwt struct {
	Issuer  string       `toml:"issuer" comment:"jwt issuer"`
	Access  AccessToken  `toml:"access" comment:"access token configuration"`
	Refresh RefreshToken `toml:"refresh" comment:"refresh token configuration"`
}

Jwt is configuration for jwt signing

type Log

type Log struct {
	Filename string     `toml:"filename" comment:"log output file"`
	Prompt   string     `toml:"-"`
	Level    slog.Level `toml:"level" comment:"support levels: DEBUG | INFO | WARN | ERROR"`
	Format   string     `toml:"format" comment:"TEXT or JSON"`
	Source   bool       `toml:"source" comment:"whether to show source file in logs"`
	Color    bool       `toml:"color" comment:"enable color log"`
}

Log is configuration for logging

type MetaInfo

type MetaInfo struct {
	AppName   string
	Author    string
	Version   string
	BuildTime string
}

MetaInfo for program

type RateLimit

type RateLimit struct {
	Public struct {
		Limit  int               `toml:"limit"`
		Window duration.Duration `toml:"window"`
	} `toml:"public"`
}

type Redis

type Redis struct {
	Address      string            `toml:"address" comment:"host address"`
	Password     string            `toml:"password" comment:"redis auth"`
	WriteTimeout duration.Duration `toml:"writeTimeout" comment:"Timeout for socket writes."`
	ReadTimeout  duration.Duration `toml:"readTimeout" comment:"Timeout for socket reads."`
}

Redis is configuration for redis internal

type RefreshToken

type RefreshToken struct {
	Expire duration.Duration `toml:"expire" comment:"duration to expire refresh token"`
	Key    string            `toml:"key" comment:"refresh token signing key"`
}

type Server

type Server struct {
	Address      string            `toml:"address" comment:"server bind address"`
	BasePath     string            `toml:"basepath" comment:"base path for api"`
	ReadTimeout  duration.Duration `toml:"readTimeout" comment:"the maximum duration for reading the entire request"`
	WriteTimeout duration.Duration `toml:"writeTimeout" comment:"the maximum duration before timing out writes of the response"`
	IdleTimeout  duration.Duration `toml:"idleTimeout" comment:"the maximum amount of time to wait for the next request when keep-alives are enabled"`
	MultipartMax int64             `toml:"multipartMax" comment:"value of 'maxMemory' param that is given to http.Request's ParseMultipartForm"`
	Pprof        bool              `toml:"pprof" comment:"enabled pprof program profiling"`
	Swagger      bool              `toml:"swagger" comment:"enable swagger documentation"`
	TLS          TLS               `toml:"tls" comment:"tls certificate"`
}

Server is configuration for the http server

type TLS

type TLS struct {
	Cert string `toml:"cert" comment:"tls certificate"`
	Key  string `toml:"key" comment:"tls key"`
}

type VerifyCode

type VerifyCode struct {
	TTL      duration.Duration `toml:"ttl" comment:"lifetime for verification code"`
	RetryTTL duration.Duration `toml:"retry" comment:"max wait time before asking for another new verification code"`
}

Jump to

Keyboard shortcuts

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