This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
/
api.go
90 lines (81 loc) · 2.42 KB
/
api.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
package main
import (
"fmt"
"github.com/codegangsta/negroni"
"github.com/julienschmidt/httprouter"
"github.com/miekg/dns"
. "gloon/record_set"
"log"
"net/http"
"strings"
"time"
)
var DnsTypes = map[string]uint16{"A": dns.TypeA}
func PanicHandler(w http.ResponseWriter, r *http.Request, p interface{}) {
handlePanic(p)
Json(w, "Internal Error", 500)
}
func LogMiddleWare(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
next(rw, r)
dur := time.Since(start)
res := rw.(negroni.ResponseWriter)
log.Printf("Completed: %s %s -- %d (%v)", r.Method, r.URL.Path, res.Status(), dur)
}
func Json(w http.ResponseWriter, text string, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
fmt.Fprintf(w, text)
}
func ApiPutHost(w http.ResponseWriter, r *http.Request, ps httprouter.Params, recs *RecordSet) {
dnsType := strings.ToUpper(ps.ByName("type"))
host := ps.ByName("host")
addr := ps.ByName("ip")
dt, ok := DnsTypes[dnsType]
if !ok {
Json(w, "Address type not found", 404)
return
}
recs.Put(dt, host, addr)
Json(w, "ok", 200)
}
func ApiDelHost(w http.ResponseWriter, r *http.Request, ps httprouter.Params, recs *RecordSet) {
dnsType := strings.ToUpper(ps.ByName("type"))
host := ps.ByName("host")
dt, ok := DnsTypes[dnsType]
if !ok {
Json(w, "Address type not found", 404)
return
}
recs.Del(dt, host)
Json(w, "ok", 200)
}
func ApiDelHostAddr(w http.ResponseWriter, r *http.Request, ps httprouter.Params, recs *RecordSet) {
dnsType := strings.ToUpper(ps.ByName("type"))
host := ps.ByName("host")
addr := ps.ByName("addr")
dt, ok := DnsTypes[dnsType]
if !ok {
Json(w, "Address type not found", 404)
return
}
recs.DelAddr(dt, host, addr)
Json(w, "ok", 200)
}
func RunApiServer(settings *Settings, recs *RecordSet) {
router := httprouter.New()
router.PanicHandler = PanicHandler
router.PUT("/records/:type/:host/:ip", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ApiPutHost(w, r, ps, recs)
})
router.DELETE("/records/:type/:host", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ApiDelHost(w, r, ps, recs)
})
router.DELETE("/records/:type/:host/:addr", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ApiDelHostAddr(w, r, ps, recs)
})
n := negroni.New()
n.Use(negroni.HandlerFunc(LogMiddleWare))
n.UseHandler(router)
log.Fatal(http.ListenAndServe(settings.ApiAddr, n))
}