-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
128 lines (115 loc) · 2.78 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"log"
"net/http"
"os"
)
var path string
var addr string
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "%s <addr>\n", os.Args[0])
os.Exit(2)
}
addr = os.Args[1]
fmt.Println("will listen for http traffic on", addr)
endpoints := NewEndpoints()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" && r.URL.Path != "" {
http.Error(w, "empty path", http.StatusBadRequest)
}
endpoints.Lock()
defer endpoints.Unlock()
js, err := endpoints.Json()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
})
http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
key, ratio, err := parseKeyRatio(r.URL.Path, "/static/ratio")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
endpoints.Lock()
defer endpoints.Unlock()
e, ok := endpoints.Get(key)
if !ok {
e = NewEndpointBasic(ratio)
endpoints.Set(key, e)
}
e.ServeHTTP(w, r)
})
http.HandleFunc("/static-by-ip/", func(w http.ResponseWriter, r *http.Request) {
key, ratio, err := parseKeyRatio(r.URL.Path, "/static-by-ip/ratio")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
endpoints.Lock()
defer endpoints.Unlock()
e, ok := endpoints.Get(key)
if !ok {
e = NewEndpointByIp(ratio)
endpoints.Set(key, e)
}
e.ServeHTTP(w, r)
})
http.HandleFunc("/dynamic/", func(w http.ResponseWriter, r *http.Request) {
key, ratio, err := parseDynamicKeyRatio(r.URL.Path, "/dynamic/key[/ratio]")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
endpoints.Lock()
defer endpoints.Unlock()
// user wants to update
if ratio != -1 {
e, ok := endpoints.Get(key)
if !ok {
http.Error(w, "not found", http.StatusBadRequest)
} else {
e.Update(ratio)
w.Write([]byte("updated\n"))
}
return
}
e, ok := endpoints.Get(key)
if !ok {
e = NewEndpointBasic(0)
endpoints.Set(key, e)
}
e.ServeHTTP(w, r)
})
http.HandleFunc("/dynamic-by-ip/", func(w http.ResponseWriter, r *http.Request) {
key, ratio, err := parseDynamicKeyRatio(r.URL.Path, "/dynamic-by-ip/key[/ratio]")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
endpoints.Lock()
defer endpoints.Unlock()
// user wants to update
if ratio != -1 {
e, ok := endpoints.Get(key)
if !ok {
http.Error(w, "not found", http.StatusBadRequest)
} else {
e.Update(ratio)
w.Write([]byte("updated\n"))
}
return
}
e, ok := endpoints.Get(key)
if !ok {
e = NewEndpointByIp(0)
endpoints.Set(key, e)
}
e.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(addr, nil))
}