/
redir.go
59 lines (52 loc) · 1.46 KB
/
redir.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
package webapi
import (
"net/http"
"github.com/zenazn/goji/web"
"io/ioutil"
"encoding/json"
)
var respOk = map[string]string {
"response": "ok",
}
func (w *WebApp) AddRedir(c web.C, wr http.ResponseWriter, r *http.Request) {
from := c.URLParams["from"]
to := c.URLParams["to"]
err := w.dnsBackend.redirBackend.AddRedirIp(from,to)
if err == nil {
w.render.JSON(wr, http.StatusOK, respOk)
} else {
w.render.JSON(wr, http.StatusOK, respErr(err))
}
}
func (w *WebApp) BatchAddRedir(c web.C, wr http.ResponseWriter, r *http.Request) {
raw,err := ioutil.ReadAll(r.Body)
if (err != nil) {
w.render.JSON(wr, http.StatusOK, respErr(err))
return
}
ipList := make(map[string]string)
err = json.Unmarshal(raw, &ipList)
if (err != nil) {
w.render.JSON(wr, http.StatusOK, respErr(err))
return}
err = w.dnsBackend.redirBackend.SetRedirIp(ipList)
if (err != nil) {
w.render.JSON(wr, http.StatusOK, respErr(err))
return
}
log.Notice("loaded %d IPs", len(ipList))
w.render.JSON(wr, http.StatusOK, respOk)
}
func (w *WebApp) DeleteRedir(c web.C, wr http.ResponseWriter, r *http.Request) {
from := c.URLParams["from"]
err := w.dnsBackend.redirBackend.DeleteRedirIp(from)
if err == nil {
w.render.JSON(wr, http.StatusOK, respOk)
} else {
w.render.JSON(wr, http.StatusOK, respErr(err))
}
}
func (w *WebApp) ListRedir(c web.C, wr http.ResponseWriter, r *http.Request) {
list, _ := w.dnsBackend.redirBackend.ListRedirIp()
w.render.JSON(wr, http.StatusOK, list)
}