-
Notifications
You must be signed in to change notification settings - Fork 1
/
entity.go
71 lines (68 loc) · 1.96 KB
/
entity.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
package main
import (
"encoding/json"
"github.com/sirupsen/logrus"
"net/http"
"strings"
"time"
)
type EntityHandler struct{}
func (_ *EntityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
writeJson(w, 405, ApiResponse{Error: true, Message: "Method not allowed"})
return
}
if r.Header.Get("Authorization") == "" {
writeJson(w, 403, ApiResponse{Error: true, Message: "Unauthorized"})
return
}
if r.Header.Get("Authorization") != Config.Auth {
writeJson(w, 403, ApiResponse{Error: true, Message: "Forbidden"})
return
}
if strings.Index(r.Header.Get("Content-Type"), "application/json") == -1 {
writeJson(w, 400, ApiResponse{Error: true, Message: "Content-Type either not found, or not application/json!"})
return
}
body := &EntityRequest{}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
logrus.Errorf("Failed to decode JSON body for entity request: %s", err.Error())
writeJson(w, 500, ApiResponse{Error: true, Message: "Unable to decode JSON body!"})
return
}
if body.ID == "" {
writeJson(w, 400, ApiResponse{Error: true, Message: "ID not specified!"})
return
}
if body.Type == "" {
writeJson(w, 400, ApiResponse{Error: true, Message: "type not specified!"})
return
}
Server.PutEntityChan(body.ID)
results := make([]EntityResponse, 0, len(Server.Clients))
for _, cluster := range Server.Clients {
if cluster.State == ClusterWaiting || cluster.State == ClusterConnecting {
results = append(results, EntityResponse{Error: "cluster unhealthy"})
continue
}
cluster.Write(Entity, body)
select {
case resp := <-Server.GetEntityChan(body.ID):
{
results = append(results, EntityResponse{
ID: "",
Error: resp.Error,
Data: resp.Data,
})
break
}
case <-time.After(5 * time.Second):
{
results = append(results, EntityResponse{Error: "timed out"})
break
}
}
}
Server.DeleteEntityChan(body.ID)
writeJson(w, 200, ApiResponse{Data: results})
}