-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest.go
executable file
·97 lines (93 loc) · 3.26 KB
/
rest.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
package yesdns
// Depends on:
// resolver.go/SyncResolversWithDatabase
import (
"log"
"net/http"
"encoding/json"
"fmt"
//"path/filepath"
)
// Runs REST API HTTP server forever.
//
// httpListenAddr: (string) interface and port to listen on
// database: (*Database) Reference to local database that stores DNS records.
func ServeRestApi(httpListenAddr string, database *Database, reloadChannel chan <- bool, tlsCertFile string, tlsKeyFile string) {
http.HandleFunc("/v1/question", func(w http.ResponseWriter, r *http.Request) {
// Decode json
if r.Body == nil {
http.Error(w, "Empty body not allowed", http.StatusBadRequest)
return
}
var dnsRecord DnsMessage
if err := json.NewDecoder(r.Body).Decode(&dnsRecord); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Handle method
if r.Method == http.MethodPut {
// TODO validate dnsRecord
log.Printf("DEBUG Saving %s\n", dnsRecord)
if err := database.WriteDnsMessage(dnsRecord); err != nil {
log.Printf("ERROR Error saving %s. Error was: %s\n", dnsRecord, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// TODO return 204 No content
} else if r.Method == http.MethodDelete {
// TODO validate dnsRecord
log.Printf("DEBUG Deleting %s\n", dnsRecord)
if err := database.DeleteDnsMessage(dnsRecord); err != nil {
log.Printf("ERROR Error deleting %s. Error was: %s\n", dnsRecord, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// TODO Return 204 No Content
} else {
// TODO return json error message
http.Error(w, fmt.Sprintf("Method %s not allowed for /v1/question\n", r.Method), http.StatusMethodNotAllowed)
}
})
http.HandleFunc("/v1/resolver", func(w http.ResponseWriter, r *http.Request) {
// Decode json
if r.Body == nil {
http.Error(w, "Empty body not allowed", http.StatusBadRequest)
return
}
var resolver Resolver
if err := json.NewDecoder(r.Body).Decode(&resolver); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if r.Method == http.MethodPut {
if err := database.WriteResolver(resolver); err != nil {
log.Printf("ERROR Error writing %s. Error was: %s\n", resolver, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reloadChannel <- true
} else if r.Method == http.MethodDelete {
if err := database.DeleteResolver(resolver); err != nil {
log.Printf("ERROR Error deleting %s. Error was: %s\n", resolver, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reloadChannel <- true
} else {
// TODO return json error message
http.Error(w, fmt.Sprintf("Method %s not allowed for /v1/resolver\n", r.Method), http.StatusMethodNotAllowed)
}
})
// Start serving REST API forever
if tlsCertFile == "" || tlsKeyFile == "" {
log.Printf("INFO Starting unsecured REST API listener on %s\n", httpListenAddr)
log.Fatal(http.ListenAndServe(httpListenAddr, nil))
} else {
log.Printf("INFO Starting TLS REST API listener on %s\n", httpListenAddr)
// tlsCertFile, _ := filepath.Abs(tlsCertFile)
// tlsKeyFile, _ := filepath.Abs(tlsKeyFile)
log.Fatal(http.ListenAndServeTLS(httpListenAddr, tlsCertFile, tlsKeyFile, nil))
}
}