Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
feat: trim null bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
CSUwangj committed Apr 11, 2020
1 parent 70d48ac commit fccbacb
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cborplugin/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
Expand All @@ -36,6 +37,7 @@ var log = logging.MustGetLogger("echo")
var logFormat = logging.MustStringFormatter(
"%{level:.4s} %{id:03x} %{message}",
)
var dataDir string

func stringToLogLevel(level string) (logging.Level, error) {
switch level {
Expand Down Expand Up @@ -92,8 +94,21 @@ func requestHandler(response http.ResponseWriter, req *http.Request) {

// send length prefixed CBOR response
reply := cborplugin.Response{
Payload: []byte(`this is test echo file`),
Payload: []byte(`vote recieved`),
}

f, err := os.OpenFile(path.Join(dataDir, "votes"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Error(err)
panic(err)
}
request.Payload = bytes.Trim(request.Payload, "\x00")
if _, err := f.WriteString(string(request.Payload[4:]) + "\n"); err != nil {
log.Error(err)
panic(err)
}
f.Close()
var serialized []byte
enc := codec.NewEncoderBytes(&serialized, cborHandle)
if err := enc.Encode(reply); err != nil {
Expand All @@ -114,6 +129,7 @@ func main() {
var logDir string
flag.StringVar(&logDir, "log_dir", "", "logging directory")
flag.StringVar(&logLevel, "log_level", "DEBUG", "logging level could be set to: DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL")
flag.StringVar(&dataDir, "data_dir", "", "data store directory")
flag.Parse()

level, err := stringToLogLevel(logLevel)
Expand Down

0 comments on commit fccbacb

Please sign in to comment.