Closes #20 Reviewed-on: #21 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
|
|
auxcfg "git.kundeng.us/phoenix/textsender-models/tx0/config/auxiliary"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
const App_Name = "catapult"
|
|
|
|
type App struct {
|
|
ApiUrl string
|
|
AuthUrl string
|
|
ServiceUsername string
|
|
ServicePassphrase string
|
|
TwilioConfig *auxcfg.TwilioConfig
|
|
}
|
|
|
|
func Load() (*App, error) {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
cwd, _ := os.Getwd()
|
|
envPath := path.Join(cwd, "../..", ".env")
|
|
if err = godotenv.Load(envPath); err != nil {
|
|
prevPath := path.Join(envPath, "../..", ".env")
|
|
if err = godotenv.Load(prevPath); err != nil {
|
|
return nil, fmt.Errorf("Error loading .env file: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
apiUrl := os.Getenv("API_URL")
|
|
authUrl := os.Getenv("AUTH_URL")
|
|
serviceUsername := os.Getenv("SERVICE_USERNAME")
|
|
servicePassphrase := os.Getenv("SERVICE_PASSPHRASE")
|
|
|
|
if len(apiUrl) == 0 {
|
|
return nil, fmt.Errorf("Api Url not provided")
|
|
} else if len(authUrl) == 0 {
|
|
return nil, fmt.Errorf("Auth url not provided")
|
|
} else if len(serviceUsername) == 0 {
|
|
return nil, fmt.Errorf("Service username not provided")
|
|
} else if len(servicePassphrase) == 0 {
|
|
return nil, fmt.Errorf("Service passphrase not provided")
|
|
} else {
|
|
if cfg, err := loadTwilioConfig(); err != nil {
|
|
return nil, err
|
|
} else {
|
|
return &App{
|
|
ApiUrl: apiUrl, AuthUrl: authUrl, ServiceUsername: serviceUsername, ServicePassphrase: servicePassphrase, TwilioConfig: cfg,
|
|
}, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadTwilioConfig() (*auxcfg.TwilioConfig, error) {
|
|
authSid := os.Getenv("TWILIO_AUTH_SID")
|
|
serviceSid := os.Getenv("TWILIO_SERVICE_SID")
|
|
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
|
|
phoneNumber := os.Getenv("TWILIO_PHONE_NUMBER")
|
|
|
|
if len(authSid) == 0 {
|
|
return nil, fmt.Errorf("Twilio config auth sid not provided")
|
|
} else if len(serviceSid) == 0 {
|
|
return nil, fmt.Errorf("Twilio config service sid not provided")
|
|
} else if len(authToken) == 0 {
|
|
return nil, fmt.Errorf("Twilio config token not provided")
|
|
} else if len(phoneNumber) == 0 {
|
|
return nil, fmt.Errorf("Twilio config phone number not provided")
|
|
} else {
|
|
cfg := auxcfg.TwilioConfig{}
|
|
cfg.AccountSID = authSid
|
|
cfg.AuthToken = authToken
|
|
cfg.ServiceSID = serviceSid
|
|
cfg.Number = phoneNumber
|
|
return &cfg, nil
|
|
}
|
|
}
|