-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (72 loc) · 1.66 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
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"sync"
"text/template"
)
type Request struct {
Auctions []Auction `json:"auctions"`
}
type Auction struct {
UUID string `json:"uuid"`
Item string `json:"item_name"`
Price int `json:"starting_bid"`
Profit int
Rarity string `json:"tier"`
Bin bool `json:"bin"`
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
itemParam := ""
if r.Method == "POST" {
// Get the input text from the form
itemParam = r.FormValue("item")
return
}
t, err := template.ParseFiles("table.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp, err := http.Get("https://api.hypixel.net/v2/skyblock/auctions")
if err != nil {
panic(err)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var data Request
err = decoder.Decode(&data)
if err != nil {
panic(err)
}
// Create a data structure to pass to template
var filteredAuctions []Auction
var mu sync.Mutex // Add mutex to protect concurrent access
var wg sync.WaitGroup
wg.Add(len(data.Auctions))
for _, auction := range data.Auctions {
go func(auction Auction) {
defer wg.Done()
if auction.Bin && (auction.Item == itemParam || itemParam == "") {
mu.Lock()
filteredAuctions = append(filteredAuctions, auction)
mu.Unlock()
}
}(auction)
}
wg.Wait()
err = t.Execute(w, filteredAuctions)
if err != nil {
fmt.Println(err)
w.Write([]byte(err.Error()))
}
})
l, err := net.Listen("tcp", ":8080")
if err == nil {
fmt.Println("Listening on port 8080")
}
http.Serve(l, nil)
}