Documentation
¶
Index ¶
- Constants
- func As[T any](types ...any) any
- func From[T any]() any
- func LoadJSONConfig(p LoadJSONConfigParams) error
- func LoadJSONConfigInto(cfg any, automaticEnv bool, defaultCfgPath string) error
- func NewGlobalSlogLogger(p SlogLoggerParams) (*slog.Logger, error)
- func NewModule() fx.Option
- func UseSlogLogger() fx.Option
- type CoreConfig
- type CoreEnv
- func (e CoreEnv) AppAutomaticEnvValue() bool
- func (e CoreEnv) AppConfigLocationValue() (string, error)
- func (e CoreEnv) AppNameValue() string
- func (e CoreEnv) AppVersionValue() string
- func (e CoreEnv) IsProd() bool
- func (e CoreEnv) LogFormatValue() string
- func (e CoreEnv) LogLevelValue() string
- func (e CoreEnv) ProfileValue() string
- func (e CoreEnv) RequiredValues() []any
- type LoadJSONConfigParams
- type SentryConfig
- type SentryEnv
- type SlogLoggerParams
Constants ¶
const ( ConfigFolder = "configs" ConfigFile = "app.json" )
const ( ProfileProduction = "production" ProfileDevelopment = "development" ProfileDebug = "debug" )
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v1.1.0
As register already registered type T under multiple interfaces. Useful if you need a single required object to provide multiple required types. This method allows you to inject the original object, and all type it registered by this function.
func From ¶ added in v1.1.0
From create a function that accepts and return self. This method can be used with other As... methods of multiple fx packages when you want to keep both the original type and annotated type after annotated. For example, fx.Provide(newMyService, AsInterface(From[*myService])).
func LoadJSONConfig ¶
func LoadJSONConfig(p LoadJSONConfigParams) error
LoadJSONConfig load config into CoreConfig.
func LoadJSONConfigInto ¶
LoadJSONConfigInto load json config into cfg pointer.
func NewGlobalSlogLogger ¶
func NewGlobalSlogLogger(p SlogLoggerParams) (*slog.Logger, error)
NewGlobalSlogLogger create a logger instance and register it globally.
func NewModule ¶
NewModule Create a module that autoconfigure slog, sentry and populate configuration from file or environment. The env config object must implement CoreConfig, and registered using AsConfigFor to be autopopulated. The env config must also register as SentryConfig to enable sentry feature.
func UseSlogLogger ¶
UseSlogLogger configure fx to use slog.Default logger.
Types ¶
type CoreConfig ¶
type CoreConfig interface {
// AppNameValue application name.
AppNameValue() string
// AppVersionValue application version.
AppVersionValue() string
// AppConfigLocationValue base config file to load from.
// Config file must in JSON format.
// Return empty string to disable loading from a config file.
// Default implementations read config from file:./configs/app.json.
AppConfigLocationValue() (string, error)
// AppAutomaticEnvValue enable read env variable into config struct automatically.
AppAutomaticEnvValue() bool
// ProfileValue application env profile (production,development,debug).
ProfileValue() string
// RequiredValues the list of field that must specify.
// This method must return a list of pointers to specified field on the same object.
RequiredValues() []any
// LogLevelValue application log level.
LogLevelValue() string
// LogFormatValue the format of log, accept "text", "json"
LogFormatValue() string
// IsProd shorthand production profile checking.
IsProd() bool
}
type CoreEnv ¶
type CoreEnv struct {
AppName string `json:"app_name" mapstructure:"app_name"`
AppVersion string `json:"app_version" mapstructure:"app_version"`
LogLevel string `json:"log_level" mapstructure:"log_level"`
Profile string `json:"profile" mapstructure:"profile"`
SentryEnv
}
nolint:staticcheck
func (CoreEnv) AppAutomaticEnvValue ¶
func (CoreEnv) AppConfigLocationValue ¶
func (CoreEnv) AppNameValue ¶
func (CoreEnv) AppVersionValue ¶
func (CoreEnv) LogFormatValue ¶ added in v1.1.0
func (CoreEnv) LogLevelValue ¶
func (CoreEnv) ProfileValue ¶
func (CoreEnv) RequiredValues ¶
type LoadJSONConfigParams ¶
type LoadJSONConfigParams struct {
fx.In
Config CoreConfig
}
type SentryConfig ¶
type SentryEnv ¶
type SentryEnv struct {
SentryDsn string `json:"sentry_dsn" mapstructure:"sentry_dsn"`
SentryLogLevel string `json:"sentry_log_level" mapstructure:"sentry_log_level"`
}
func (SentryEnv) SentryDsnValue ¶
func (SentryEnv) SentryLogLevelValue ¶
type SlogLoggerParams ¶
type SlogLoggerParams struct {
fx.In
Config CoreConfig
LogConfig SentryConfig `optional:"true"`
Lifecycle fx.Lifecycle
}