-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
175 lines (140 loc) · 3.8 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
var lambdaClient *lambda.Client
var s3Client *s3.Client
const (
funcName = "ccp-proj3"
inputBucket = "cse546proj3-input"
outputBucket = "cse546proj3-output"
)
type S3Event struct {
Detail struct {
Bucket `json:"bucket"`
Object `json:"object"`
} `json:"detail"`
}
type Bucket struct {
Name string `json:"name"`
}
type Object struct {
Key string `json:"key"`
}
type Records struct {
S3 `json:"s3"`
}
type Events struct {
Recs []Records `json:"Records"`
}
func addAuth(handler http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if ok && user == "abc" && pass == "def" {
handler.ServeHTTP(w, r)
return
}
log.Println(user, pass)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
}
type S3 struct {
Bucket `json:"bucket"`
Object `json:"object"`
}
func Eventhandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var event S3Event
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Error occured while reading request body", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// log.Println(string(data))
if err := json.Unmarshal(data, &event); err != nil {
log.Println("Error while decoding json data", err.Error())
return
}
log.Printf("Received event Bucket: %s, Object: %s\n", event.Detail.Bucket.Name, event.Detail.Object.Key)
if event.Detail.Bucket.Name == inputBucket {
name := funcName
data, err := json.Marshal(Events{
Recs: []Records{
{S3: S3{event.Detail.Bucket, event.Detail.Object}},
},
})
if err != nil {
log.Println("Unable to make json")
w.WriteHeader(http.StatusInternalServerError)
return
}
// log.Println(string(data))
params := lambda.InvokeInput{
FunctionName: &name,
InvocationType: types.InvocationTypeEvent,
Payload: data,
}
if _, err := lambdaClient.Invoke(r.Context(), ¶ms); err != nil {
log.Println("Error while invoking lambda function", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Println("Lambda invoked successfully!")
w.WriteHeader(http.StatusOK)
} else if event.Detail.Bucket.Name == outputBucket {
res, err := s3Client.GetObject(r.Context(), &s3.GetObjectInput{
Bucket: &event.Detail.Bucket.Name,
Key: &event.Detail.Object.Key,
})
if err != nil {
log.Println("Error getting object from bucket")
w.WriteHeader(http.StatusInternalServerError)
return
}
defer res.Body.Close()
output, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println("Error reading s3 response body")
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Printf("Output for image: %s is %s\n", event.Detail.Object.Key, string(output))
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
func runServer(addr string, port int) {
http.HandleFunc("/", addAuth(Eventhandler))
log.Printf("starting server at %s:%d\n", addr, port)
http.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), nil)
}
func main() {
var port int
var addr string
flag.IntVar(&port, "p", 12500, "Provide a port number")
flag.StringVar(&addr, "addr", "0.0.0.0", "Provide an address to host server")
flag.Parse()
// aws config
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("Unable to load SDK config %v\n", err)
}
lambdaClient = lambda.NewFromConfig(cfg)
s3Client = s3.NewFromConfig(cfg)
runServer(addr, port)
}