Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit ac94b2f

Browse files
committed
Revert "API Gateway data format support, #23"
This reverts commit 5740205.
1 parent 5740205 commit ac94b2f

File tree

1 file changed

+23
-50
lines changed

1 file changed

+23
-50
lines changed

main.go

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package main
1616

1717
import (
18-
"encoding/json"
1918
"fmt"
2019
"io/ioutil"
2120
"log"
@@ -35,12 +34,9 @@ const (
3534
)
3635

3736
type message struct {
38-
ID string
39-
Deadline int64
40-
StatusCode int
41-
Headers map[string]interface{}
42-
IsBase64Encoded bool
43-
Body string
37+
id string
38+
deadline int64
39+
data []byte
4440
}
4541

4642
var (
@@ -86,37 +82,32 @@ func newTask(w http.ResponseWriter, r *http.Request) {
8682

8783
now := time.Now().UnixNano()
8884
task := message{
89-
ID: fmt.Sprintf("%d", now),
90-
Deadline: now + functionTTL,
91-
Body: string(body),
85+
id: fmt.Sprintf("%d", now),
86+
deadline: now + functionTTL,
87+
data: body,
9288
}
93-
fmt.Printf("<- %s %s\n", task.ID, task.Body)
89+
fmt.Printf("<- %s %s\n", task.id, task.data)
9490

9591
resultsChannel := make(chan message)
9692
mutex.Lock()
97-
results[task.ID] = resultsChannel
93+
results[task.id] = resultsChannel
9894
mutex.Unlock()
9995
defer close(resultsChannel)
10096

10197
tasks <- task
10298

10399
select {
104100
case <-time.After(time.Duration(functionTTL)):
105-
fmt.Printf("-> ! %s Deadline is reached\n", task.ID)
101+
fmt.Printf("-> ! %s Deadline is reached\n", task.id)
106102
w.WriteHeader(http.StatusGone)
107-
w.Write([]byte(fmt.Sprintf("Deadline is reached, data %s", task.Body)))
103+
w.Write([]byte(fmt.Sprintf("Deadline is reached, data %s", task.data)))
108104
case result := <-resultsChannel:
109-
fmt.Printf("-> %s %d %s\n", result.ID, result.StatusCode, result.Body)
110-
for k, v := range result.Headers {
111-
if value, ok := v.(string); ok {
112-
w.Header().Add(k, value)
113-
}
114-
}
115-
w.WriteHeader(result.StatusCode)
116-
w.Write([]byte(result.Body))
105+
fmt.Printf("-> %s %s\n", result.id, result.data)
106+
w.WriteHeader(http.StatusOK)
107+
w.Write(result.data)
117108
}
118109
mutex.Lock()
119-
delete(results, task.ID)
110+
delete(results, task.id)
120111
mutex.Unlock()
121112
return
122113
}
@@ -125,13 +116,13 @@ func getTask(w http.ResponseWriter, r *http.Request) {
125116
task := <-tasks
126117

127118
// Dummy headers required by Rust client. Replace with something meaningful
128-
w.Header().Set("Lambda-Runtime-Aws-Request-Id", task.ID)
129-
w.Header().Set("Lambda-Runtime-Deadline-Ms", strconv.Itoa(int(task.Deadline)))
119+
w.Header().Set("Lambda-Runtime-Aws-Request-Id", task.id)
120+
w.Header().Set("Lambda-Runtime-Deadline-Ms", strconv.Itoa(int(task.deadline)))
130121
w.Header().Set("Lambda-Runtime-Invoked-Function-Arn", "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime")
131122
w.Header().Set("Lambda-Runtime-Trace-Id", "0")
132123

133124
w.WriteHeader(http.StatusOK)
134-
w.Write([]byte(task.Body))
125+
w.Write(task.data)
135126
return
136127
}
137128

@@ -163,7 +154,7 @@ func responseHandler(w http.ResponseWriter, r *http.Request) {
163154
return
164155
}
165156

166-
body, err := ioutil.ReadAll(r.Body)
157+
data, err := ioutil.ReadAll(r.Body)
167158
if err != nil {
168159
fmt.Printf("! %s\n", err)
169160
return
@@ -179,41 +170,23 @@ func responseHandler(w http.ResponseWriter, r *http.Request) {
179170
return
180171
}
181172

182-
result := message{
183-
Body: string(body),
184-
StatusCode: 200,
185-
}
186173
switch kind {
187174
case "response":
188-
if js, ok := parseJSON(body); ok {
189-
result = js
190-
}
191175
case "error":
192-
result.StatusCode = 500
193-
fmt.Printf("! Error: %s\n", body)
176+
fmt.Printf("! Error: %s\n", data)
194177
default:
195178
w.WriteHeader(http.StatusNotFound)
196179
w.Write([]byte(fmt.Sprintf("Unknown endpoint: %s", kind)))
197180
return
198181
}
199-
result.ID = id
200-
resultsChannel <- result
182+
resultsChannel <- message{
183+
id: id,
184+
data: data,
185+
}
201186
w.WriteHeader(http.StatusAccepted)
202187
return
203188
}
204189

205-
func parseJSON(s []byte) (message, bool) {
206-
var js message
207-
if err := json.Unmarshal(s, &js); err != nil {
208-
fmt.Println(s, err)
209-
return js, false
210-
}
211-
if js.StatusCode == 0 {
212-
return js, false
213-
}
214-
return js, true
215-
}
216-
217190
func api() error {
218191
apiRouter := http.NewServeMux()
219192
apiRouter.HandleFunc(awsEndpoint+"/init/error", initError)

0 commit comments

Comments
 (0)