lib/app/root.go

80 lines
2.2 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-18 03:13:54 +00:00
"code.tnxs.net/taxnexus/lib/api/auth0/auth0_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
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-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-10 18:16:20 +00:00
// InitConfig exports the config initialization func
func InitConfig(systemName string) {
if configured {
return
}
sugar = logger.New(zapcore.InfoLevel)
sugar.Info("app.InitConfig: 📥 %s", systemName)
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 {
sugar.Info("board.initAPIUsers: 📥")
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),
},
}
sugar.Debugf("board.initAPIUsers: 👍 📤")
return users
2021-01-18 03:13:54 +00:00
}