/
gagg.go
312 lines (246 loc) · 8.33 KB
/
gagg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"fmt"
"github.com/nerfmiester/gagg/Config"
//"github.com/nerfmiester/gagg/Structs"
"log"
"github.com/streadway/amqp"
"github.com/ivpusic/toml"
"flag"
"os"
"net/http"
"io/ioutil"
"time"
"strconv"
"math/rand"
"errors"
"net/url"
)
var tomlConfig Config.TomlConfig
var name = flag.String("name", "World", "A name to say hello to")
var meta bool
var health bool
var address string
var debug bool
var shootme bool
var (
uri = flag.String("uri", "amqp://guest:guest@localhost:5672/", "AMQP URI")
exchangeName = flag.String("exchange", "test-exchange", "Durable AMQP exchange name")
exchangeType = flag.String("exchange-type", "direct", "Exchange type - direct|fanout|topic|x-custom")
routingKey = flag.String("key", "test-key", "AMQP routing key")
body = flag.String("body", "foobar", "Body of message")
reliable = flag.Bool("reliable", true, "Wait for the publisher confirmation before exiting")
getFlights bool
)
var arrivalDate string
var departureDate string
var from string
var to string
var date time.Time
var port string
var Url *url.URL
var channel string
var channelMap = make(map[string]string)
func init() {
date = time.Now()
flag.BoolVar(&health, "health", false, "Get meta endpoitnt.")
flag.BoolVar(&meta, "meta", false, "Get meta endpoitnt.")
flag.BoolVar(&meta, "m", false, "Geta meta endpoint.")
flag.BoolVar(&health, "h", false, "Geta meta endpoint.")
flag.BoolVar(&getFlights, "f", false, "Use json language.")
flag.BoolVar(&getFlights, "flights", false, "Use TOML language.")
flag.BoolVar(&debug, "d", false, "Debug mode.")
flag.BoolVar(&debug, "debug", false, "debug mode.")
flag.BoolVar(&shootme, "shoot", false, "shoot me.")
flag.StringVar(&address,"url", "http://public-api.agg.pre1.gb.tsm.internal","host url")
flag.StringVar(&port,"p", "8080","Host port to use, default is non prod envs.")
flag.StringVar(&port,"port", "8080","Host port to use, default is non prod envs.")
flag.StringVar(&departureDate,"dd", "2006-01-02","Departure date")
flag.StringVar(&arrivalDate,"ad", "2006-01-02","Arival date")
flag.StringVar(&from,"from", "MAN","From IATA code")
flag.StringVar(&to,"to", "CPH","To IATA code")
flag.StringVar(&channel, "c" , "f", "channel f=flights, c=carhire, h=hotels, l=holidays")
if (departureDate=="2006-01-02") {
departureDate = date.Format("2006-01-02")
}
rand.Seed(time.Now().UnixNano())
channelMap["f"]="flights"
channelMap["c"]="carhire"
channelMap["h"]="hotels"
channelMap["l"]="holidays"
}
func main() {
flag.Parse()
if (shootme) {
fmt.Println("Bang..........")
fmt.Printf("Date as string........%s-%d-%s\n", strconv.Itoa(date.Year()) ,date.Month(),strconv.Itoa(date.Day()))
fmt.Printf("From is .........%s\n", departureDate)
if (arrivalDate=="2006-01-02") {
fourteenDays := time.Hour * 24 * 14
parsedDate, err := time.Parse("2006-01-02", departureDate)
if err != nil {
fmt.Println(err)
}
arrivalDate = parsedDate.Add(fourteenDays).Format("2006-01-02")
}
fmt.Printf("To is .........%s\n", arrivalDate)
fmt.Printf("A random user is ......... %s\n", RandStringBytes(18))
err, channelString := channelLookup(channel)
if (err != nil) {
fmt.Printf("Your channel value .........%s , is bullshit use f=flights, c=carhire, h=hotels, l=holidays\n", channel)
os.Exit(1)
}
Url, err = url.Parse(address + ":" + port + "/")
if err != nil {
panic("boom")
}
//curl http://localhost:8080/gb/flights/v1/search/MAN/2015-11-22/DUB/2015-11-29/2/0/0/e/0/false\?return=true\&userId=hgjfkdshgdfs\&source=TIV
Url.Path += "gb/" + channelString + "v1/search/" + from + "/" + departureDate + "/" + to +"/" + arrivalDate + "/2/0/0/e/0/false"
parameters := url.Values{}
parameters.Add("return", "true")
parameters.Add("userId", RandStringBytes(18))
parameters.Add("source", "TIV")
Url.RawQuery = parameters.Encode()
fmt.Printf("Encoded URL is %q\n", Url.String())
if resp, err := http.Get(Url.String()); err != nil {
failOnError(err, "couldn't get url")
} else {
robots, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", robots)
}
os.Exit(0)
}
fmt.Println("Hello")
getToml()
if resp, err := http.Get(Url.String()); err != nil {
failOnError(err, "couldn't get url")
} else {
robots, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", robots)
}
if tomlConfig.Agg.MessageSend {
if err := publishMessage(*uri, *exchangeName, *exchangeType, *routingKey, *body, *reliable); err != nil {
failOnError(err, "Failed to send message")
} else {
fmt.Printf("Result: success\n")
}
}
}
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func publishMessage(amqpURI, exchange, exchangeType, routingKey, body string, reliable bool) error {
// This function dials, connects, declares, publishes, and tears down,
// all in one go. In a real service, you probably want to maintain a
// long-lived connection as state, and publish against that.
log.Printf("dialing %q", amqpURI)
connection, err := amqp.Dial(amqpURI)
if err != nil {
return fmt.Errorf("Dial: %s", err)
}
defer connection.Close()
log.Printf("got Connection, getting Channel")
channel, err := connection.Channel()
if err != nil {
return fmt.Errorf("Channel: %s", err)
}
log.Printf("got Channel, declaring %q Exchange (%q)", exchangeType, exchange)
if err := channel.ExchangeDeclare(
exchange, // name
exchangeType, // type
true, // durable
false, // auto-deleted
false, // internal
false, // noWait
nil, // arguments
); err != nil {
return fmt.Errorf("Exchange Declare: %s", err)
}
// Reliable publisher confirms require confirm.select support from the
// connection.
if reliable {
log.Printf("enabling publishing confirms.")
if err := channel.Confirm(false); err != nil {
return fmt.Errorf("Channel could not be put into confirm mode: %s", err)
}
confirms := channel.NotifyPublish(make(chan amqp.Confirmation, 1))
defer confirmOne(confirms)
}
log.Printf("declared Exchange, publishing %dB body (%q)", len(body), body)
if err = channel.Publish(
exchange, // publish to an exchange
routingKey, // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentType: "text/plain",
ContentEncoding: "",
Body: []byte(body),
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent
Priority: 0, // 0-9
// a bunch of application/implementation-specific fields
},
); err != nil {
return fmt.Errorf("Exchange Publish: %s", err)
}
return nil
}
// One would typically keep a channel of publishings, a sequence number, and a
// set of unacknowledged sequence numbers and loop until the publishing channel
// is closed.
func confirmOne(confirms <-chan amqp.Confirmation) {
log.Printf("waiting for confirmation of one publishing")
if confirmed := <-confirms; confirmed.Ack {
log.Printf("confirmed delivery with delivery tag: %d", confirmed.DeliveryTag)
} else {
log.Printf("failed delivery of delivery tag: %d", confirmed.DeliveryTag)
}
}
func getToml() {
if _, err := toml.DecodeFile("./Config/config.toml", &tomlConfig); err != nil {
log.Fatal(err)
// return
}
if (tomlConfig.Debug.Enabled) {
fmt.Printf("Title: %s\n", tomlConfig.Title)
fmt.Printf("Owner: %s (%s, %s), Born: %s\n",
tomlConfig.Owner.Name, tomlConfig.Owner.Org, tomlConfig.Owner.Bio,
tomlConfig.Owner.DOB)
for serverName, server := range tomlConfig.Servers {
fmt.Printf("Server: %s (%s, %s)\n", serverName, server.IP, server.DC)
}
fmt.Printf("Client data: %v\n", tomlConfig.Clients.Data)
fmt.Printf("Client hosts: %v\n", tomlConfig.Clients.Hosts)
fmt.Printf("Agg: %s, \n", tomlConfig.Agg.Server)
for i, port := range tomlConfig.Agg.Ports {
fmt.Printf("Port %d is %d\n", i, port)
}
}
}
func RandStringBytes(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func channelLookup(channel string) (err error, channelstring string) {
if _, ok := channelMap[channel] ; ok == true {
return nil, channelMap[channel]
} else {
return errors.New("No strings supplied"),""
}
}