本文整理匯總了Golang中github.com/mainflux/mainflux/db.MgoDb類的典型用法代碼示例。如果您正苦於以下問題:Golang MgoDb類的具體用法?Golang MgoDb怎麽用?Golang MgoDb使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了MgoDb類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UpdateChannel
/**
* UpdateChannel()
*/
func UpdateChannel(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
if len(data) == 0 {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "no data provided"}`
io.WriteString(w, str)
return
}
var body map[string]interface{}
if err := json.Unmarshal(data, &body); err != nil {
panic(err)
}
/**
if validateJsonSchema("channel", body) != true {
println("Invalid schema")
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "invalid json schema in request"}`
io.WriteString(w, str)
return
}
**/
id := bone.GetValue(r, "channel_id")
// 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.Wait()
// Wait on status from MQTT handler (which executes DB write)
status := <-clients.WriteStatusChannel
w.WriteHeader(status.Nb)
str := `{"response": "` + status.Str + `"}`
io.WriteString(w, str)
}
示例2: GetDevice
/**
* GetDevice()
*/
func GetDevice(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
id := bone.GetValue(r, "device_id")
result := models.Device{}
err := Db.C("devices").Find(bson.M{"id": id}).One(&result)
if err != nil {
log.Print(err)
w.WriteHeader(http.StatusNotFound)
str := `{"response": "not found", "id": "` + id + `"}`
if err != nil {
log.Print(err)
}
io.WriteString(w, str)
return
}
w.WriteHeader(http.StatusOK)
res, err := json.Marshal(result)
if err != nil {
log.Print(err)
}
io.WriteString(w, string(res))
}
示例3: GetChannels
/**
* GetChannels()
*/
func GetChannels(w http.ResponseWriter, r *http.Request) {
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
// Get fileter values from parameters:
// - climit = count limit, limits number of returned `channel` elements
// - vlimit = value limit, limits number of values within the channel
var climit, vlimit int
var err error
s := r.URL.Query().Get("climit")
if len(s) == 0 {
// Set default limit to -5
climit = -100
} else {
climit, err = strconv.Atoi(s)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "wrong count limit"}`
io.WriteString(w, str)
return
}
}
s = r.URL.Query().Get("vlimit")
if len(s) == 0 {
// Set default limit to -5
vlimit = -100
} else {
vlimit, err = strconv.Atoi(s)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "wrong value limit"}`
io.WriteString(w, str)
return
}
}
// 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 {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
res, err := json.Marshal(results)
if err != nil {
log.Print(err)
}
io.WriteString(w, string(res))
}
示例4: GetDevices
/**
* GetDevices()
*/
func GetDevices(w http.ResponseWriter, r *http.Request) {
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
results := []models.Device{}
if err := Db.C("devices").Find(nil).All(&results); err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
res, err := json.Marshal(results)
if err != nil {
log.Print(err)
}
io.WriteString(w, string(res))
}
示例5: GetChannel
/**
* GetChannel()
*/
func GetChannel(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
id := bone.GetValue(r, "channel_id")
var vlimit int
var err error
s := r.URL.Query().Get("vlimit")
if len(s) == 0 {
// Set default limit to -5
vlimit = -5
} else {
vlimit, err = strconv.Atoi(s)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "wrong limit"}`
io.WriteString(w, str)
return
}
}
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 {
log.Print(err)
w.WriteHeader(http.StatusNotFound)
str := `{"response": "not found", "id": "` + id + `"}`
io.WriteString(w, str)
return
}
w.WriteHeader(http.StatusOK)
res, err := json.Marshal(result)
if err != nil {
log.Print(err)
}
io.WriteString(w, string(res))
}
示例6: DeleteChannel
/**
* DeleteChannel()
*/
func DeleteChannel(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
id := bone.GetValue(r, "channel_id")
err := Db.C("channels").Remove(bson.M{"id": id})
if err != nil {
log.Print(err)
w.WriteHeader(http.StatusNotFound)
str := `{"response": "not deleted", "id": "` + id + `"}`
io.WriteString(w, str)
return
}
w.WriteHeader(http.StatusOK)
str := `{"response": "deleted", "id": "` + id + `"}`
io.WriteString(w, str)
}
示例7: CreateChannel
/**
* CreateChannel ()
*/
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 len(data) == 0 {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "no data (Device ID) provided"}`
io.WriteString(w, str)
return
}
var body map[string]interface{}
if err := json.Unmarshal(data, &body); err != nil {
panic(err)
}
/**
if validateJsonSchema("channel", 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
// and get the "channels" collection
// from this new session
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
c := models.Channel{}
if err := json.Unmarshal(data, &c); err != nil {
panic(err)
}
// Creating UUID Version 4
uuid := uuid.NewV4()
fmt.Println(uuid.String())
c.Id = uuid.String()
// Insert reference to DeviceId
if len(c.Device) == 0 {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "no device ID provided in request"}`
io.WriteString(w, str)
return
}
// TODO Check if Device ID is valid (in database)
// Timestamp
t := time.Now().UTC().Format(time.RFC3339)
c.Created, c.Updated = t, t
// Insert Channel
if err := Db.C("channels").Insert(c); err != nil {
w.WriteHeader(http.StatusInternalServerError)
str := `{"response": "cannot create channel"}`
io.WriteString(w, str)
return
}
w.WriteHeader(http.StatusOK)
str := `{"response": "created", "id": "` + c.Id + `"}`
io.WriteString(w, str)
}
示例8: CreateDevice
/**
* CreateDevice ()
*/
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 {
println("HERE")
panic(err)
}
if len(data) == 0 {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "no data provided"}`
io.WriteString(w, str)
return
}
var body map[string]interface{}
if err := json.Unmarshal(data, &body); err != nil {
panic(err)
}
/*
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
// and get the "devices" collection
// from this new session
Db := db.MgoDb{}
Db.Init()
defer Db.Close()
// Set up defaults and pick up new values from user-provided JSON
d := models.Device{Name: "Some Name", Online: false}
if err := json.Unmarshal(data, &d); err != nil {
println("Cannot decode!")
log.Print(err.Error())
panic(err)
}
// Creating UUID Version 4
uuid := uuid.NewV4()
fmt.Println(uuid.String())
d.Id = uuid.String()
// Timestamp
t := time.Now().UTC().Format(time.RFC3339)
d.Created, d.Updated = t, t
// Insert Device
if err := Db.C("devices").Insert(d); err != nil {
w.WriteHeader(http.StatusInternalServerError)
str := `{"response": "cannot create device"}`
io.WriteString(w, str)
return
}
w.WriteHeader(http.StatusOK)
str := `{"response": "created", "id": "` + d.Id + `"}`
io.WriteString(w, str)
}
示例9: UpdateDevice
/**
* UpdateDevice()
*/
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 {
println("HERE")
panic(err)
}
if len(data) == 0 {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "no data provided"}`
io.WriteString(w, str)
return
}
var body map[string]interface{}
if err := json.Unmarshal(data, &body); err != nil {
panic(err)
}
/*
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()
id := bone.GetValue(r, "device_id")
// Check if someone is trying to change "id" key
// and protect us from this
if _, ok := body["id"]; ok {
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "invalid request: device id is read-only"}`
io.WriteString(w, str)
return
}
if _, ok := body["created"]; ok {
println("Error: can not change device")
w.WriteHeader(http.StatusBadRequest)
str := `{"response": "invalid request: 'created' is read-only"}`
io.WriteString(w, str)
return
}
// Timestamp
t := time.Now().UTC().Format(time.RFC3339)
body["updated"] = t
colQuerier := bson.M{"id": id}
change := bson.M{"$set": body}
if err := Db.C("devices").Update(colQuerier, change); err != nil {
log.Print(err)
w.WriteHeader(http.StatusNotFound)
str := `{"response": "not updated", "id": "` + id + `"}`
io.WriteString(w, str)
return
}
w.WriteHeader(http.StatusOK)
str := `{"response": "updated", "id": "` + id + `"}`
io.WriteString(w, str)
}