mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-26 13:48:53 +08:00
Gofmt
Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
This commit is contained in:
parent
c12a88d12f
commit
d38ea79dde
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
12
main.go
12
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 {
|
||||
|
@ -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"`
|
||||
}
|
||||
)
|
||||
|
@ -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"`
|
||||
}
|
||||
)
|
||||
|
22
opts.go
22
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
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user