From d38ea79dde748a12ac5da327cb429c3b21c4b783 Mon Sep 17 00:00:00 2001 From: Drasko DRASKOVIC Date: Fri, 4 Nov 2016 23:10:13 +0100 Subject: [PATCH] Gofmt Signed-off-by: Drasko DRASKOVIC --- clients/mqtt_client.go | 21 ++++++++------- config/config.go | 21 ++++++++------- controllers/channels.go | 51 ++++++++++++++++++------------------- controllers/devices.go | 51 ++++++++++++++++++------------------- controllers/status.go | 2 +- main.go | 12 ++++----- models/channel.go | 12 ++++----- models/device.go | 10 ++++---- opts.go | 22 ++++++++-------- servers/http_server.go | 8 +++--- servers/http_server_test.go | 47 ++++++++++++++++------------------ 11 files changed, 124 insertions(+), 133 deletions(-) diff --git a/clients/mqtt_client.go b/clients/mqtt_client.go index 02a9d24e..0d182083 100644 --- a/clients/mqtt_client.go +++ b/clients/mqtt_client.go @@ -1,37 +1,37 @@ package clients import ( - "fmt" - "strings" - "time" - "log" "encoding/json" + "fmt" + "log" "net/http" "strconv" + "strings" + "time" + "github.com/mainflux/mainflux/config" "github.com/mainflux/mainflux/db" "github.com/mainflux/mainflux/models" - "github.com/mainflux/mainflux/config" + mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/krylovsk/gosenml" "gopkg.in/mgo.v2/bson" - mqtt "github.com/eclipse/paho.mqtt.golang" ) type ( ChannelWriteStatus struct { - Nb int + Nb int Str string } MqttConn struct { - Opts *mqtt.ClientOptions + Opts *mqtt.ClientOptions Client mqtt.Client } ) var ( - MqttClient mqtt.Client + MqttClient mqtt.Client WriteStatusChannel chan ChannelWriteStatus ) @@ -44,7 +44,7 @@ var msgHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) chanId := s[len(s)-1] status := WriteChannel(chanId, msg.Payload()) - // Send status to HTTP publisher + // Send status to HTTP publisher WriteStatusChannel <- status fmt.Println(status) @@ -75,7 +75,6 @@ func (mqc *MqttConn) MqttSub(cfg config.Config) { WriteStatusChannel = make(chan ChannelWriteStatus) } - /** * WriteChannel() * Generic function that updates the channel value. diff --git a/config/config.go b/config/config.go index 79406cf4..77fd2c9d 100644 --- a/config/config.go +++ b/config/config.go @@ -16,25 +16,24 @@ import ( type Config struct { // HTTP - HttpHost string - HttpPort int + HttpHost string + HttpPort int // Mongo - MongoHost string - MongoPort int - MongoDatabase string + MongoHost string + MongoPort int + MongoDatabase string // MQTT - MqttHost string - MqttPort int + MqttHost string + MqttPort int // Influx - InfluxHost string - InfluxPort int - InfluxDatabase string + InfluxHost string + InfluxPort int + InfluxDatabase string } - func (cfg *Config) Parse() { var confFile string diff --git a/controllers/channels.go b/controllers/channels.go index 9c2f0726..32f0bce9 100644 --- a/controllers/channels.go +++ b/controllers/channels.go @@ -12,19 +12,19 @@ import ( "encoding/json" "fmt" "log" - "time" "strconv" + "time" + "github.com/mainflux/mainflux/clients" "github.com/mainflux/mainflux/db" "github.com/mainflux/mainflux/models" - "github.com/mainflux/mainflux/clients" "github.com/satori/go.uuid" "gopkg.in/mgo.v2/bson" "io" - "net/http" "io/ioutil" + "net/http" "github.com/go-zoo/bone" ) @@ -38,9 +38,9 @@ func CreateChannel(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8") data, err := ioutil.ReadAll(r.Body) - if err != nil { - panic(err) - } + if err != nil { + panic(err) + } if len(data) == 0 { w.WriteHeader(http.StatusBadRequest) @@ -106,7 +106,7 @@ func CreateChannel(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) str := `{"response": "created", "id": "` + c.Id + `"}` - io.WriteString(w, str) + io.WriteString(w, str) } /** @@ -127,7 +127,8 @@ func GetChannels(w http.ResponseWriter, r *http.Request) { // Set default limit to -5 climit = -100 } else { - climit, err = strconv.Atoi(s); if err != nil { + climit, err = strconv.Atoi(s) + if err != nil { w.WriteHeader(http.StatusBadRequest) str := `{"response": "wrong count limit"}` io.WriteString(w, str) @@ -140,7 +141,8 @@ func GetChannels(w http.ResponseWriter, r *http.Request) { // Set default limit to -5 vlimit = -100 } else { - vlimit, err = strconv.Atoi(s); if err != nil { + vlimit, err = strconv.Atoi(s) + if err != nil { w.WriteHeader(http.StatusBadRequest) str := `{"response": "wrong value limit"}` io.WriteString(w, str) @@ -151,8 +153,8 @@ func GetChannels(w http.ResponseWriter, r *http.Request) { // Query DB results := []models.Channel{} if err := Db.C("channels").Find(nil). - Select(bson.M{"values": bson.M{"$slice": vlimit}}). - Sort("-_id").Limit(climit).All(&results); err != nil { + Select(bson.M{"values": bson.M{"$slice": vlimit}}). + Sort("-_id").Limit(climit).All(&results); err != nil { log.Print(err) } @@ -162,7 +164,7 @@ func GetChannels(w http.ResponseWriter, r *http.Request) { if err != nil { log.Print(err) } - io.WriteString(w, string(res)) + io.WriteString(w, string(res)) } /** @@ -177,7 +179,6 @@ func GetChannel(w http.ResponseWriter, r *http.Request) { id := bone.GetValue(r, "channel_id") - var vlimit int var err error s := r.URL.Query().Get("vlimit") @@ -185,7 +186,8 @@ func GetChannel(w http.ResponseWriter, r *http.Request) { // Set default limit to -5 vlimit = -5 } else { - vlimit, err = strconv.Atoi(s); if err != nil { + vlimit, err = strconv.Atoi(s) + if err != nil { w.WriteHeader(http.StatusBadRequest) str := `{"response": "wrong limit"}` io.WriteString(w, str) @@ -195,8 +197,8 @@ func GetChannel(w http.ResponseWriter, r *http.Request) { result := models.Channel{} if err := Db.C("channels").Find(bson.M{"id": id}). - Select(bson.M{"values": bson.M{"$slice": vlimit}}). - One(&result); err != nil { + Select(bson.M{"values": bson.M{"$slice": vlimit}}). + One(&result); err != nil { log.Print(err) w.WriteHeader(http.StatusNotFound) str := `{"response": "not found", "id": "` + id + `"}` @@ -204,13 +206,12 @@ func GetChannel(w http.ResponseWriter, r *http.Request) { return } - w.WriteHeader(http.StatusOK) res, err := json.Marshal(result) if err != nil { log.Print(err) } - io.WriteString(w, string(res)) + io.WriteString(w, string(res)) } /** @@ -224,9 +225,9 @@ func UpdateChannel(w http.ResponseWriter, r *http.Request) { defer Db.Close() data, err := ioutil.ReadAll(r.Body) - if err != nil { - panic(err) - } + if err != nil { + panic(err) + } if len(data) == 0 { w.WriteHeader(http.StatusBadRequest) @@ -255,7 +256,7 @@ func UpdateChannel(w http.ResponseWriter, r *http.Request) { // Publish the channel update. // This will be catched by the MQTT main client (subscribed to all channel topics) // and then written in the DB in the MQTT handler - token := clients.MqttClient.Publish("mainflux/" + id, 0, false, string(data)) + token := clients.MqttClient.Publish("mainflux/"+id, 0, false, string(data)) token.Wait() // Wait on status from MQTT handler (which executes DB write) @@ -278,7 +279,7 @@ func DeleteChannel(w http.ResponseWriter, r *http.Request) { id := bone.GetValue(r, "channel_id") err := Db.C("channels").Remove(bson.M{"id": id}) - if err != nil { + if err != nil { log.Print(err) w.WriteHeader(http.StatusNotFound) str := `{"response": "not deleted", "id": "` + id + `"}` @@ -288,7 +289,5 @@ func DeleteChannel(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) str := `{"response": "deleted", "id": "` + id + `"}` - io.WriteString(w, str) + io.WriteString(w, str) } - - diff --git a/controllers/devices.go b/controllers/devices.go index f6034c66..54ce1210 100644 --- a/controllers/devices.go +++ b/controllers/devices.go @@ -21,8 +21,8 @@ import ( "gopkg.in/mgo.v2/bson" "io" - "net/http" "io/ioutil" + "net/http" "github.com/go-zoo/bone" ) @@ -35,10 +35,10 @@ func CreateDevice(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8") data, err := ioutil.ReadAll(r.Body) - if err != nil { + if err != nil { println("HERE") - panic(err) - } + panic(err) + } if len(data) == 0 { w.WriteHeader(http.StatusBadRequest) @@ -53,13 +53,13 @@ func CreateDevice(w http.ResponseWriter, r *http.Request) { } /* - if validateJsonSchema("device", body) != true { - println("Invalid schema") - w.WriteHeader(http.StatusBadRequest) - str := `{"response": "invalid json schema in request"}` - io.WriteString(w, str) - return - } + if validateJsonSchema("device", body) != true { + println("Invalid schema") + w.WriteHeader(http.StatusBadRequest) + str := `{"response": "invalid json schema in request"}` + io.WriteString(w, str) + return + } */ // Init new Mongo session @@ -97,7 +97,7 @@ func CreateDevice(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) str := `{"response": "created", "id": "` + d.Id + `"}` - io.WriteString(w, str) + io.WriteString(w, str) } /** @@ -119,7 +119,7 @@ func GetDevices(w http.ResponseWriter, r *http.Request) { if err != nil { log.Print(err) } - io.WriteString(w, string(res)) + io.WriteString(w, string(res)) } /** @@ -152,7 +152,7 @@ func GetDevice(w http.ResponseWriter, r *http.Request) { if err != nil { log.Print(err) } - io.WriteString(w, string(res)) + io.WriteString(w, string(res)) } /** @@ -162,10 +162,10 @@ func UpdateDevice(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8") data, err := ioutil.ReadAll(r.Body) - if err != nil { + if err != nil { println("HERE") - panic(err) - } + panic(err) + } if len(data) == 0 { w.WriteHeader(http.StatusBadRequest) @@ -180,16 +180,15 @@ func UpdateDevice(w http.ResponseWriter, r *http.Request) { } /* - if validateJsonSchema("device", body) != true { - println("Invalid schema") - w.WriteHeader(http.StatusBadRequest) - str := `{"response": "invalid json schema in request"}` - io.WriteString(w, str) - return - } + if validateJsonSchema("device", body) != true { + println("Invalid schema") + w.WriteHeader(http.StatusBadRequest) + str := `{"response": "invalid json schema in request"}` + io.WriteString(w, str) + return + } */ - Db := db.MgoDb{} Db.Init() defer Db.Close() @@ -254,5 +253,5 @@ func DeleteDevice(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) str := `{"response": "deleted", "id": "` + id + `"}` - io.WriteString(w, str) + io.WriteString(w, str) } diff --git a/controllers/status.go b/controllers/status.go index 22e2df1c..5433bda2 100644 --- a/controllers/status.go +++ b/controllers/status.go @@ -21,5 +21,5 @@ func GetStatus(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) str := `{"running": true}` - io.WriteString(w, str) + io.WriteString(w, str) } diff --git a/main.go b/main.go index 46f44fac..0c5e010f 100644 --- a/main.go +++ b/main.go @@ -9,17 +9,17 @@ package main import ( + "flag" "fmt" - "os" - "strings" - "strconv" + "github.com/fatih/color" + "github.com/mainflux/mainflux/clients" "github.com/mainflux/mainflux/config" "github.com/mainflux/mainflux/db" "github.com/mainflux/mainflux/servers" - "github.com/mainflux/mainflux/clients" - "github.com/fatih/color" + "os" "runtime" - "flag" + "strconv" + "strings" ) type MainfluxLite struct { diff --git a/models/channel.go b/models/channel.go index d9d5a4a4..d4cc710f 100644 --- a/models/channel.go +++ b/models/channel.go @@ -14,19 +14,19 @@ import ( type ( Channel struct { - Id string `json:"id"` - Device string `json:"device"` + Id string `json:"id"` + Device string `json:"device"` // Name is optional. If present, it is pre-pended to `bn` member of SenML. - Name string `json:"name"` + Name string `json:"name"` // Unit is optional. If present, it is pre-pended to `bu` member of SenML. - Unit string `json:"unit"` + Unit string `json:"unit"` - Values []gosenml.Entry `json:"values"` + Values []gosenml.Entry `json:"values"` Created string `json:"created"` Updated string `json:"updated"` - Metadata map[string]interface{} `json:"metadata"` + Metadata map[string]interface{} `json:"metadata"` } ) diff --git a/models/device.go b/models/device.go index 8741e393..b39652bc 100644 --- a/models/device.go +++ b/models/device.go @@ -13,17 +13,17 @@ type ( Id string `json:"id"` Name string `json:"name"` - Description string `json:"description"` + Description string `json:"description"` - Online bool `json:"online"` - ConnectedAt string `json:"connected_at"` - DisonnectedAt string `json:"disconnected_at"` + Online bool `json:"online"` + ConnectedAt string `json:"connected_at"` + DisonnectedAt string `json:"disconnected_at"` Channels []Channel `json:"channels"` Created string `json:"created"` Updated string `json:"updated"` - Metadata map[string]interface{} `json:"metadata"` + Metadata map[string]interface{} `json:"metadata"` } ) diff --git a/opts.go b/opts.go index 050e3fae..5c8fdc10 100644 --- a/opts.go +++ b/opts.go @@ -10,15 +10,15 @@ package main // Options block for gnatsd server. type Options struct { - Host string - Port int - Trace bool - Debug bool - MaxConn int - Logtime bool - Authorization string - Username string - Password string - PidFile string - LogFile string + Host string + Port int + Trace bool + Debug bool + MaxConn int + Logtime bool + Authorization string + Username string + Password string + PidFile string + LogFile string } diff --git a/servers/http_server.go b/servers/http_server.go index 41b7e512..c04210b0 100644 --- a/servers/http_server.go +++ b/servers/http_server.go @@ -9,17 +9,15 @@ package servers import ( - "strconv" "net/http" + "strconv" - "github.com/mainflux/mainflux/controllers" "github.com/mainflux/mainflux/config" + "github.com/mainflux/mainflux/controllers" "github.com/go-zoo/bone" - ) - func HttpServer(cfg config.Config) { mux := bone.New() @@ -51,7 +49,7 @@ func HttpServer(cfg config.Config) { /** * Server */ - http.ListenAndServe(cfg.HttpHost + ":" + strconv.Itoa(cfg.HttpPort), mux) + http.ListenAndServe(cfg.HttpHost+":"+strconv.Itoa(cfg.HttpPort), mux) // Use following to start HTTPS server on the same port //iris.ListenTLS(cfg.HttpHost + ":" + strconv.Itoa(cfg.HttpPort), "tls/mainflux.crt", "tls/mainflux.key") diff --git a/servers/http_server_test.go b/servers/http_server_test.go index 693f29a6..f0ca44e1 100644 --- a/servers/http_server_test.go +++ b/servers/http_server_test.go @@ -9,12 +9,12 @@ package servers import ( + "log" + "net/http" + "net/http/httptest" + "os" "testing" "time" - "log" - "os" - "net/http" - "net/http/httptest" "github.com/mainflux/mainflux/config" "github.com/mainflux/mainflux/controllers" @@ -24,10 +24,9 @@ import ( "gopkg.in/mgo.v2" ) - func TestMain(m *testing.M) { // We are in testing - notify the program - // so that it is not confused if some other commad line + // so that it is not confused if some other commad line // arguments come in - for example when test is started with `go test -v ./...` // which is what Travis does os.Setenv("TEST_ENV", "1") @@ -75,31 +74,29 @@ func TestServer(t *testing.T) { var cfg config.Config cfg.Parse() - // Create a request to pass to our handler. We don't have any query parameters for now, so we'll - // pass 'nil' as the third parameter. - req, err := http.NewRequest("GET", "/status", nil) - if err != nil { + // pass 'nil' as the third parameter. + req, err := http.NewRequest("GET", "/status", nil) + if err != nil { t.Fatal(err) - } + } - // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. - rr := httptest.NewRecorder() - handler := http.HandlerFunc(controllers.GetStatus) + // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. + rr := httptest.NewRecorder() + handler := http.HandlerFunc(controllers.GetStatus) - // Our handlers satisfy http.Handler, so we can call their ServeHTTP method - // directly and pass in our Request and ResponseRecorder. - handler.ServeHTTP(rr, req) + // Our handlers satisfy http.Handler, so we can call their ServeHTTP method + // directly and pass in our Request and ResponseRecorder. + handler.ServeHTTP(rr, req) - // Check the status code is what we expect. - if status := rr.Code; status != http.StatusOK { + // Check the status code is what we expect. + if status := rr.Code; status != http.StatusOK { t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) - } + } - // Check the response body is what we expect. - expected := `{"running": true}` - if rr.Body.String() != expected { + // Check the response body is what we expect. + expected := `{"running": true}` + if rr.Body.String() != expected { t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected) - } + } } -