Write data in database table
This commit is contained in:
31
env/env.go
vendored
Normal file
31
env/env.go
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"mqttListener/config"
|
||||
"mqttListener/models"
|
||||
|
||||
"fmt"
|
||||
//"log"
|
||||
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Env struct {
|
||||
DB models.Datastore
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
func (env *Env) DevicesIndex(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, http.StatusText(405), 405)
|
||||
return
|
||||
}
|
||||
devices, err := env.DB.AllDevices()
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(500), 500)
|
||||
return
|
||||
}
|
||||
for _, device := range devices {
|
||||
fmt.Fprintf(w, "%s, %s, %s, £%.2f\n", device.ID, device.MAC, device.SN, device.LastMsg)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user