lib/app/root.go

94 lines
2.7 KiB
Go
Raw 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-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-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-19 01:50:45 +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 sugar *zap.SugaredLogger
2021-01-10 21:23:12 +00:00
var config = Configuration{}
2021-01-10 18:16:20 +00:00
var appViper = viper.New()
2021-01-18 03:13:54 +00:00
var auth0Client = auth0_client.Default
2021-01-27 22:39:23 +00:00
var geoClient = geo_client.Default
var crmClient = crm_client.Default
2021-01-27 23:11:19 +00:00
var stashClient = stash_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-18 03:13:54 +00:00
var configured = false
2021-01-19 01:50:45 +00:00
var apiUsers map[string]User
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
const dateFormatDB = "2006-01-02"
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
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())
2021-01-19 01:50:45 +00:00
apiUsers = initAPIUsers()
2021-01-10 18:16:20 +00:00
sugar.Debugf("app.InitConfig: 👍 📤 serviceAccounts: %d", len(config.ServiceAccounts))
configured = true
}
2021-01-18 03:13:54 +00:00
2021-01-19 01:50:45 +00:00
func initAPIUsers() map[string]User {
2021-01-28 19:17:39 +00:00
sugar.Info("app.initAPIUsers: 📥")
2021-01-19 01:50:45 +00:00
users := map[string]User{
"auth": {
APIKey: GetServiceAccount("auth").APIKey,
Auth: httptransport.APIKeyAuth(
"X-API-Key",
"header",
GetServiceAccount("auth").APIKey,
),
},
"test-service-account": {
APIKey: GetServiceAccount("test-service-account").APIKey,
Auth: httptransport.APIKeyAuth(
"X-API-Key",
"header",
GetServiceAccount("test-service-account").APIKey),
},
}
2021-01-28 19:17:39 +00:00
sugar.Debugf("app.initAPIUsers: 👍 📤")
2021-01-19 01:50:45 +00:00
return users
2021-01-18 03:13:54 +00:00
}