-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
56 lines (49 loc) · 1.45 KB
/
http.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
package main
import "http"
import "fmt"
const lenNamePath = len("/name/")
func setContentText(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain")
}
func GetNodeByName(store *NodeStore) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
setContentText(w)
name := req.URL.Path[lenNamePath:]
node, present := store.Get(name)
if present {
fmt.Fprintln(w, node)
} else {
http.NotFound(w, req)
}
}
}
func SetNodeByName(store *NodeStore) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
setContentText(w)
var node Node
if _, err := fmt.Fscanln(req.Body, &node); err != nil {
http.Error(w, "Failed to parse node", 400)
} else {
store.Set(&node)
}
}
}
func GetAllNodes(store *NodeStore) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
setContentText(w)
for _, node := range store.GetAll() {
fmt.Fprintln(w, node)
}
var peerCN string
if len(req.TLS.PeerCertificates) > 0 {
peerCN = req.TLS.PeerCertificates[0].Subject.CommonName
} else {
peerCN = ""
}
fmt.Printf("complete = %v\n", req.TLS.HandshakeComplete)
fmt.Printf("cipherSuite = %v\n", req.TLS.CipherSuite)
fmt.Printf("protocol = %v\n", req.TLS.NegotiatedProtocol)
fmt.Printf(" mutual? = %v\n", req.TLS.NegotiatedProtocolIsMutual)
fmt.Printf("peerCN = %v\n", peerCN)
}
}