Files
catapult/internal/app/app.go
T
phoenixandphoenix 7ec1f58f6a message_event_response-changes (#19)
Reviewed-on: #19
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-12-22 22:35:48 +00:00

82 lines
2.2 KiB
Go

package app
import (
"fmt"
"os"
"path"
"git.kundeng.us/phoenix/textsender-models/tx0/config"
"github.com/joho/godotenv"
)
const App_Name = "catapult"
type App struct {
ApiUrl string
AuthUrl string
ServiceUsername string
ServicePassphrase string
TwilioConfig *config.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() (*config.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 := config.TwilioConfig{}
cfg.AccountSID = authSid
cfg.AuthToken = authToken
cfg.ServiceSID = serviceSid
cfg.Number = phoneNumber
return &cfg, nil
}
}