Write data in database table

This commit is contained in:
2020-09-02 09:02:30 +02:00
parent 8f7a285289
commit ac3bb1ebe9
7 changed files with 106 additions and 74 deletions

31
env/env.go vendored Normal file
View 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)
}
}