-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (142 loc) · 2.89 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/AstraApp/server/scraper"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func section(x, y, z int) string {
if x < 0 {
if y < 0 {
if z < 0 {
return "3"
}
if z >= 0 {
return "7"
}
}
if y >= 0 {
if z < 0 {
return "1"
}
if z >= 0 {
return "5"
}
}
}
if x >= 0 {
if y < 0 {
if z < 0 {
return "4"
}
if z >= 0 {
return "8"
}
}
if y >= 0 {
if z < 0 {
return "2"
}
if z >= 0 {
return "6"
}
}
}
return ""
}
// Section .
type Section struct {
Name string `json:"satellitename"`
Velocity string `json:"velocity"`
Image string `json:"image"`
}
//section ranges from "1" to "8" (The sections in json)
// Name, Velocity, ImageURL
func fetchSection(section string) (*Section, error) {
//section := section(x, y, z)
file, err := os.Open("scraper/satellites.json")
if err != nil {
return nil, err
}
var satellites map[string][]Section
if err := json.NewDecoder(file).Decode(&satellites); err != nil {
return nil, err
}
for key, sections := range satellites {
// Satellite name
if key != section {
continue
}
// Satellite sections
for i := 0; i < len(sections); i++ {
fmt.Println("Name: " + sections[i].Name)
fmt.Println("Velocity: " + sections[i].Velocity)
fmt.Println("Image: " + sections[i].Image)
return §ions[i], nil
}
}
return nil, nil
}
func main() {
fmt.Println("Astra Server")
s := scraper.New()
r := chi.NewRouter()
r.Use(middleware.Logger)
//http://127.0.0.1:8080/?x=5&y=-8&z=3
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
x, err := strconv.Atoi(r.URL.Query().Get("x"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
y, err := strconv.Atoi(r.URL.Query().Get("y"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
z, err := strconv.Atoi(r.URL.Query().Get("z"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
section, err := fetchSection(section(x, y, z))
if err != nil {
log.Print(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
satellite, err := s.Scrape(section.Name)
if err != nil {
log.Print(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
b, err := json.MarshalIndent(struct {
Name string
Velocity string
Image string
ResourceID string
ResourceName string
Description string
}{
section.Name,
section.Velocity,
section.Image,
satellite.Spase.Observatory.ResourceID,
satellite.Spase.Observatory.ResourceHeader.ResourceName,
satellite.Spase.Observatory.ResourceHeader.Description,
}, "", "\t")
if err != nil {
log.Print(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(b)
})
http.ListenAndServe(":8080", r)
}