-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
445 lines (369 loc) · 13.2 KB
/
main.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/limiter"
)
func main() {
app := fiber.New()
limiterMiddleware := limiter.New(limiter.Config{
Max: 300, // Maximum number of requests
Expiration: 24 * time.Hour, // Expiration duration for the rate limiter
KeyGenerator: func(c *fiber.Ctx) string { // Custom key generator function
return c.IP() + "#" + c.Path()
},
})
// Endpoint to run the planets binary
app.Get("/run-planets", limiterMiddleware, func(c *fiber.Ctx) error {
debugParam := c.Query("debug")
debug := false
if debugParam == "true" {
debug = true
}
birthdate := c.Query("birthdate")
utctime := c.Query("utctime")
latitude := c.Query("latitude")
longitude := c.Query("longitude")
altitude := c.Query("altitude")
housesystem := c.Query("housesystem")
formattedDate, err := parseBirthdate(birthdate)
if err != nil {
return c.Status(http.StatusBadRequest).JSON(map[string]string{
"error": fmt.Sprintf("Failed to parse birthdate: %v", err),
})
}
args := fmt.Sprintf("-b%s -utc%s -p0123456t -fPlbs -hsy%s -geopos%s,%s,%s -g\",\" -head", formattedDate, utctime, housesystem, longitude, latitude, altitude)
output, err := runBinary("swetest", args, debug)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to run planets binary: %v", err),
})
}
parsedOutput, err := parsePlanetsOutput(output)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to parse planets binary output: %v", err),
})
}
if debug {
fmt.Println("Planet result:", parsedOutput)
}
return c.JSON(parsedOutput)
})
// Endpoint to run the houses binary
app.Get("/run-houses", limiterMiddleware, func(c *fiber.Ctx) error {
debugParam := c.Query("debug")
debug := false
if debugParam == "true" {
debug = true
}
birthdate := c.Query("birthdate")
utctime := c.Query("utctime")
latitude := c.Query("latitude")
longitude := c.Query("longitude")
altitude := c.Query("altitude")
housesystem := c.Query("housesystem")
formattedDate, err := parseBirthdate(birthdate)
if err != nil {
return c.Status(http.StatusBadRequest).JSON(map[string]string{
"error": fmt.Sprintf("Failed to parse birthdate: %v", err),
})
}
args := fmt.Sprintf("-house -p -fPlb -b%s -utc%s -hsy%s -geopos%s,%s,%s -g\",\" -head", formattedDate, utctime, housesystem, longitude, latitude, altitude)
output, err := runBinary("swetest", args, debug)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to run houses binary: %v", err),
})
}
parsedOutput, err := parseHousesOutput(output)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to parse houses binary output: %v", err),
})
}
if debug {
fmt.Println("House result:", parsedOutput)
}
return c.JSON(parsedOutput)
})
// Endpoint to run the star binary
app.Get("/run-star", limiterMiddleware, func(c *fiber.Ctx) error {
debugParam := c.Query("debug")
debug := false
if debugParam == "true" {
debug = true
}
birthdate := c.Query("birthdate")
utctime := c.Query("utctime")
stars := c.Query("stars")
housesystem := c.Query("housesystem")
longitude := c.Query("longitude")
latitude := c.Query("latitude")
altitude := c.Query("altitude")
formattedDate, err := parseBirthdate(birthdate)
if err != nil {
return c.Status(http.StatusBadRequest).JSON(map[string]string{
"error": fmt.Sprintf("Failed to parse birthdate: %v", err),
})
}
starList := strings.Split(stars, ",")
var result []map[string]string
for _, star := range starList {
args := fmt.Sprintf("-b%s -utc%s -pf -fPlbsjW= -xf%s -head -g\",\"", formattedDate, utctime, star)
output, err := runBinary("swetest", args, debug)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to run star binary for star '%s': %v", star, err),
})
}
parsedOutput, err := parseStarOutput(output)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to parse star binary output for star '%s': %v", star, err),
})
}
if debug {
fmt.Println("Star result:", parsedOutput)
}
result = append(result, parsedOutput...)
}
for i := 7; i < 10; i++ {
args2 := fmt.Sprintf("-b%s -utc%s -p%d -hsy%s -fPlbsjW= -geopos%s,%s,%s -g\",\" -head", formattedDate, utctime, i, housesystem, longitude, latitude, altitude)
output2, err := runBinary("swetest", args2, debug)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to run star binary for planets: %v %s", err, output2),
})
}
parsedOutput2, err := parseStarOutput(output2)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(map[string]string{
"error": fmt.Sprintf("Failed to parse star binary output for planets: %v", err),
})
}
if debug {
fmt.Println("Star result:", parsedOutput2)
}
result = append(result, parsedOutput2...)
}
return c.JSON(result)
})
// Endpoint to get the available options and parameters
app.Get("/options", func(c *fiber.Ctx) error {
debugParam := c.Query("debug")
debug := false
if debugParam == "true" {
debug = true
}
response := getOptionResponse(debug)
return c.JSON(response)
})
// Start the server
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
app.Listen(":" + port)
}
func parseBirthdate(date string) (string, error) {
formats := []string{"2.1.2006", "2-1-2006", "2/1/2006"}
var parsedTime time.Time
var err error
for _, format := range formats {
parsedTime, err = time.ParseInLocation(format, date, time.UTC)
if err == nil {
break
}
}
if err != nil {
return "", fmt.Errorf("failed to parse birthdate: %v", err)
}
// Validate the month value
if parsedTime.Month() > 12 {
return "", fmt.Errorf("invalid month value in birthdate: %d", parsedTime.Month())
}
formattedDate := parsedTime.Format("2.1.2006")
return formattedDate, nil
}
func runBinary(binaryName string, args string, debug bool) (string, error) {
cmd := exec.Command(binaryName, strings.Split(args, " ")...)
if debug {
fmt.Println("Debug mode enabled for astroAPI")
fmt.Println("This commmand:", cmd)
}
outputPipe, err := cmd.StdoutPipe()
if err != nil {
return "", fmt.Errorf("failed to create output pipe: %v", err)
}
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("failed to start command: %v", err)
}
outputBytes, err := ioutil.ReadAll(outputPipe)
if err != nil {
return "", fmt.Errorf("failed to read command output: %v", err)
}
if err := cmd.Wait(); err != nil {
return "", fmt.Errorf("command failed: %v", err)
}
output := string(outputBytes)
if debug {
fmt.Println("Binary Output:", output)
}
return output, nil
}
func parsePlanetsOutput(output string) ([]map[string]string, error) {
lines := strings.Split(output, "\n")
var result []map[string]string
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || !strings.Contains(line, ",") {
continue
}
fields := strings.Split(line, ",")
if len(fields) != 4 {
return nil, fmt.Errorf("failed to parse planets binary output line: %s", line)
}
name := strings.TrimSpace(strings.Trim(fields[0], "\"")) // Remove leading/trailing spaces and extra `"`
longitude := strings.TrimSpace(strings.Trim(fields[1], "\"")) // Remove leading/trailing spaces and extra `"`
latitude := strings.TrimSpace(strings.Trim(fields[2], "\"")) // Remove leading/trailing spaces and extra `"`
dailySpeed := strings.TrimSpace(strings.Trim(fields[3], "\"")) // Remove leading/trailing spaces and extra `"`
item := map[string]string{
"name": name,
"longitude": longitude,
"latitude": latitude,
"dailySpeed": dailySpeed,
}
result = append(result, item)
}
return result, nil
}
func parseHousesOutput(output string) ([]map[string]string, error) {
lines := strings.Split(output, "\n")
var result []map[string]string
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || !strings.Contains(line, ",") {
continue
}
fields := strings.Split(line, ",")
if len(fields) != 2 {
return nil, fmt.Errorf("failed to parse houses binary output line: %s", line)
}
name := strings.TrimSpace(strings.Trim(fields[0], "\"")) // Remove leading/trailing spaces and extra `"`
longitude := strings.TrimSpace(strings.Trim(fields[1], "\"")) // Remove leading/trailing spaces and extra `"`
item := map[string]string{
"name": name,
"longitude": longitude,
}
result = append(result, item)
}
return result, nil
}
func parseStarOutput(output string) ([]map[string]string, error) {
lines := strings.Split(output, "\n")
var result []map[string]string
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || !strings.Contains(line, ",") {
continue
}
fields := strings.Split(line, ",")
if len(fields) == 7 {
starName := strings.TrimSpace(strings.Trim(fields[0], "\""))
altName := "planet"
longitude := strings.TrimSpace(strings.Trim(fields[1], "\""))
latitude := strings.TrimSpace(strings.Trim(fields[2], "\""))
speed := strings.TrimSpace(strings.Trim(fields[3], "\""))
house := strings.TrimSpace(strings.Trim(fields[4], "\""))
distance := strings.TrimSpace(strings.Trim(fields[5], "\""))
magnitude := strings.TrimSpace(strings.Trim(fields[6], "\""))
magnitude = strings.TrimSuffix(magnitude, "m")
item := map[string]string{
"starName": starName,
"altName": altName,
"longitude": longitude,
"latitude": latitude,
"speed": speed,
"house": house,
"distance": distance,
"magnitude": magnitude,
}
result = append(result, item)
continue
}
if len(fields) != 8 {
return nil, fmt.Errorf("failed to parse star output line: %s", line)
}
starName := strings.TrimSpace(strings.Trim(fields[0], "\""))
altName := strings.TrimSpace(strings.Trim(fields[1], "\""))
longitude := strings.TrimSpace(strings.Trim(fields[2], "\""))
latitude := strings.TrimSpace(strings.Trim(fields[3], "\""))
speed := strings.TrimSpace(strings.Trim(fields[4], "\""))
house := strings.TrimSpace(strings.Trim(fields[5], "\""))
distance := strings.TrimSpace(strings.Trim(fields[6], "\""))
magnitude := strings.TrimSpace(strings.Trim(fields[7], "\""))
magnitude = strings.TrimSuffix(magnitude, "m")
item := map[string]string{
"starName": starName,
"altName": altName,
"longitude": longitude,
"latitude": latitude,
"speed": speed,
"house": house,
"distance": distance,
"magnitude": magnitude,
}
result = append(result, item)
}
return result, nil
}
func getOptionResponse(debug bool) map[string]interface{} {
response := map[string]interface{}{
"endpoints": map[string]interface{}{
"/run-planets": map[string]interface{}{
"description": "Endpoint to run the planets binary.",
"parameters": map[string]interface{}{
"birthdate": "string (required) - Birthdate in the format of 'dd.mm.yyyy', 'dd-mm-yyyy', or 'dd/mm/yyyy'.",
"utctime": "string (required) - UTC time in the format of 'hh:mm'.",
"latitude": "string (required) - Latitude in decimal format.",
"longitude": "string (required) - Longitude in decimal format.",
"altitude": "string (required) - Altitude in meters.",
"housesystem": "string (required) - House system (P for Placidus, R for Regiomontanus).",
"debug": "bool - Enable debug mode.",
},
},
"/run-houses": map[string]interface{}{
"description": "Endpoint to run the houses binary.",
"parameters": map[string]interface{}{
"birthdate": "string (required) - Birthdate in the format of 'dd.mm.yyyy', 'dd-mm-yyyy', or 'dd/mm/yyyy'.",
"utctime": "string (required) - UTC time in the format of 'hh:mm'.",
"latitude": "string (required) - Latitude in decimal format.",
"longitude": "string (required) - Longitude in decimal format.",
"altitude": "string (required) - Altitude in meters.",
"housesystem": "string (required) - House system (P for Placidus, R for Regiomontanus).",
"debug": "bool - Enable debug mode.",
},
},
"/run-star": map[string]interface{}{
"description": "Endpoint to run the star binary.",
"parameters": map[string]interface{}{
"birthdate": "string (required) - Birthdate in the format of 'dd.mm.yyyy', 'dd-mm-yyyy', or 'dd/mm/yyyy'.",
"utctime": "string (required) - UTC time in the format of 'hh:mm'.",
"stars": "string (required) - Comma-separated list of stars.",
"longitude": "string (required) - Longitude in decimal format.",
"altitude": "string (required) - Altitude in meters.",
"housesystem": "string (required) - House system (P for Placidus, R for Regiomontanus).",
"debug": "bool - Enable debug mode.",
},
},
},
}
return response
}