-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (93 loc) · 2.16 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
package main
import (
"encoding/json"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
const (
opensea_url = "https://api.opensea.io/api/v1/assets?order_direction=desc&offset=0&limit=20"
)
func handler(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
offset := "0"
limit := "20"
order_direction := "desc"
owner := ""
if x, ok := q["owner"]; ok {
owner = x[0]
} else {
http.ServeFile(w, r, "./assets/index.html")
return
}
if x, ok := q["order_direction"]; ok {
order_direction = x[0]
}
if x, ok := q["offset"]; ok {
offset = x[0]
}
if x, ok := q["limit"]; ok {
limit = x[0]
}
resp, err := http.Get(opensea_url + "&owner=" + owner + "&order_direction=" + order_direction + "&offset=" + offset + "&limit=" + limit)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
m := map[string]interface{}{}
t := template.New("nfts.html") // Create a template.
t, err = t.ParseFiles("./assets/nfts.html") // Parse template file.
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := json.Unmarshal([]byte(body), &m); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := t.Execute(w, m); err == nil {
log.Println("success ", resp)
} else {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
}
// Route declaration
func router() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", handler).Methods("GET")
return r
}
// Initiate web server
func main() {
log.Print("starting server...")
// Determine port for HTTP service.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("defaulting to port %s", port)
}
// Start HTTP server.
log.Printf("listening on port %s", port)
router := router()
srv := &http.Server{
Handler: router,
Addr: ":" + port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}