-
Notifications
You must be signed in to change notification settings - Fork 8
/
RequestFabric.go
71 lines (64 loc) · 1.77 KB
/
RequestFabric.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 "errors"
import "io/ioutil"
import "net/http"
import "encoding/json"
import "log"
type JsonFabric struct {
}
//parse json message
func (fabric *JsonFabric) ProduceJsonRequest(request *http.Request) (Request, error) {
bodyData, err := ioutil.ReadAll(request.Body)
if err != nil {
stErr := "error: Can't read request body"
log.Println(stErr)
return nil, errors.New(stErr)
}
defer request.Body.Close()
var basicRequest BasicRequest
err = json.Unmarshal(bodyData, &basicRequest)
if err != nil {
stErr := "error: Can't parse basic data"
log.Println(stErr)
return nil, errors.New(stErr)
}
switch basicRequest.Type {
case ServiceStatus:
var serviceStateRequest ServiceStateRequest
err := json.Unmarshal(bodyData, &serviceStateRequest)
if err != nil {
stErr := "error: Can't parse service state request"
log.Println(stErr)
return nil, errors.New("error: Can't parse service state request")
}
return serviceStateRequest, nil
case SystemMonitor:
var systemStateRequest SystemStateRequest
err := json.Unmarshal(bodyData, &systemStateRequest)
if err != nil {
stErr := "error: Can't parse system state request"
log.Println(stErr)
return nil, errors.New(stErr)
}
return systemStateRequest, nil
case TopProcess:
var topRequest TopRequest
err := json.Unmarshal(bodyData, &topRequest)
if err != nil {
stErr := "error: Can't parse top request"
log.Println(stErr)
return nil, errors.New(stErr)
}
return topRequest, nil
case KillProcess:
var killRequest KillRequest
err := json.Unmarshal(bodyData, &killRequest)
if err != nil {
stErr := "error: Can't parse top request"
log.Println(stErr)
return nil, errors.New(stErr)
}
return killRequest, nil
}
return nil, errors.New("error: Unknown request type")
}