-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandlers.go
89 lines (79 loc) · 2.36 KB
/
handlers.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
"time"
)
func Router() *mux.Router {
r := mux.NewRouter()
r.Handle("/token/{token}/{address}.svg", http.HandlerFunc(TokenBadgeHandler))
r.Handle("/token/{token}/{address}/usd.svg", http.HandlerFunc(TokenBadgeUSDHandler))
r.Handle("/{coin}/{address}.svg", http.HandlerFunc(NormalBadgeHandler))
r.Handle("/{coin}/{address}/usd.svg", http.HandlerFunc(USDBadgeHandler))
r.Handle("/", http.HandlerFunc(IndexHandler))
return r
}
func StartHTTPServer() {
fmt.Println("BALANCE BADGE running on http://localhost:9090")
r := Router()
srv := &http.Server{
Addr: "0.0.0.0:9090",
WriteTimeout: time.Second * 10,
ReadTimeout: time.Second * 10,
IdleTimeout: time.Second * 30,
Handler: r,
}
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
stats := &Server{
Online: true,
HourHits: statusHourHits,
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(stats)
}
func USDBadgeHandler(w http.ResponseWriter, r *http.Request) {
badge := NewBadge(r).SetBalance().SetUSD().Clean()
badge.Serve(w, r)
}
func TokenBadgeUSDHandler(w http.ResponseWriter, r *http.Request) {
badge := NewBadge(r).TokenBadge().SetUSD().Clean()
badge.Serve(w, r)
}
func TokenBadgeHandler(w http.ResponseWriter, r *http.Request) {
badge := NewBadge(r).TokenBadge().Clean()
badge.Serve(w, r)
}
func NormalBadgeHandler(w http.ResponseWriter, r *http.Request) {
badge := NewBadge(r).SetBalance().Clean()
badge.Serve(w, r)
}
func (b *Badge) Serve(w http.ResponseWriter, r *http.Request) {
load := time.Now().Sub(b.start)
fmt.Printf("%v | %v | %v | %v | %v | %v | %v\n", time.Now().Format(time.RFC3339), load, r.URL.Path, b.Coin, b.FullAddress, r.RemoteAddr, r.Header.Get("X-Forwarded-For"))
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", b.cache)
if b.error == nil {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
svgTemplate.Execute(w, b)
statusHourHits++
}
func httpGet(url string, method string, data []byte) (*http.Response, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
httpClient := &http.Client{Timeout: 30 * time.Second}
resp, err := httpClient.Do(req)
return resp, err
}