/
helpers.go
51 lines (45 loc) · 1.14 KB
/
helpers.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
package cheshire
import (
"crypto/rand"
// "crypto/sha256"
"encoding/base32"
// "fmt"
// "hash"
// "log"
"io"
"strings"
)
// Sends an error response to the channel
func SendError(txn *Txn, code int, message string) (int, error) {
// log.Printf("SEND ERROR: %s : %s",txn.TxnId(), message)
resp := NewError(txn, code, message)
c, err := txn.Write(resp)
return c, err
}
// sends a standard 200 success response.
// will close the current transaction
func SendSuccess(txn *Txn) {
// log.Printf("SEND SUCCESS: %s", txn.TxnId())
txn.Write(NewResponse(txn))
}
// Creates a new response based on this request txn.
// auto fills the txn id
func NewResponse(txn RequestTxnId) *Response {
response := newResponse()
response.SetTxnId(txn.TxnId())
return response
}
func NewError(txn RequestTxnId, code int, message string) *Response {
response := NewResponse(txn)
response.SetStatus(code, message)
return response
}
func RandString(length int) string {
k := make([]byte, length*2)
if _, err := io.ReadFull(rand.Reader, k); err != nil {
return ""
}
str := strings.TrimRight(
base32.StdEncoding.EncodeToString(k), "=")
return str[:length]
}