-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (57 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"encoding/base64"
"encoding/json"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rekognition"
)
// RequestBody is the parsed body from the post request.
type RequestBody struct {
Image string
}
func main() {
sess := session.New(&aws.Config{
Region: aws.String("us-east-1"),
})
svc := rekognition.New(sess)
http.Handle("/", http.FileServer(http.Dir("./html")))
http.HandleFunc("/check", func(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
http.Error(w, "Request Body Expected", 400)
return
}
var parsed RequestBody
err := json.NewDecoder(r.Body).Decode(&parsed)
if err != nil {
http.Error(w, err.Error(), 400)
}
// Decode the string.
decodedImage, err := base64.StdEncoding.DecodeString(parsed.Image)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// Send request to Rekognition.
input := &rekognition.DetectLabelsInput{
Image: &rekognition.Image{
Bytes: decodedImage,
},
}
result, err := svc.DetectLabels(input)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
output, err := json.Marshal(result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(output)
})
http.ListenAndServe(":3000", nil)
}