-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
101 lines (79 loc) · 2.32 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
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
messages "github.com/nokusukun/psfs/pb"
"github.com/nokusukun/psfs/satellite"
)
type WriteRequest struct {
PacketType string `json:"type"`
Destination string `json:"destination"`
Namespace string `json:"namespace"`
Content interface{} `json:"content"`
}
func generateAPI(sat *satellite.Satellite) *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/peers", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Retieving Peers")
var ids []string
for _, p := range sat.Peers {
ids = append(ids, p.ID.Pretty())
}
_ = json.NewEncoder(w).Encode(ids)
}).Methods("GET")
router.HandleFunc("/write", func(w http.ResponseWriter, r *http.Request) {
request := WriteRequest{}
_ = json.NewDecoder(r.Body).Decode(&request)
var errCode string
p, exists := sat.Peers[request.Destination]
if exists {
err := p.Write(messages.PacketType(messages.PacketType_value[request.PacketType]),
request.Namespace,
request.Content)
if err != nil {
errCode = fmt.Sprintf("failed to write: %v", err)
}
} else {
errCode = fmt.Sprintf("peer does not exist: %v", request.Destination)
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"error": errCode,
})
})
router.HandleFunc("/broadcast", func(w http.ResponseWriter, r *http.Request) {
request := WriteRequest{}
_ = json.NewDecoder(r.Body).Decode(&request)
var errCode string
err := sat.BroadcastMessage(request.Namespace, request.Content)
if err != nil {
errCode = fmt.Sprintf("failed to broadcast: %v", err)
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"error": errCode,
})
})
router.HandleFunc("/whois/{peerId}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
errCode := ""
var response interface{}
p, exists := sat.Peers[vars["peerId"]]
if exists {
resp, err := p.Request("whois", "")
if err != nil {
errCode = fmt.Sprintf("failed request: %v", err)
} else {
fmt.Println("Waiting for response...")
response = <-resp
}
} else {
errCode = fmt.Sprintf("peer does not exist: %v", vars["peerId"])
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"error": errCode,
"response": response,
})
}).Methods("GET")
return router
}