lib/app/config.go

197 lines
6.6 KiB
Go
Raw Permalink Normal View History

2021-01-10 18:16:20 +00:00
package app
import (
"time"
"code.tnxs.net/taxnexus/lib/app/logger"
"go.uber.org/zap/zapcore"
)
2021-01-10 21:23:12 +00:00
// Worker describes how many service workers to launch
2021-01-10 18:16:20 +00:00
type Worker struct {
2021-01-24 21:45:57 +00:00
ChannelSize int64 `mapstructure:"channel_size,omitempty"`
WorkerCount int `mapstructure:"worker_count,omitempty"`
2021-01-10 18:16:20 +00:00
}
2021-01-10 21:23:12 +00:00
// CacheSize describes how to chunkify resources
type CacheSize struct {
Increment int64
}
// Metrics defines the if and where to collect data
type Metrics struct {
2021-01-10 18:16:20 +00:00
Address string `mapstructure:"address,omitempty"`
Enabled bool `mapstructure:"enabled,omitempty"`
}
2021-01-10 21:23:12 +00:00
// Endpoint describes a web URL as a service endpoint
type Endpoint struct {
2021-01-10 18:16:20 +00:00
BasePath string `mapstructure:"base_path,omitempty"`
Host string `mapstructure:"host,omitempty"`
Scheme string `mapstructure:"scheme,omitempty"`
}
2021-01-10 21:23:12 +00:00
// Chunk defines cache chunk size
type Chunk struct {
2021-01-10 18:16:20 +00:00
size int32 `mapstructure:"size,omitempty"`
}
2021-01-10 21:23:12 +00:00
// ServiceAccount defines and external service
2021-01-10 18:16:20 +00:00
type ServiceAccount struct {
APIKey string `mapstructure:"api_key,omitempty"`
ApplicationName string `mapstructure:"application_name,omitempty"`
AuthenticationType string `mapstructure:"authentication_type,omitempty"`
BackendID string `mapstructure:"backend_id,omitempty"`
ClientID string `mapstructure:"client_id,omitempty"`
ClientSecret string `mapstructure:"client_secret,omitempty"`
DSN string `mapstructure:"dsn,omitempty"`
2021-01-10 21:23:12 +00:00
Endpoint Endpoint `mapstructure:"endpoint,omitempty"`
2021-01-10 18:16:20 +00:00
Password string `mapstructure:"password,omitempty"`
Production bool `mapstructure:"production,omitempty"`
SecurityToken string `mapstructure:"security_token,omitempty"`
Credentials string `mapstructure:"credentials,omitempty"`
Timeout time.Duration `mapstructure:"timeout,omitempty"`
Type string `mapstructure:"type,omitempty"`
Username string `mapstructure:"username,omitempty"`
Vendor string `mapstructure:"vendor,omitempty"`
}
2021-01-10 21:23:12 +00:00
// Configuration defines the config struct
type Configuration struct {
2021-01-10 18:16:20 +00:00
AppName string `mapstructure:"app_name,omitempty"`
BackendID string `mapstructure:"backend_id,omitempty"`
BuildEnv string `mapstructure:"build_env,omitempty"`
2021-01-10 22:27:09 +00:00
CacheSizes map[string]CacheSize `mapstructure:"cache_sizes,omitempty"`
2021-01-10 18:16:20 +00:00
ClusterID string `mapstructure:"cluster_id,omitempty"`
2021-01-10 21:23:12 +00:00
Chunks map[string]Chunk `mapstructure:"chunks,omitempty"`
2021-01-10 18:16:20 +00:00
DBMS string `mapstructure:"dbms,omitempty"`
DBMSHost string `mapstructure:"dbms_host,omitempty"`
DBMSName string `mapstructure:"dbms_name,omitempty"`
DBMSPassword string `mapstructure:"dbms_password,omitempty"`
DBMSUsername string `mapstructure:"dbms_username,omitempty"`
DSN string `mapstructure:"dsn,omitempty"`
2021-01-10 21:23:12 +00:00
Endpoint Endpoint `mapstructure:"endpoint,omitempty"`
2021-01-10 18:16:20 +00:00
Environment string `mapstructure:"environment,omitempty"`
GelfURI string `mapstructure:"gelf_uri,omitempty"`
GitHubOrgName string `mapstructure:"git_hub_org_name,omitempty"`
LogLevel int `mapstructure:"log_level,omitempty"`
2021-01-10 21:23:12 +00:00
Metrics Metrics `mapstructure:"metrics,omitempty"`
2021-01-10 18:16:20 +00:00
KafkaServer string `mapstructure:"kafka_server,omitempty"`
PDFRenderEngine string `mapstructure:"pdf_render_engine,omitempty"`
RegistryPriv string `mapstructure:"registry_priv,omitempty"`
RegistryPublic string `mapstructure:"registry_public,omitempty"`
RepoName string `mapstructure:"repo_name,omitempty"`
ServiceAccounts map[string]ServiceAccount `mapstructure:"service_accounts,omitempty"`
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout,omitempty"`
Version string `mapstructure:"version,omitempty"`
Workers map[string]Worker `mapstructure:"workers,omitempty"`
}
2021-01-10 22:27:09 +00:00
// GetCacheSize returns the named cache size
func GetCacheSize(name string) int64 {
obj, ok := config.CacheSizes[name]
if ok {
return obj.Increment
}
sugar.Warnf("app.GetCacheSize: unknown cache size identifier: %s", name)
return 0
}
2021-01-10 18:16:20 +00:00
// GetPDFRenderEngine returns a config file parameter
func GetPDFRenderEngine() string {
return config.PDFRenderEngine
}
// GetKafkaServer returns a config file parameter
func GetKafkaServer() string {
if config.KafkaServer != "" {
return config.KafkaServer
}
return "nats.fabric.tnxs.net"
}
// GetChunkSize returns a config file parameter
func GetChunkSize(chunkName string) int {
obj, ok := config.Chunks[chunkName]
if ok {
return int(obj.size)
}
return 0
}
// GetMetricsAddress return the metrics Address value
func GetMetricsAddress() string {
return config.Metrics.Address
}
// IsMetrics returns TRUE if metrics enabled
func IsMetrics() bool {
return config.Metrics.Enabled
}
// GetWorker returns the named worker record
func GetWorker(name string) *Worker {
worker, ok := config.Workers[name]
if ok {
return &worker
}
return nil
}
// GetServiceAccount returns the named service account struct
func GetServiceAccount(name string) *ServiceAccount {
serviceaccount, ok := config.ServiceAccounts[name]
if ok {
return &serviceaccount
}
return nil
}
// GetAppName retrieves the Taxnexus App Name for this microservice
func GetAppName() string {
return config.AppName
}
// GetEnvironment retrieves the current runtime environment
func GetEnvironment() string {
return config.Environment
}
// GetBackendID retrieves the backend ID
func GetBackendID() string {
return config.BackendID
}
// GetClusterID retrieves the cluster ID
func GetClusterID() string {
return config.ClusterID
}
// GetDSN retrieves the Taxnexus database DSN for this microservice
func GetDSN() string {
return config.DSN
}
// GetDBMS retrieves the DBMS string
func GetDBMS() string {
return config.DBMS
}
// GetLogLevel returns the Log Level
func GetLogLevel() zapcore.Level {
switch config.LogLevel {
case int(logger.DebugLevel):
return logger.DebugLevel
case int(logger.InfoLevel):
return logger.InfoLevel
case int(logger.WarnLevel):
return logger.WarnLevel
case int(logger.ErrorLevel):
return logger.ErrorLevel
case int(logger.PanicLevel):
return logger.PanicLevel
default:
return logger.DebugLevel
}
}