lib/app/root.go

87 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-01-10 18:16:20 +00:00
// Package app is a utility package
package app
import (
"strings"
2021-01-18 03:13:54 +00:00
"time"
2021-01-10 18:16:20 +00:00
2021-01-30 19:39:44 +00:00
"code.tnxs.net/taxnexus/lib/api/auth/auth_client"
2021-01-18 03:13:54 +00:00
"code.tnxs.net/taxnexus/lib/api/auth0/auth0_client"
2021-01-27 22:39:23 +00:00
"code.tnxs.net/taxnexus/lib/api/crm/crm_client"
"code.tnxs.net/taxnexus/lib/api/geo/geo_client"
2021-02-02 04:19:16 +00:00
"code.tnxs.net/taxnexus/lib/api/ledger/ledger_client"
2021-01-28 18:05:25 +00:00
"code.tnxs.net/taxnexus/lib/api/ops/ops_client"
2021-01-28 20:00:21 +00:00
"code.tnxs.net/taxnexus/lib/api/regs/regs_client"
2021-01-27 23:11:19 +00:00
"code.tnxs.net/taxnexus/lib/api/stash/stash_client"
2021-01-10 18:16:20 +00:00
"code.tnxs.net/taxnexus/lib/app/logger"
2021-01-30 19:39:44 +00:00
httptransport "github.com/go-openapi/runtime/client"
2021-01-10 18:16:20 +00:00
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var appViper = viper.New()
2021-01-18 03:13:54 +00:00
var auth0Client = auth0_client.Default
2021-01-30 19:39:44 +00:00
var authClient = auth_client.Default
var authUser *User
var config = Configuration{}
var configured = false
2021-01-27 22:39:23 +00:00
var crmClient = crm_client.Default
2021-01-30 19:39:44 +00:00
var geoClient = geo_client.Default
2021-02-02 04:19:16 +00:00
var ledgerClient = ledger_client.Default
2021-01-28 18:05:25 +00:00
var opsClient = ops_client.Default
2021-01-28 20:00:21 +00:00
var regsClient = regs_client.Default
2021-01-30 19:39:44 +00:00
var stashClient = stash_client.Default
var sugar *zap.SugaredLogger
2021-01-10 18:16:20 +00:00
2021-01-18 03:13:54 +00:00
const getTimeout = 6 * time.Minute
const postTimeout = 6 * time.Minute
const constNotFound = "not found"
2021-01-28 01:58:08 +00:00
const percentFactor = 100
2021-01-14 06:36:35 +00:00
const dateFormat = "2006-01-02"
const dateTimeFormat = "2006-01-02T15:04:05-0800"
const dateTimeFormatAlt = "2006-01-02 15:04:05"
const dateFormatAlt = "1/2/2006"
2021-01-27 22:39:23 +00:00
const countyGeocodeLength = 7
2021-01-30 19:39:44 +00:00
2021-02-05 22:04:17 +00:00
var maxTaxTypes = int64(2000) //nolint:gomnd // it's a var const
2021-01-30 19:39:44 +00:00
// const cityGeocodeLength = 12
2021-01-10 18:16:20 +00:00
// InitConfig exports the config initialization func
2021-01-27 21:50:15 +00:00
func InitConfig(systemName string, initalLogLevel zapcore.Level) {
2021-01-10 18:16:20 +00:00
if configured {
return
}
2021-01-27 21:50:15 +00:00
sugar = logger.New(initalLogLevel)
sugar.Infof("app.InitConfig: 📥 %s", systemName)
2021-01-10 18:16:20 +00:00
appViper.SetConfigType("yaml")
appViper.SetConfigName(systemName)
appViper.AddConfigPath("/etc/taxnexus")
appViper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
appViper.SetEnvPrefix("taxnexus")
appViper.AutomaticEnv() // read in environment variables that match
err := appViper.ReadInConfig()
if err != nil {
sugar.Fatalf("app.InitConfig: 💣 ⛔ can't read system config: %w", err)
}
err = appViper.Unmarshal(&config)
if err != nil {
sugar.Fatalf("app.InitConfig: 💣 ⛔ can't unmarshal system config: %w", err)
}
sugar = logger.New(GetLogLevel())
sugar.Debugf("app.InitConfig: 👍 📤 serviceAccounts: %d", len(config.ServiceAccounts))
configured = true
}
2021-01-30 19:39:44 +00:00
func apiUser(service string) *User {
serviceAccount := GetServiceAccount(service)
if serviceAccount == nil {
return nil
}
return &User{
APIKey: serviceAccount.APIKey,
Auth: httptransport.APIKeyAuth("X-API-Key", "header", serviceAccount.APIKey),
}
}