5
0
Fork 0

Added integration with backend-subscribe

master
UnicodingUnicorn 2019-02-16 01:29:29 +08:00
parent fa145f7948
commit 7886168592
2 changed files with 40 additions and 1 deletions

View File

@ -15,3 +15,14 @@ Refer to protobuf definitions in ```backend-protobuf```.
| new_store | Publish to | Store | - | - | - |
| request_store | Request | DataRequest | - | Response | client |
| scan_store | Request | ScanRequest | - | Response | client |
### new_store
Pushes the results of its operation to ```backend-subscribe```.
| Code | Message | Description |
| ---- | ------- | ----------- |
| 200 | Inserted bite's key | Store operation was successful |
| 400 | 400 Bad Request | Key could not be marshalled properly |
| 500 | 500 Internal Server Error | Error storing the bite in badger |

30
main.go
View File

@ -52,13 +52,22 @@ func main() {
func NewStore(m *nats.Msg) {
storeRequest := Store{}
if err := proto.Unmarshal(m.Data, &storeRequest); err != nil {
log.Println(err)
log.Println(err) // Fail quietly since protobuf data is needed torespond
return
}
key, err := MarshalKey(storeRequest.Type, storeRequest.Bite.Key, storeRequest.Bite.Start)
if err != nil {
log.Println(err)
errRes := Response {
Code: 400,
Message: http.StatusText(http.StatusBadRequest),
Client: storeRequest.Bite.Client,
}
errResBytes, errResErr := proto.Marshal(&errRes)
if errResErr == nil {
nc.Publish("res", errResBytes)
}
return
}
@ -70,7 +79,26 @@ func NewStore(m *nats.Msg) {
if err != nil {
log.Println(err)
errRes := Response {
Code: 500,
Message: http.StatusText(http.StatusInternalServerError),
Client: storeRequest.Bite.Client,
}
errResBytes, errResErr := proto.Marshal(&errRes)
if errResErr == nil {
nc.Publish("res", errResBytes)
}
return
} else {
res := Response {
Code: 200,
Message: []byte(key),
Client: storeRequest.Bite.Client,
}
resBytes, resErr := proto.Marshal(&res)
if resErr == nil {
nc.Publish("res", resBytes)
}
}
}