Refactored to use backend-store. Basically just a proxy now
parent
3087ff7783
commit
c77fb4761a
|
@ -59,7 +59,8 @@ Content-Type: application/json
|
|||
|
||||
| Code | Description |
|
||||
| ---- | ----------- |
|
||||
| 400 | Absolutely ridiculous number of things. I give up. |
|
||||
| 400 | Malformed input (from/to not timestamp, key not alphanumeric). |
|
||||
| 500 | NATs or protobuf serilisation encountered errors. |
|
||||
|
||||
---
|
||||
|
||||
|
@ -87,6 +88,7 @@ Raw audio data.
|
|||
| Code | Description |
|
||||
| ---- | ----------- |
|
||||
| 400 | start is not an uint/key is not an alphanumeric string/specified bite could not be found |
|
||||
| 500 | NATs or protobuf serilisation encountered errors. |
|
||||
|
||||
---
|
||||
|
||||
|
@ -114,3 +116,4 @@ Raw audio data.
|
|||
| Code | Description |
|
||||
| ---- | ----------- |
|
||||
| 400 | start is not an uint/key is not an alphanumeric string/specified bite could not be found |
|
||||
| 500 | NATs or protobuf serilisation encountered errors. |
|
||||
|
|
24
bite.pb.go
24
bite.pb.go
|
@ -24,6 +24,7 @@ type Bite struct {
|
|||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||
Start uint64 `protobuf:"varint,2,opt,name=start,proto3" json:"start,omitempty"`
|
||||
Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
|
||||
Client *Client `protobuf:"bytes,4,opt,name=client,proto3" json:"client,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
|
@ -75,6 +76,13 @@ func (m *Bite) GetData() []byte {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (m *Bite) GetClient() *Client {
|
||||
if m != nil {
|
||||
return m.Client
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Bite)(nil), "main.Bite")
|
||||
}
|
||||
|
@ -82,12 +90,14 @@ func init() {
|
|||
func init() { proto.RegisterFile("bite.proto", fileDescriptor_e1ec993646b17549) }
|
||||
|
||||
var fileDescriptor_e1ec993646b17549 = []byte{
|
||||
// 105 bytes of a gzipped FileDescriptorProto
|
||||
// 139 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4a, 0xca, 0x2c, 0x49,
|
||||
0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x72, 0xe2, 0x62,
|
||||
0x71, 0xca, 0x2c, 0x49, 0x15, 0x12, 0xe0, 0x62, 0xce, 0x4e, 0xad, 0x94, 0x60, 0x54, 0x60, 0xd4,
|
||||
0xe0, 0x0c, 0x02, 0x31, 0x85, 0x44, 0xb8, 0x58, 0x8b, 0x4b, 0x12, 0x8b, 0x4a, 0x24, 0x98, 0x14,
|
||||
0x18, 0x35, 0x58, 0x82, 0x20, 0x1c, 0x21, 0x21, 0x2e, 0x96, 0x94, 0xc4, 0x92, 0x44, 0x09, 0x66,
|
||||
0x05, 0x46, 0x0d, 0x9e, 0x20, 0x30, 0x3b, 0x89, 0x0d, 0x6c, 0xa0, 0x31, 0x20, 0x00, 0x00, 0xff,
|
||||
0xff, 0x28, 0xbd, 0x24, 0x95, 0x5e, 0x00, 0x00, 0x00,
|
||||
0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x93, 0xe2, 0x49, 0xce,
|
||||
0xc9, 0x4c, 0xcd, 0x2b, 0x81, 0x88, 0x29, 0x65, 0x70, 0xb1, 0x38, 0x65, 0x96, 0xa4, 0x0a, 0x09,
|
||||
0x70, 0x31, 0x67, 0xa7, 0x56, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x06, 0x81, 0x98, 0x42, 0x22,
|
||||
0x5c, 0xac, 0xc5, 0x25, 0x89, 0x45, 0x25, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0x2c, 0x41, 0x10, 0x8e,
|
||||
0x90, 0x10, 0x17, 0x4b, 0x4a, 0x62, 0x49, 0xa2, 0x04, 0xb3, 0x02, 0xa3, 0x06, 0x4f, 0x10, 0x98,
|
||||
0x2d, 0xa4, 0xc2, 0xc5, 0x06, 0x31, 0x53, 0x82, 0x45, 0x81, 0x51, 0x83, 0xdb, 0x88, 0x47, 0x0f,
|
||||
0x64, 0x91, 0x9e, 0x33, 0x58, 0x2c, 0x08, 0x2a, 0x97, 0xc4, 0x06, 0xb6, 0xd0, 0x18, 0x10, 0x00,
|
||||
0x00, 0xff, 0xff, 0x0a, 0x85, 0x92, 0x1b, 0x92, 0x00, 0x00, 0x00,
|
||||
}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: client.proto
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Client struct {
|
||||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||
Client string `protobuf:"bytes,2,opt,name=client,proto3" json:"client,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Client) Reset() { *m = Client{} }
|
||||
func (m *Client) String() string { return proto.CompactTextString(m) }
|
||||
func (*Client) ProtoMessage() {}
|
||||
func (*Client) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_014de31d7ac8c57c, []int{0}
|
||||
}
|
||||
|
||||
func (m *Client) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Client.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Client) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Client.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *Client) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Client.Merge(m, src)
|
||||
}
|
||||
func (m *Client) XXX_Size() int {
|
||||
return xxx_messageInfo_Client.Size(m)
|
||||
}
|
||||
func (m *Client) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Client.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Client proto.InternalMessageInfo
|
||||
|
||||
func (m *Client) GetKey() string {
|
||||
if m != nil {
|
||||
return m.Key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Client) GetClient() string {
|
||||
if m != nil {
|
||||
return m.Client
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Client)(nil), "main.Client")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("client.proto", fileDescriptor_014de31d7ac8c57c) }
|
||||
|
||||
var fileDescriptor_014de31d7ac8c57c = []byte{
|
||||
// 83 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x49, 0xce, 0xc9, 0x4c,
|
||||
0xcd, 0x2b, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x32,
|
||||
0xe2, 0x62, 0x73, 0x06, 0x8b, 0x0a, 0x09, 0x70, 0x31, 0x67, 0xa7, 0x56, 0x4a, 0x30, 0x2a, 0x30,
|
||||
0x6a, 0x70, 0x06, 0x81, 0x98, 0x42, 0x62, 0x5c, 0x6c, 0x10, 0x1d, 0x12, 0x4c, 0x60, 0x41, 0x28,
|
||||
0x2f, 0x89, 0x0d, 0x6c, 0x80, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x95, 0x71, 0x3f, 0xbd, 0x50,
|
||||
0x00, 0x00, 0x00,
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: store/data_request.proto
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type DataRequest struct {
|
||||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||
Start uint64 `protobuf:"varint,2,opt,name=start,proto3" json:"start,omitempty"`
|
||||
Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *DataRequest) Reset() { *m = DataRequest{} }
|
||||
func (m *DataRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*DataRequest) ProtoMessage() {}
|
||||
func (*DataRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_1c7f2c7bfa43f02c, []int{0}
|
||||
}
|
||||
|
||||
func (m *DataRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_DataRequest.Unmarshal(m, b)
|
||||
}
|
||||
func (m *DataRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_DataRequest.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *DataRequest) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_DataRequest.Merge(m, src)
|
||||
}
|
||||
func (m *DataRequest) XXX_Size() int {
|
||||
return xxx_messageInfo_DataRequest.Size(m)
|
||||
}
|
||||
func (m *DataRequest) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_DataRequest.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_DataRequest proto.InternalMessageInfo
|
||||
|
||||
func (m *DataRequest) GetKey() string {
|
||||
if m != nil {
|
||||
return m.Key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *DataRequest) GetStart() uint64 {
|
||||
if m != nil {
|
||||
return m.Start
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *DataRequest) GetType() string {
|
||||
if m != nil {
|
||||
return m.Type
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*DataRequest)(nil), "main.DataRequest")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("store/data_request.proto", fileDescriptor_1c7f2c7bfa43f02c) }
|
||||
|
||||
var fileDescriptor_1c7f2c7bfa43f02c = []byte{
|
||||
// 119 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x28, 0x2e, 0xc9, 0x2f,
|
||||
0x4a, 0xd5, 0x4f, 0x49, 0x2c, 0x49, 0x8c, 0x2f, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0xd1, 0x2b,
|
||||
0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0xf2, 0xe4, 0xe2, 0x76, 0x49,
|
||||
0x2c, 0x49, 0x0c, 0x82, 0x48, 0x09, 0x09, 0x70, 0x31, 0x67, 0xa7, 0x56, 0x4a, 0x30, 0x2a, 0x30,
|
||||
0x6a, 0x70, 0x06, 0x81, 0x98, 0x42, 0x22, 0x5c, 0xac, 0xc5, 0x25, 0x89, 0x45, 0x25, 0x12, 0x4c,
|
||||
0x0a, 0x8c, 0x1a, 0x2c, 0x41, 0x10, 0x8e, 0x90, 0x10, 0x17, 0x4b, 0x49, 0x65, 0x41, 0xaa, 0x04,
|
||||
0x33, 0x58, 0x21, 0x98, 0x9d, 0xc4, 0x06, 0x36, 0xd7, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x70,
|
||||
0x54, 0xb9, 0xcc, 0x73, 0x00, 0x00, 0x00,
|
||||
}
|
70
key.go
70
key.go
|
@ -1,70 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var ExtractKeyParseError = errors.New("ExtractKey: parse error, possibly because seprator was not found")
|
||||
|
||||
// Marshal keys
|
||||
func validObj(obj string) bool {
|
||||
return obj == "bite" || obj == "user"
|
||||
}
|
||||
|
||||
// TODO: ensure security of regexp
|
||||
var validConversationRegexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
|
||||
|
||||
func validConversation(conversation string) bool {
|
||||
return validConversationRegexp.MatchString(conversation)
|
||||
}
|
||||
|
||||
const conversationSeprator = '@'
|
||||
const objSeprator = '+'
|
||||
|
||||
func MarshalKey(obj, conversation string, start uint64) ([]byte, error) {
|
||||
prefixBytes, err := MarshalKeyPrefix(obj, conversation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
startBytes := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(startBytes, start)
|
||||
|
||||
return append(prefixBytes, startBytes...), nil
|
||||
}
|
||||
|
||||
func MarshalKeyPrefix(obj, conversation string) ([]byte, error) {
|
||||
if !validObj(obj) || !validConversation(conversation) {
|
||||
return nil, errors.New("main: FormatKey: bad obj or conversation")
|
||||
}
|
||||
return []byte(obj + string(objSeprator) + conversation + string(conversationSeprator)), nil
|
||||
}
|
||||
|
||||
func ExtractKey(b []byte) (string, string, uint64, error) {
|
||||
startStart := bytes.LastIndexByte(b, conversationSeprator) + 1
|
||||
if startStart < 0 {
|
||||
return "", "", 0, ExtractKeyParseError
|
||||
}
|
||||
startBytes := b[startStart:]
|
||||
|
||||
convStart := bytes.LastIndexByte(b[:startStart-1], objSeprator) + 1
|
||||
if convStart < 0 {
|
||||
return "", "", 0, ExtractKeyParseError
|
||||
}
|
||||
convBytes := b[convStart : startStart-1]
|
||||
|
||||
objStart := 0
|
||||
if objStart < 0 {
|
||||
return "", "", 0, ExtractKeyParseError
|
||||
}
|
||||
objBytes := b[objStart : convStart-1]
|
||||
|
||||
obj := string(objBytes)
|
||||
conv := string(convBytes)
|
||||
start := binary.BigEndian.Uint64(startBytes)
|
||||
|
||||
return obj, conv, start, nil
|
||||
}
|
267
main.go
267
main.go
|
@ -1,11 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"net/http"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/dgraph-io/badger"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
|
@ -18,6 +18,7 @@ var dbPath string
|
|||
var natsHost string
|
||||
|
||||
var db *badger.DB
|
||||
var natsConn *nats.Conn
|
||||
|
||||
func main() {
|
||||
// Parse flags
|
||||
|
@ -40,15 +41,17 @@ func main() {
|
|||
|
||||
// NATS client
|
||||
nc, _ := nats.Connect(natsHost);
|
||||
nc.Subscribe("new_bite", NewBite);
|
||||
nc.Subscribe("new_bite_user", NewBiteUser);
|
||||
// nc.Subscribe("new_bite", NewBite);
|
||||
// nc.Subscribe("new_bite_user", NewBiteUser);
|
||||
defer nc.Close()
|
||||
|
||||
natsConn = nc;
|
||||
|
||||
// Routes
|
||||
router := httprouter.New()
|
||||
router.GET("/conversation/:key/scan", ScanBites) // Scanning
|
||||
router.GET("/conversation/:key/start/:start", GetBite) // GET bites
|
||||
router.GET("/conversation/:key/start/:start/user", GetBiteUser) // GET bite_users
|
||||
// router.GET("/conversation/:key/start/:start/user", GetBiteUser) // GET bite_users
|
||||
|
||||
// Start server
|
||||
log.Printf("starting server on %s", listen)
|
||||
|
@ -62,71 +65,36 @@ func ParseStartString(start string) (uint64, error) {
|
|||
// Sub handlers
|
||||
// m.data = Bite protobuf
|
||||
func NewBite(m *nats.Msg) {
|
||||
bite := Bite{}
|
||||
if err := proto.Unmarshal(m.Data, &bite); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
key, err := MarshalKey("bite", bite.Key, bite.Start)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Update(func(txn *badger.Txn) error {
|
||||
// TODO: prevent overwriting existing
|
||||
err := txn.Set(key, bite.Data)
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
New("bite", m)
|
||||
}
|
||||
|
||||
func NewBiteUser(m *nats.Msg) {
|
||||
New("user", m)
|
||||
}
|
||||
|
||||
func New(t string, m *nats.Msg) {
|
||||
bite := Bite{}
|
||||
if err := proto.Unmarshal(m.Data, &bite); err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
key, err := MarshalKey("user", bite.Key, bite.Start)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
storeRequest := Store {
|
||||
Type: t,
|
||||
Bite: &bite,
|
||||
}
|
||||
reqBytes, reqErr := proto.Marshal(&storeRequest)
|
||||
|
||||
err = db.Update(func(txn *badger.Txn) error {
|
||||
// TODO: prevent overwriting existing
|
||||
err := txn.Set(key, bite.Data)
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
if reqErr != nil {
|
||||
log.Print(reqErr)
|
||||
return
|
||||
}
|
||||
}
|
||||
natsConn.Publish("new_store", reqBytes)
|
||||
}
|
||||
|
||||
// Route handlers
|
||||
|
||||
type BitesList struct {
|
||||
Previous uint64 `json:"previous"` // One bite before starts. Hint for how many steps the client can skip
|
||||
Starts []uint64 `json:"starts"`
|
||||
Next uint64 `json:"next"` // One bite after starts. Hint for how many steps the client can skip
|
||||
}
|
||||
|
||||
func ScanBites(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
prefix, err := MarshalKeyPrefix("bite", p.ByName("key"))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
from, err := ParseStartString(r.FormValue("from"))
|
||||
from, err := ParseStartString(r.FormValue("from"))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
@ -137,143 +105,88 @@ func ScanBites(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|||
return
|
||||
}
|
||||
|
||||
fromKey, err := MarshalKey("bite", p.ByName("key"), from)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
scanRequest := ScanRequest {
|
||||
Key: p.ByName("key"),
|
||||
From: from,
|
||||
To: to,
|
||||
Type: "bite",
|
||||
}
|
||||
|
||||
bitesList := BitesList{}
|
||||
drBytes, drErr := proto.Marshal(&scanRequest);
|
||||
if drErr != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.View(func(txn *badger.Txn) error {
|
||||
opts := badger.DefaultIteratorOptions
|
||||
opts.PrefetchValues = false
|
||||
opts.Reverse = true
|
||||
it := txn.NewIterator(opts)
|
||||
defer it.Close()
|
||||
msg, natsErr := natsConn.Request("scan_store", drBytes, 10 * 1000 * time.Millisecond) // 10s timeout
|
||||
if natsErr != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch previous key
|
||||
it.Seek(fromKey)
|
||||
if it.ValidForPrefix(fromKey) {
|
||||
// Lazy check to compare key == seeked key
|
||||
it.Next()
|
||||
}
|
||||
if !it.ValidForPrefix(prefix) {
|
||||
return nil
|
||||
}
|
||||
item := it.Item()
|
||||
key := item.Key()
|
||||
res := Response {}
|
||||
if err := proto.Unmarshal(msg.Data, &res); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, _, start, err := ExtractKey(key)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
bitesList.Previous = start
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.View(func(txn *badger.Txn) error {
|
||||
opts := badger.DefaultIteratorOptions
|
||||
opts.PrefetchValues = false
|
||||
it := txn.NewIterator(opts)
|
||||
defer it.Close()
|
||||
|
||||
for it.Seek(fromKey); it.ValidForPrefix(prefix); it.Next() {
|
||||
item := it.Item()
|
||||
key := item.Key()
|
||||
|
||||
_, _, start, err := ExtractKey(key)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if start > to {
|
||||
// A key was found that is greater than to
|
||||
// Save that as next
|
||||
bitesList.Next = start
|
||||
break
|
||||
}
|
||||
|
||||
bitesList.Starts = append(bitesList.Starts, start)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(bitesList)
|
||||
if res.Code == 200 {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(res.Message)
|
||||
} else if len(res.Message) == 0 {
|
||||
http.Error(w, http.StatusText(int(res.Code)), int(res.Code))
|
||||
} else {
|
||||
http.Error(w, string(res.Message), int(res.Code))
|
||||
}
|
||||
}
|
||||
|
||||
func GetBite(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
w.Header().Add("Content-Type", "audio/wav")
|
||||
start, err := ParseStartString(p.ByName("start"))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
key, err := MarshalKey("bite", p.ByName("key"), start)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.View(func(txn *badger.Txn) error {
|
||||
item, err := txn.Get(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = item.Value(func(value []byte) error {
|
||||
w.Write(value)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
Get("bite", w, r, p)
|
||||
}
|
||||
|
||||
func GetBiteUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
start, err := ParseStartString(p.ByName("start"))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
key, err := MarshalKey("user", p.ByName("key"), start)
|
||||
Get("user", w, r, p)
|
||||
}
|
||||
|
||||
func Get(t string, w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
start, err := ParseStartString(p.ByName("start"))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.View(func(txn *badger.Txn) error {
|
||||
item, err := txn.Get(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = item.Value(func(value []byte) error {
|
||||
w.Write(value)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
dataRequest := DataRequest {
|
||||
Key: p.ByName("key"),
|
||||
Start: start,
|
||||
Type: t,
|
||||
}
|
||||
|
||||
drBytes, drErr := proto.Marshal(&dataRequest);
|
||||
if drErr != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
msg, natsErr := natsConn.Request("request_store", drBytes, 10 * 1000 * time.Millisecond) // 10s timeout
|
||||
if natsErr != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
res := Response {}
|
||||
if err := proto.Unmarshal(msg.Data, &res); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.Code == 200 {
|
||||
w.Header().Add("Content-Type", "audio/wav")
|
||||
w.Write(res.Message)
|
||||
} else if len(res.Message) == 0 {
|
||||
http.Error(w, http.StatusText(int(res.Code)), int(res.Code))
|
||||
} else {
|
||||
http.Error(w, string(res.Message), int(res.Code))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: response.proto
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Response struct {
|
||||
Code uint32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
|
||||
Message []byte `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
|
||||
Client *Client `protobuf:"bytes,3,opt,name=client,proto3" json:"client,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Response) Reset() { *m = Response{} }
|
||||
func (m *Response) String() string { return proto.CompactTextString(m) }
|
||||
func (*Response) ProtoMessage() {}
|
||||
func (*Response) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_0fbc901015fa5021, []int{0}
|
||||
}
|
||||
|
||||
func (m *Response) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Response.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Response.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *Response) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Response.Merge(m, src)
|
||||
}
|
||||
func (m *Response) XXX_Size() int {
|
||||
return xxx_messageInfo_Response.Size(m)
|
||||
}
|
||||
func (m *Response) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Response.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Response proto.InternalMessageInfo
|
||||
|
||||
func (m *Response) GetCode() uint32 {
|
||||
if m != nil {
|
||||
return m.Code
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *Response) GetMessage() []byte {
|
||||
if m != nil {
|
||||
return m.Message
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Response) GetClient() *Client {
|
||||
if m != nil {
|
||||
return m.Client
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Response)(nil), "main.Response")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("response.proto", fileDescriptor_0fbc901015fa5021) }
|
||||
|
||||
var fileDescriptor_0fbc901015fa5021 = []byte{
|
||||
// 129 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x4a, 0x2d, 0x2e,
|
||||
0xc8, 0xcf, 0x2b, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc,
|
||||
0x93, 0xe2, 0x49, 0xce, 0xc9, 0x4c, 0xcd, 0x2b, 0x81, 0x88, 0x29, 0xc5, 0x71, 0x71, 0x04, 0x41,
|
||||
0x55, 0x09, 0x09, 0x71, 0xb1, 0x24, 0xe7, 0xa7, 0xa4, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x06,
|
||||
0x81, 0xd9, 0x42, 0x12, 0x5c, 0xec, 0xb9, 0xa9, 0xc5, 0xc5, 0x89, 0xe9, 0xa9, 0x12, 0x4c, 0x0a,
|
||||
0x8c, 0x1a, 0x3c, 0x41, 0x30, 0xae, 0x90, 0x0a, 0x17, 0x1b, 0xc4, 0x24, 0x09, 0x66, 0x05, 0x46,
|
||||
0x0d, 0x6e, 0x23, 0x1e, 0x3d, 0x90, 0xf1, 0x7a, 0xce, 0x60, 0xb1, 0x20, 0xa8, 0x5c, 0x12, 0x1b,
|
||||
0xd8, 0x1a, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x14, 0x8c, 0xe3, 0xa3, 0x8c, 0x00, 0x00,
|
||||
0x00,
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: store/scan_request.proto
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type ScanRequest struct {
|
||||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||
From uint64 `protobuf:"varint,2,opt,name=from,proto3" json:"from,omitempty"`
|
||||
To uint64 `protobuf:"varint,3,opt,name=to,proto3" json:"to,omitempty"`
|
||||
Type string `protobuf:"bytes,4,opt,name=type,proto3" json:"type,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *ScanRequest) Reset() { *m = ScanRequest{} }
|
||||
func (m *ScanRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*ScanRequest) ProtoMessage() {}
|
||||
func (*ScanRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_812dc305bd560f72, []int{0}
|
||||
}
|
||||
|
||||
func (m *ScanRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_ScanRequest.Unmarshal(m, b)
|
||||
}
|
||||
func (m *ScanRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_ScanRequest.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *ScanRequest) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_ScanRequest.Merge(m, src)
|
||||
}
|
||||
func (m *ScanRequest) XXX_Size() int {
|
||||
return xxx_messageInfo_ScanRequest.Size(m)
|
||||
}
|
||||
func (m *ScanRequest) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_ScanRequest.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_ScanRequest proto.InternalMessageInfo
|
||||
|
||||
func (m *ScanRequest) GetKey() string {
|
||||
if m != nil {
|
||||
return m.Key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *ScanRequest) GetFrom() uint64 {
|
||||
if m != nil {
|
||||
return m.From
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *ScanRequest) GetTo() uint64 {
|
||||
if m != nil {
|
||||
return m.To
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *ScanRequest) GetType() string {
|
||||
if m != nil {
|
||||
return m.Type
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*ScanRequest)(nil), "main.ScanRequest")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("store/scan_request.proto", fileDescriptor_812dc305bd560f72) }
|
||||
|
||||
var fileDescriptor_812dc305bd560f72 = []byte{
|
||||
// 129 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x28, 0x2e, 0xc9, 0x2f,
|
||||
0x4a, 0xd5, 0x2f, 0x4e, 0x4e, 0xcc, 0x8b, 0x2f, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0xd1, 0x2b,
|
||||
0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x0a, 0xe7, 0xe2, 0x0e, 0x4e,
|
||||
0x4e, 0xcc, 0x0b, 0x82, 0x48, 0x09, 0x09, 0x70, 0x31, 0x67, 0xa7, 0x56, 0x4a, 0x30, 0x2a, 0x30,
|
||||
0x6a, 0x70, 0x06, 0x81, 0x98, 0x42, 0x42, 0x5c, 0x2c, 0x69, 0x45, 0xf9, 0xb9, 0x12, 0x4c, 0x0a,
|
||||
0x8c, 0x1a, 0x2c, 0x41, 0x60, 0xb6, 0x10, 0x1f, 0x17, 0x53, 0x49, 0xbe, 0x04, 0x33, 0x58, 0x84,
|
||||
0xa9, 0x24, 0x1f, 0xa4, 0xa6, 0xa4, 0xb2, 0x20, 0x55, 0x82, 0x05, 0xac, 0x0d, 0xcc, 0x4e, 0x62,
|
||||
0x03, 0xdb, 0x62, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x2e, 0x77, 0x60, 0x56, 0x81, 0x00, 0x00,
|
||||
0x00,
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: store.proto
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Store struct {
|
||||
Type string `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"`
|
||||
Bite *Bite `protobuf:"bytes,2,opt,name=bite,proto3" json:"bite,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Store) Reset() { *m = Store{} }
|
||||
func (m *Store) String() string { return proto.CompactTextString(m) }
|
||||
func (*Store) ProtoMessage() {}
|
||||
func (*Store) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_98bbca36ef968dfc, []int{0}
|
||||
}
|
||||
|
||||
func (m *Store) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Store.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Store) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Store.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *Store) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Store.Merge(m, src)
|
||||
}
|
||||
func (m *Store) XXX_Size() int {
|
||||
return xxx_messageInfo_Store.Size(m)
|
||||
}
|
||||
func (m *Store) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Store.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Store proto.InternalMessageInfo
|
||||
|
||||
func (m *Store) GetType() string {
|
||||
if m != nil {
|
||||
return m.Type
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Store) GetBite() *Bite {
|
||||
if m != nil {
|
||||
return m.Bite
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Store)(nil), "main.Store")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("store.proto", fileDescriptor_98bbca36ef968dfc) }
|
||||
|
||||
var fileDescriptor_98bbca36ef968dfc = []byte{
|
||||
// 104 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2e, 0x2e, 0xc9, 0x2f,
|
||||
0x4a, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x93, 0xe2, 0x4a,
|
||||
0xca, 0x2c, 0x81, 0x8a, 0x28, 0x59, 0x73, 0xb1, 0x06, 0x83, 0x14, 0x08, 0x09, 0x71, 0xb1, 0x94,
|
||||
0x54, 0x16, 0xa4, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x06, 0x81, 0xd9, 0x42, 0x72, 0x5c, 0x2c,
|
||||
0x20, 0xa5, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0xdc, 0x46, 0x5c, 0x7a, 0x20, 0xdd, 0x7a, 0x4e, 0x99,
|
||||
0x25, 0xa9, 0x41, 0x60, 0xf1, 0x24, 0x36, 0xb0, 0x19, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff,
|
||||
0x84, 0x05, 0xf9, 0x1a, 0x64, 0x00, 0x00, 0x00,
|
||||
}
|
Loading…
Reference in New Issue