Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change json.NewDecoder to json.Unmarshal in binaries #308

Merged
merged 3 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions cmd/apps/skychat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -132,8 +133,14 @@ func handleConn(conn net.Conn) {
}

func messageHandler(w http.ResponseWriter, req *http.Request) {
raw, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

data := map[string]string{}
if err := json.NewDecoder(req.Body).Decode(&data); err != nil {
if err := json.Unmarshal(raw, &data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -171,12 +178,13 @@ func messageHandler(w http.ResponseWriter, req *http.Request) {
go handleConn(conn)
}

_, err := conn.Write([]byte(data["message"]))
if err != nil {
if _, err := conn.Write([]byte(data["message"])); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)

connsMu.Lock()
delete(chatConns, pk)
connsMu.Unlock()

return
}

Expand Down
13 changes: 11 additions & 2 deletions cmd/setup-node/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"io"
"io/ioutil"
"log"
"log/syslog"
"net/http"
Expand Down Expand Up @@ -62,10 +63,18 @@ var rootCmd = &cobra.Command{
}

conf := &setup.Config{}
if err := json.NewDecoder(rdr).Decode(&conf); err != nil {
log.Fatalf("Failed to decode %s: %s", rdr, err)

raw, err := ioutil.ReadAll(rdr)
if err != nil {
logger.Fatalf("Failed to read config: %v", err)
}

if err := json.Unmarshal(raw, &conf); err != nil {
logger.WithField("raw", string(raw)).Fatalf("Failed to decode config: %s", err)
}

logger.Infof("Config: %#v", conf)

sn, err := setup.NewNode(conf, metrics.NewPrometheus("setupnode"))
if err != nil {
logger.Fatal("Failed to create a setup node: ", err)
Expand Down
11 changes: 9 additions & 2 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,17 @@ func (cfg *runCfg) readConfig() *runCfg {
rdr = bufio.NewReader(os.Stdin)
}

if err := json.NewDecoder(rdr).Decode(&cfg.conf); err != nil {
cfg.logger.Fatalf("Failed to decode %s: %s", rdr, err)
raw, err := ioutil.ReadAll(rdr)
if err != nil {
cfg.logger.Fatalf("Failed to read config: %v", err)
}

if err := json.Unmarshal(raw, &cfg.conf); err != nil {
cfg.logger.WithField("raw", string(raw)).Fatalf("Failed to decode config: %s", err)
}

cfg.logger.Infof("Config: %#v", &cfg.conf)

cfg.conf.Path = configPath

return cfg
Expand Down
2 changes: 1 addition & 1 deletion pkg/visor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *Config) flush() error {
return ErrNoConfigPath
}

c.log.Infof("Updating visor config to %+v", c)
c.log.Infof("Updating visor config to %#v", c)

bytes, err := json.MarshalIndent(c, "", "\t")
if err != nil {
Expand Down