You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.0 KiB
119 lines
3.0 KiB
7 years ago
|
// +build linux darwin
|
||
|
|
||
7 years ago
|
package main
|
||
|
|
||
|
import (
|
||
7 years ago
|
"fmt"
|
||
7 years ago
|
"net/http"
|
||
7 years ago
|
"os"
|
||
7 years ago
|
"strconv"
|
||
7 years ago
|
|
||
7 years ago
|
"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"
|
||
7 years ago
|
"github.com/flosch/pongo2"
|
||
7 years ago
|
"github.com/labstack/echo"
|
||
|
"github.com/labstack/echo/middleware"
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
var tplHome = pongo2.Must(pongo2.FromFile("html/home.html"))
|
||
7 years ago
|
var sess *session.Session
|
||
|
var err error
|
||
|
var svc *dynamodb.DynamoDB
|
||
7 years ago
|
|
||
7 years ago
|
// func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
func indexHandler(c echo.Context) error {
|
||
7 years ago
|
// Execute the template per HTTP request
|
||
7 years ago
|
out, err := tplHome.Execute(pongo2.Context{"query": "dddd"})
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
return err
|
||
7 years ago
|
}
|
||
7 years ago
|
return c.HTML(http.StatusOK, out)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
func healthCheckHandler(c echo.Context) error {
|
||
7 years ago
|
// A very simple health check.
|
||
7 years ago
|
return c.JSON(http.StatusOK, `{"alive": true}`)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
func overallHandler(c echo.Context) error {
|
||
7 years ago
|
|
||
|
devices, _ := GetDevices(svc)
|
||
|
var result []Item
|
||
|
for _, i := range devices.Items {
|
||
|
device := Device{}
|
||
|
err := dynamodbattribute.UnmarshalMap(i, &device)
|
||
|
if err != nil {
|
||
7 years ago
|
return err
|
||
7 years ago
|
}
|
||
|
data, _ := device.History(svc, 1)
|
||
7 years ago
|
one := data[0]
|
||
|
one.Info.Name = device.Name
|
||
|
one.Info.Model = device.Model
|
||
7 years ago
|
result = append(result, data[0])
|
||
|
}
|
||
7 years ago
|
return c.JSON(http.StatusOK, result)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// func oneDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
func oneDeviceHandler(c echo.Context) error {
|
||
|
id := c.Param("id")
|
||
7 years ago
|
// default limit is 5
|
||
|
limit, _ := strconv.ParseInt(c.QueryParam("limit"), 10, 64)
|
||
|
if limit == 0 {
|
||
|
limit = 5
|
||
|
}
|
||
|
device, err := GetDevice(svc, id)
|
||
|
if err != nil {
|
||
|
return c.String(http.StatusInternalServerError, "Error unmarshalling")
|
||
|
}
|
||
|
result, err := GetHistory(svc, id, limit)
|
||
7 years ago
|
var ev []Item
|
||
|
if err != nil {
|
||
|
return c.String(http.StatusInternalServerError, "Error unmarshalling")
|
||
|
}
|
||
|
for _, i := range result.Items {
|
||
|
item := Item{}
|
||
|
err = dynamodbattribute.UnmarshalMap(i, &item)
|
||
7 years ago
|
item.Info.Name = device.Name
|
||
|
item.Info.Model = device.Model
|
||
7 years ago
|
if err != nil {
|
||
|
return c.String(http.StatusInternalServerError, "Error unmarshalling")
|
||
|
}
|
||
|
ev = append(ev, item)
|
||
|
}
|
||
7 years ago
|
return c.JSON(http.StatusOK, ev)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
func main() {
|
||
7 years ago
|
|
||
7 years ago
|
// Initialize a session in us-west-2 that the SDK will use to load
|
||
|
// credentials from the shared credentials file ~/.aws/credentials.
|
||
7 years ago
|
sess, err = session.NewSession(&aws.Config{
|
||
|
Region: aws.String("ap-southeast-1")},
|
||
|
)
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
fmt.Println("aws error ")
|
||
7 years ago
|
fmt.Println(err.Error())
|
||
|
os.Exit(1)
|
||
|
}
|
||
7 years ago
|
|
||
|
// Create DynamoDB client
|
||
|
svc = dynamodb.New(sess)
|
||
7 years ago
|
e := echo.New()
|
||
|
e.Use(middleware.Logger())
|
||
|
e.Use(middleware.Recover())
|
||
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||
|
AllowOrigins: []string{"*"},
|
||
|
AllowMethods: []string{"GET", "OPTIONS"},
|
||
|
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
|
||
|
}))
|
||
|
|
||
|
e.GET("/health", healthCheckHandler)
|
||
|
e.GET("/id/:id", oneDeviceHandler)
|
||
|
e.GET("/all", overallHandler)
|
||
|
e.GET("/", indexHandler)
|
||
7 years ago
|
e.Logger.Fatal(e.Start(":31101"))
|
||
7 years ago
|
}
|