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.
112 lines
2.9 KiB
112 lines
2.9 KiB
package main |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
"io" |
|
"log" |
|
"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/gorilla/mux" |
|
) |
|
|
|
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) { |
|
// Execute the template per HTTP request |
|
err := tplHome.ExecuteWriter(pongo2.Context{"query": "dddd"}, w) |
|
if err != nil { |
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
|
} |
|
} |
|
|
|
func healthCheckHandler(w http.ResponseWriter, r *http.Request) { |
|
// A very simple health check. |
|
w.WriteHeader(http.StatusOK) |
|
w.Header().Set("Content-Type", "application/json") |
|
|
|
// In the future we could report back on the status of our DB, or our cache |
|
// (e.g. Redis) by performing a simple PING, and include them in the response. |
|
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() { |
|
|
|
// Initialize a session in us-west-2 that the SDK will use to load |
|
// credentials from the shared credentials file ~/.aws/credentials. |
|
sess, err = session.NewSession(&aws.Config{ |
|
Region: aws.String("ap-southeast-1")}, |
|
) |
|
if err != nil { |
|
fmt.Println("aws error ") |
|
fmt.Println(err.Error()) |
|
os.Exit(1) |
|
} |
|
|
|
// Create DynamoDB client |
|
svc = dynamodb.New(sess) |
|
|
|
// func main() { |
|
// session, err := r.Connect(r.ConnectOpts{ |
|
// Address: "localhost:28015", |
|
// Database: "rtcom", |
|
// }) |
|
// if err != nil { |
|
// log.Panic(err.Error()) |
|
// } |
|
// router := NewRouter() |
|
|
|
// router.Handle("channel add", addChannel) |
|
// router.Handle("channel subscribe", subscribeChannel) |
|
|
|
// http.Handle("/", router) |
|
// http.ListenAndServe(":4000", nil) |
|
r := mux.NewRouter() |
|
// r.HandleFunc("/", HomeHandler) |
|
// r.HandleFunc("/products", ProductsHandler) |
|
// r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT") |
|
// r.HandleFunc("/authors", handler).Queries("surname", "{surname}") |
|
r.HandleFunc("/health", healthCheckHandler) |
|
r.HandleFunc("/id/{id}", oneDeviceHandler) |
|
r.HandleFunc("/all", overallHandler) |
|
r.HandleFunc("/", indexHandler) |
|
log.Fatal(http.ListenAndServe(":8000", r)) |
|
}
|
|
|