|
| 1 | +package configuration |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/microparts/configuration-golang" |
| 9 | + "github.com/microparts/errors-go" |
| 10 | + "github.com/microparts/logs-go" |
| 11 | + "gopkg.in/yaml.v2" |
| 12 | +) |
| 13 | + |
| 14 | +type connectionConfig struct { |
| 15 | + URL string `yaml:"url"` |
| 16 | + WSAP string `yaml:"wsap"` |
| 17 | + Originator string `yaml:"originator"` |
| 18 | + Password string `yaml:"password"` |
| 19 | + PasswordRaw string |
| 20 | + PinCode string `yaml:"pin_code"` |
| 21 | + VatURL string `yaml:"vat_url"` |
| 22 | + VatSOAPURL string `yaml:"vat_soap_url"` |
| 23 | + VatOfficeID string `yaml:"vat_office_id"` |
| 24 | + VatSign string `yaml:"vat_sign"` |
| 25 | +} |
| 26 | + |
| 27 | +type amadeusConfig struct { |
| 28 | + Connection connectionConfig `yaml:"connection"` |
| 29 | + MaxRecommendations int `yaml:"max_recommendations"` |
| 30 | +} |
| 31 | + |
| 32 | +type httpConfig struct { |
| 33 | + Host string `yaml:"host"` |
| 34 | + Port string `yaml:"port"` |
| 35 | +} |
| 36 | + |
| 37 | +type formatsConfig struct { |
| 38 | + Time string `yaml:"time"` |
| 39 | + Date string `yaml:"date"` |
| 40 | + XMLDate string `yaml:"xml_date"` |
| 41 | +} |
| 42 | + |
| 43 | +// CfgType service config structure |
| 44 | +type CfgType struct { |
| 45 | + Amadeus amadeusConfig `yaml:"amadeus"` |
| 46 | + HTTP httpConfig `yaml:"http"` |
| 47 | + Formats formatsConfig `yaml:"formats"` |
| 48 | + Queue queueConfig `yaml:"queue"` |
| 49 | + Log *logs.Config `yaml:"log"` |
| 50 | + Settings SettingsConfig `yaml:"settings"` |
| 51 | + Notifications NotificationsConfig `yaml:"notifications"` |
| 52 | +} |
| 53 | + |
| 54 | +type queueConfig struct { |
| 55 | + PubSub PubSubConfig `yaml:"pubsub"` |
| 56 | +} |
| 57 | + |
| 58 | +// PubSubConfig PubSub Config |
| 59 | +type PubSubConfig struct { |
| 60 | + Host string `yaml:"host"` |
| 61 | + ProjectID string `yaml:"project_id"` |
| 62 | + Topic string `yaml:"topic"` |
| 63 | + Subscription string `yaml:"subscription"` |
| 64 | + Key string `yaml:"key"` |
| 65 | +} |
| 66 | + |
| 67 | +// SettingsConfig Settings Config |
| 68 | +type SettingsConfig struct { |
| 69 | + FoidRequreAirlines []string `yaml:"foid_require_airlines"` |
| 70 | + RemoveDuplicateAirlines bool `yaml:"remove_duplicate_airlines"` |
| 71 | + MaxAttemptsCancelVoid int `yaml:"max_attempts_cancel_void"` |
| 72 | + MaxAttemptsDocIssuance int `yaml:"max_attempts_doc_issuance"` |
| 73 | + MaxAttemptsPNRRET int `yaml:"max_attempts_pnr_ret"` |
| 74 | + MaxAttemptsPNRADD int `yaml:"max_attempts_pnr_add"` |
| 75 | + IssueExpire int `yaml:"issue_expire"` |
| 76 | + BookingRequestsDelay int `yaml:"booking_requests_delay"` |
| 77 | + IssueRequestsDelay int `yaml:"issue_requests_delay"` |
| 78 | + FareRulesParagraphsToShow []string `yaml:"fare_rules_paragraphs_to_show"` |
| 79 | + FareQualifierList []string `yaml:"fare_qualifier_list"` |
| 80 | +} |
| 81 | + |
| 82 | +type NotificationsConfig struct { |
| 83 | + ErrorMessageEmail string `yaml:"error_message_email"` |
| 84 | + MailFrom string `yaml:"mail_from"` |
| 85 | + Queue PubSubConfig `yaml:"pubsub"` |
| 86 | +} |
| 87 | + |
| 88 | +var ( |
| 89 | + // Provider is current service |
| 90 | + Provider = "amadeus" |
| 91 | +) |
| 92 | + |
| 93 | +var Config *CfgType |
| 94 | + |
| 95 | +// InitConfig initialize config data |
| 96 | +func InitConfig() error { |
| 97 | + configPath := config.GetEnv("CONFIG_PATH", "") |
| 98 | + configBytes, err := config.ReadConfigs(configPath) |
| 99 | + if errors.HasErrors(err) { |
| 100 | + log.Printf("[config] read error: %+v", err) |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + err = yaml.Unmarshal(configBytes, &Config) |
| 105 | + if errors.HasErrors(err) { |
| 106 | + log.Printf("[config] unmarshal error for bytes: %+v", configBytes) |
| 107 | + return err |
| 108 | + } |
| 109 | + // |
| 110 | + // structs.TimeFormat = func() string { |
| 111 | + // return Config.Formats.Time |
| 112 | + // } |
| 113 | + // |
| 114 | + // structs.DateFormat = func() string { |
| 115 | + // return Config.Formats.Date |
| 116 | + // } |
| 117 | + |
| 118 | + if pwdBytes, err := base64.StdEncoding.DecodeString(Config.Amadeus.Connection.Password); err == nil { |
| 119 | + Config.Amadeus.Connection.PasswordRaw = string(pwdBytes) |
| 120 | + } else { |
| 121 | + log.Println("Error decoding password. Is it not encrypted in bass64?") |
| 122 | + os.Exit(-1) |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
0 commit comments