|
|
@ -1,15 +1,25 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws" |
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session" |
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/dynamodb" |
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" |
|
|
|
"github.com/flosch/pongo2" |
|
|
|
"github.com/flosch/pongo2" |
|
|
|
"github.com/gorilla/mux" |
|
|
|
"github.com/gorilla/mux" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var tplHome = pongo2.Must(pongo2.FromFile("home.html")) |
|
|
|
var tplHome = pongo2.Must(pongo2.FromFile("home.html")) |
|
|
|
|
|
|
|
var sess *session.Session |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
var svc *dynamodb.DynamoDB |
|
|
|
|
|
|
|
|
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
// Execute the template per HTTP request
|
|
|
|
// Execute the template per HTTP request
|
|
|
@ -29,46 +39,50 @@ func healthCheckHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
io.WriteString(w, `{"alive": true}`) |
|
|
|
io.WriteString(w, `{"alive": true}`) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func overallHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
devices, _ := GetDevices(svc) |
|
|
|
|
|
|
|
var result []Item |
|
|
|
|
|
|
|
for _, i := range devices.Items { |
|
|
|
|
|
|
|
device := Device{} |
|
|
|
|
|
|
|
err := dynamodbattribute.UnmarshalMap(i, &device) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
fmt.Println("Device error unmarshalling:") |
|
|
|
|
|
|
|
fmt.Println(err.Error()) |
|
|
|
|
|
|
|
os.Exit(1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
data, _ := device.History(svc, 1) |
|
|
|
|
|
|
|
result = append(result, data[0]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
b, _ := json.Marshal(result) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
|
|
|
|
io.WriteString(w, string(b)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func oneDeviceHandler(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
|
|
|
|
|
|
|
|
// Initialize a session in us-west-2 that the SDK will use to load
|
|
|
|
// Initialize a session in us-west-2 that the SDK will use to load
|
|
|
|
// credentials from the shared credentials file ~/.aws/credentials.
|
|
|
|
// credentials from the shared credentials file ~/.aws/credentials.
|
|
|
|
// sess, err := session.NewSession(&aws.Config{
|
|
|
|
sess, err = session.NewSession(&aws.Config{ |
|
|
|
// Region: aws.String("ap-southeast-1")},
|
|
|
|
Region: aws.String("ap-southeast-1")}, |
|
|
|
// )
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Create DynamoDB client
|
|
|
|
|
|
|
|
/* svc := dynamodb.New(sess) |
|
|
|
|
|
|
|
devices, err := GetDevices(svc) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
fmt.Println("Got error unmarshalling:") |
|
|
|
fmt.Println("aws error ") |
|
|
|
fmt.Println(err.Error()) |
|
|
|
fmt.Println(err.Error()) |
|
|
|
os.Exit(1) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
} |
|
|
|
for _, i := range devices.Items { |
|
|
|
|
|
|
|
device := Device{} |
|
|
|
// Create DynamoDB client
|
|
|
|
err := dynamodbattribute.UnmarshalMap(i, &device) |
|
|
|
svc = dynamodb.New(sess) |
|
|
|
fmt.Printf(":: Name: %+v (%+v)\n", device.Name, device.Device) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result, err := GetHistory(svc, device.Device, 2) |
|
|
|
|
|
|
|
// Make the DynamoDB Query API call
|
|
|
|
|
|
|
|
// fmt.Printf("result count item: %+v|%+v|", result.Count, result.ScannedCount)
|
|
|
|
|
|
|
|
for _, i := range result.Items { |
|
|
|
|
|
|
|
item := Item{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = dynamodbattribute.UnmarshalMap(i, &item) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
fmt.Println("Got error unmarshalling:") |
|
|
|
|
|
|
|
fmt.Println(err.Error()) |
|
|
|
|
|
|
|
os.Exit(1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fmt.Println("Found item:")
|
|
|
|
|
|
|
|
fmt.Println(" > Timestamp: ", item.Timestamp, |
|
|
|
|
|
|
|
"Data == Temp: ", item.Data.Temperature, |
|
|
|
|
|
|
|
" | moisure: ", item.Data.Moisture) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// func main() {
|
|
|
|
// func main() {
|
|
|
|
// session, err := r.Connect(r.ConnectOpts{
|
|
|
|
// session, err := r.Connect(r.ConnectOpts{
|
|
|
@ -91,6 +105,8 @@ func main() { |
|
|
|
// r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
|
|
|
|
// r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
|
|
|
|
// r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
|
|
|
|
// r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
|
|
|
|
r.HandleFunc("/health", healthCheckHandler) |
|
|
|
r.HandleFunc("/health", healthCheckHandler) |
|
|
|
|
|
|
|
r.HandleFunc("/id/{id}", oneDeviceHandler) |
|
|
|
|
|
|
|
r.HandleFunc("/all", overallHandler) |
|
|
|
r.HandleFunc("/", indexHandler) |
|
|
|
r.HandleFunc("/", indexHandler) |
|
|
|
log.Fatal(http.ListenAndServe(":8000", r)) |
|
|
|
log.Fatal(http.ListenAndServe(":8000", r)) |
|
|
|
} |
|
|
|
} |
|
|
|