Closes #54 Reviewed-on: phoenix/textsender-api#56 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
169 lines
3.7 KiB
Go
169 lines
3.7 KiB
Go
package config
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.kundeng.us/phoenix/textsender-models/tx0/config"
|
|
"github.com/joho/godotenv"
|
|
|
|
"git.kundeng.us/phoenix/textsender-api/internal/version"
|
|
)
|
|
|
|
type Config struct {
|
|
DBConnString string
|
|
ServerPort string
|
|
ResetDB bool
|
|
JWTSecret string `env:"JWT_SECRET" required:"true"`
|
|
TwilioConfig *config.TwilioConfig
|
|
AllowedOrigins []string
|
|
}
|
|
|
|
type ConnectionInfo struct {
|
|
Username string
|
|
Password string
|
|
Database string
|
|
Host string
|
|
Port int
|
|
SslMode string
|
|
}
|
|
|
|
const PORT = "8080"
|
|
const APP_NAME = "textsender-api"
|
|
|
|
func PortString() string {
|
|
portMessage := fmt.Sprintf(":%s", PORT)
|
|
|
|
return portMessage
|
|
}
|
|
|
|
func (ci ConnectionInfo) Parse() string {
|
|
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", ci.Username, ci.Password, ci.Host, ci.Port, ci.Database, ci.SslMode)
|
|
}
|
|
|
|
func PrintName() {
|
|
fmt.Println(APP_NAME)
|
|
fmt.Println(version.String())
|
|
}
|
|
|
|
func Load() (*Config, *config.TwilioConfig, error) {
|
|
versionFlag := flag.Bool("version", false, "Print version information")
|
|
resetDb := flag.Bool("reset-db", false, "Reset the database schema and exit")
|
|
port := flag.String("port", PORT, "Server port")
|
|
flag.Parse()
|
|
|
|
if *versionFlag {
|
|
fmt.Println(version.String())
|
|
os.Exit(-1)
|
|
}
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
return nil, nil, fmt.Errorf("Error loading .env file")
|
|
}
|
|
|
|
unpackedConnString := UnpackDBConnString()
|
|
dbConnString := unpackedConnString.Parse()
|
|
|
|
if cfg, err := TwilioConfig(); err != nil {
|
|
return nil, nil, err
|
|
} else {
|
|
allowedOrigins := unpackAllowedOrigins()
|
|
return &Config{
|
|
DBConnString: dbConnString,
|
|
ServerPort: *port,
|
|
ResetDB: *resetDb,
|
|
JWTSecret: os.Getenv("JWT_SECRET"),
|
|
AllowedOrigins: allowedOrigins,
|
|
}, cfg, nil
|
|
}
|
|
}
|
|
|
|
func UnpackDBConnString() (connInfo ConnectionInfo) {
|
|
username := os.Getenv("DB_USER")
|
|
password := os.Getenv("DB_PASSWORD")
|
|
host := os.Getenv("DB_HOST")
|
|
port := os.Getenv("DB_PORT")
|
|
database := os.Getenv("DB_NAME")
|
|
sslMode := os.Getenv("DB_SSLMODE")
|
|
|
|
if username != "" {
|
|
connInfo.Username = username
|
|
} else {
|
|
connInfo.Username = "user"
|
|
}
|
|
|
|
if password != "" {
|
|
connInfo.Password = password
|
|
} else {
|
|
connInfo.Password = "password"
|
|
}
|
|
|
|
if host != "" {
|
|
connInfo.Host = host
|
|
} else {
|
|
connInfo.Host = "localhost"
|
|
}
|
|
|
|
if port != "" {
|
|
num, err := strconv.Atoi(port)
|
|
if err != nil {
|
|
return
|
|
}
|
|
connInfo.Port = num
|
|
} else {
|
|
connInfo.Port = 5432
|
|
}
|
|
|
|
if database != "" {
|
|
connInfo.Database = database
|
|
} else {
|
|
connInfo.Database = "textsender_db"
|
|
}
|
|
|
|
if sslMode != "" {
|
|
connInfo.SslMode = sslMode
|
|
} else {
|
|
connInfo.SslMode = "disable"
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func unpackAllowedOrigins() []string {
|
|
allowedOriginsRaw := os.Getenv("ALLOWED_ORIGINS")
|
|
allowedOriginsSplit := strings.Split(allowedOriginsRaw, ",")
|
|
return allowedOriginsSplit
|
|
}
|
|
|
|
func TwilioConfig() (*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 account sid not found")
|
|
} else if len(serviceSid) == 0 {
|
|
return nil, fmt.Errorf("Twilio config service sid not found")
|
|
} else if len(authToken) == 0 {
|
|
return nil, fmt.Errorf("Twilio config auth token not found")
|
|
} else if len(phoneNumber) == 0 {
|
|
return nil, fmt.Errorf("Twilio config phone number not found")
|
|
} else {
|
|
var cfg config.TwilioConfig
|
|
cfg.AccountSID = authSid
|
|
cfg.ServiceSID = serviceSid
|
|
cfg.AuthToken = authToken
|
|
cfg.Number = phoneNumber
|
|
|
|
return &cfg, nil
|
|
}
|
|
}
|
|
|
|
func (c *Config) GetDBConnString() string {
|
|
return c.DBConnString
|
|
}
|