forked from nictuku/dht
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HTTPserver.go
52 lines (49 loc) · 1.18 KB
/
HTTPserver.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
package dht
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// para: UPD host&port and TCP host&port
// receive from tcp, then send to node's udp port?
// or just insert to dht's routing table
func (d *DHT) StartHTTPServer(host, port string) {
serviceAddr := fmt.Sprintf("%s:%s", host, port)
// register router
http.Handle("/update", d)
var srv http.Server
srv.Addr = serviceAddr
log.Println(srv.Addr)
log.Println(srv.ListenAndServe())
}
func (d *DHT) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("Request received")
switch r.Method {
case http.MethodGet:
w.Header().Add("Content-Type", "application/json")
// TODO
// PARA
case http.MethodPost:
dec := json.NewDecoder(r.Body)
var r Registration
err := dec.Decode(&r)
if err != nil {
d.DebugLogger.Errorf("error parsing add node post:%v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
log.Println(r)
err = d.ADDHonestPeer(r.Nodeid, r.NodeAddr)
if err != nil {
d.DebugLogger.Errorf("error parsing add node post:%v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
// TODO
// case http.MethodDelete:
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
}