-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (125 loc) · 3.23 KB
/
main.go
File metadata and controls
145 lines (125 loc) · 3.23 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
package main
import (
"encoding/json"
"expvar"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/google/logger"
"github.com/go-redis/redis"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/twentyfourbytes/api/config"
)
var redisClient *redis.Client
func main() {
configuration := config.Config()
lf, err := os.OpenFile("log.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if err != nil {
logger.Fatalf("Failed to open log file: %v", err)
}
defer lf.Close()
defer logger.Init("API", true, false, lf).Close()
// initialise redis
redisClient = redis.NewClient(&redis.Options{
Addr: configuration.Redis.IP + ":" + strconv.Itoa(configuration.Redis.Port),
DB: 0,
})
corsObj := handlers.AllowedOrigins([]string{"*"})
r := mux.NewRouter()
r.HandleFunc("/dns", getDNS)
r.HandleFunc("/ip", getIP)
r.HandleFunc("/download", download)
r.HandleFunc("/upload", upload)
r.HandleFunc("/speeds", getSpeeds)
r.HandleFunc("/speed", getSpeed)
r.HandleFunc("/debug/vars", expvarHandler)
// r.HandleFunc("/upload", handleUpload)
http.Handle("/", r)
srv := &http.Server{
Handler: handlers.CORS(corsObj)(r),
Addr: configuration.Server,
WriteTimeout: 15 * time.Minute,
ReadTimeout: 15 * time.Minute,
}
logger.Infoln("Server running at", configuration.Server)
logger.Errorln("Error testing upload speed ")
logger.Fatal(srv.ListenAndServe())
}
func getDNS(w http.ResponseWriter, r *http.Request) {
var dnsServerips []string
vars := r.URL.Query().Get("r")
logger.Infoln("Requested dns query for key ", vars)
dnsip := redisClient.Keys(vars + ":*")
for _, v := range dnsip.Val() {
val := redisClient.Get(v)
err := val.Err()
if err != nil {
logger.Errorln("Error getting redis key", err)
}
dnsServerips = append(dnsServerips, val.Val())
}
w.WriteHeader(http.StatusOK)
js, e := json.Marshal(dnsServerips)
if e != nil {
logger.Errorln(e)
}
w.Write(js)
}
func getSpeeds(w http.ResponseWriter, r *http.Request) {
var speeds []Speed
vars := r.URL.Query().Get("r")
logger.Infoln("Requested All speeds for key ", vars)
dnsip := redisClient.Keys(vars + "*")
for _, v := range dnsip.Val() {
val := redisClient.Get(v)
var s Speed
b, _ := val.Bytes()
json.Unmarshal(b, &s)
speeds = append(speeds, s)
}
w.WriteHeader(http.StatusOK)
js, e := json.Marshal(speeds)
if e != nil {
logger.Errorln(e)
}
w.Write(js)
}
func getSpeed(w http.ResponseWriter, r *http.Request) {
vars := r.URL.Query().Get("r")
logger.Infoln("Requested speeds for key ", vars)
val := redisClient.Get(vars)
var s Speed
b, _ := val.Bytes()
json.Unmarshal(b, &s)
w.WriteHeader(http.StatusOK)
js, e := json.Marshal(s)
if e != nil {
logger.Errorln(e)
}
w.Write(js)
}
func getIP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
js, e := json.Marshal(strings.Split(r.RemoteAddr, ":")[0])
if e != nil {
logger.Errorln(e)
}
w.Write(js)
}
func expvarHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(w, ",\n")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "\n}\n")
}