-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
196 lines (164 loc) · 4.67 KB
/
main.go
File metadata and controls
196 lines (164 loc) · 4.67 KB
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
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"strconv"
// "net/url"
)
type Artist struct {
ID int `json:"id"`
Image string `json:"image"`
Name string `json:"name"`
Members []string `json:"members"`
CreationDate int `json:"creationDate"`
FirstAlbum string `json:"firstAlbum"`
Relations string `json:"relations"`
}
type Relations struct {
ID int `json:"id"`
DatesLocations map[string][]string `json:"datesLocations"`
}
type BigStruct struct {
Artist Artist
Relations Relations
}
var artists1 []Artist = []Artist{}
func main() {
http.HandleFunc("/", handleIndex)
http.HandleFunc("/artist", handleArtist)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
fmt.Println("Listening on port 8000... http://localhost:8080")
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
ThrowError("404", w, r)
}
url := "https://groupietrackers.herokuapp.com/api/artists"
artists, err := fetchArtist(url, w)
if err != nil {
http.Error(w, fmt.Sprintf("Error fetching artist data: %v", err), http.StatusInternalServerError)
return
}
artists1 = artists
// fmt.Println(len(artists1), "artists fetched successfully")
executeTemp(w, "index.html", artists1)
}
func executeTemp(w http.ResponseWriter, filename string, data interface{}) {
funcMap := template.FuncMap{
"sub1": sub1,
"len": func(x []string) int { return len(x) },
}
temp, err := template.New(filename).Funcs(funcMap).ParseFiles(filename)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
// Avoid recursion if error.html is missing or broken
if filename != "error.html" {
ThrowError("500", w, nil)
} else {
http.Error(w, "Critical error: cannot render error page.", http.StatusInternalServerError)
}
return
}
err = temp.ExecuteTemplate(w, filename, data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
if filename != "error.html" {
ThrowError("500", w, nil)
} else {
http.Error(w, "Critical error: cannot render error page.", http.StatusInternalServerError)
}
return
}
}
func fetchArtist(url string, w http.ResponseWriter) ([]Artist, error) {
response, err := http.Get(url)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
ThrowError("500", w, nil)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
w.WriteHeader(http.StatusInternalServerError)
ThrowError("500", w, nil)
}
body, err := io.ReadAll(response.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
ThrowError("500", w, nil)
}
var artists []Artist
if err := json.Unmarshal(body, &artists); err != nil {
return nil, fmt.Errorf("JSON unmarshal error: %v", err)
}
return artists, nil
}
func fetchRelations(id int) (Relations, error) { //issue here
url := fmt.Sprintf("https://groupietrackers.herokuapp.com/api/relation/%d", id)
response, err := http.Get(url)
if err != nil {
return Relations{}, fmt.Errorf("HTTP request error: %v", err)
}
defer response.Body.Close()
// if response.StatusCode != http.StatusOK {
// return Relations{}, fmt.Errorf("HTTP status code: %d", response.StatusCode)
// }
body, err := io.ReadAll(response.Body)
if err != nil {
return Relations{}, err
}
var rel Relations
if err := json.Unmarshal(body, &rel); err != nil {
return Relations{}, fmt.Errorf("JSON unmarshal error: %v", err)
}
return rel, nil
}
func handleArtist(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
ThrowError("400", w, nil)
return
}
if r.URL.Path != "/artist" {
w.WriteHeader(http.StatusNotFound)
ThrowError("404", w, nil)
return
}
artistID := r.FormValue("id")
fmt.Println("Received artist ID:", artistID)
// Check if artistID is empty
if artistID == "" {
http.Error(w, "Artist ID is required", http.StatusBadRequest)
return
}
idNum, err := strconv.Atoi(artistID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
ThrowError("500", w, nil)
return
}
if idNum < 1 || idNum > len(artists1) {
http.Error(w, "Invalid artist ID", http.StatusBadRequest)
return
}
fmt.Println("Parsed artist ID:", idNum)
fmt.Println("Total artists available:", len(artists1))
rel, err := fetchRelations(idNum)
if err != nil {
fmt.Println("error please ", err)
http.Error(w, "Error fetching relations", http.StatusInternalServerError)
return
}
artist := artists1[idNum-1]
bigstruct := BigStruct{
Artist: artist,
Relations: rel,
}
executeTemp(w, "artist.html", bigstruct)
}
func sub1(x int) int {
return x - 1
}