5
0
Fork 0

Split key functions into separate file

master
UnicodingUnicorn 2019-02-10 22:18:24 +08:00
parent 0e4ecf8e51
commit 3087ff7783
2 changed files with 72 additions and 67 deletions

70
key.go Normal file
View File

@ -0,0 +1,70 @@
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
}

69
main.go
View File

@ -1,14 +1,10 @@
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"flag"
"net/http"
"log"
"regexp"
"strconv"
"github.com/dgraph-io/badger"
@ -46,6 +42,7 @@ func main() {
nc, _ := nats.Connect(natsHost);
nc.Subscribe("new_bite", NewBite);
nc.Subscribe("new_bite_user", NewBiteUser);
defer nc.Close()
// Routes
router := httprouter.New()
@ -58,68 +55,6 @@ func main() {
log.Fatal(http.ListenAndServe(listen, router))
}
// 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
}
var ExtractKeyParseError = errors.New("ExtractKey: parse error, possibly because seprator was not found")
func ParseStartString(start string) (uint64, error) {
return strconv.ParseUint(start, 10, 64)
}
@ -176,7 +111,7 @@ func NewBiteUser(m *nats.Msg) {
}
}
// rOUTE HANDLERS
// Route handlers
type BitesList struct {
Previous uint64 `json:"previous"` // One bite before starts. Hint for how many steps the client can skip