forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (94 loc) · 2.84 KB
/
main.go
File metadata and controls
111 lines (94 loc) · 2.84 KB
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
// Simple testing utility to emulate the internal LocalStack Endpoint
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
log "github.com/sirupsen/logrus"
)
const apiPort = 9563
const listenPort = 48490
var invokeUrl = fmt.Sprintf("http://localhost:%d/invoke", apiPort)
func main() {
// mock for Localstack component
uid := "12345"
router := chi.NewRouter()
router.Use(middleware.Logger)
router.Post("/invocations/{invoke_id}/response", invokeResponseHandler)
router.Post("/invocations/{invoke_id}/error", invokeErrorHandler)
router.Post("/invocations/{invoke_id}/logs", invokeLogsHandler)
router.Post("/status/{runtime_id}/{status}", statusHandler)
router.Get("/test", func(w http.ResponseWriter, r *http.Request) {
invokeRequest, _ := json.Marshal(InvokeRequest{InvokeId: uid, Payload: "{\"counter\":0}"})
_, err := http.Post(invokeUrl, "application/json", bytes.NewReader(invokeRequest))
if err != nil {
log.Error(err)
}
w.WriteHeader(200)
_, err = w.Write([]byte("hi"))
if err != nil {
log.Error(err)
}
})
router.Get("/fail", func(w http.ResponseWriter, r *http.Request) {
invokeRequest, _ := json.Marshal(InvokeRequest{InvokeId: uid, Payload: "{\"counter\":0, \"fail\": \"yes\"}"})
_, err := http.Post(invokeUrl, "application/json", bytes.NewReader(invokeRequest))
if err != nil {
log.Error(err)
}
w.WriteHeader(200)
_, err = w.Write([]byte("hi"))
if err != nil {
log.Error(err)
}
})
log.Infof("Listening on port :%d", listenPort)
err := http.ListenAndServe(fmt.Sprintf(":%d", listenPort), router)
if err != nil {
log.Fatal(err)
}
}
func invokeLogsHandler(w http.ResponseWriter, r *http.Request) {
invokeId := chi.URLParam(r, "invoke_id")
log.Println(invokeId)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Error(err)
}
log.Println("log result: " + string(bodyBytes))
}
type InvokeRequest struct {
InvokeId string `json:"request-id"`
Payload string `json:"payload"`
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
runtime_id := chi.URLParam(r, "runtime_id")
status := chi.URLParam(r, "status")
log.Println(runtime_id + " + " + status)
if status == "ready" {
go func() {
invokeRequest, _ := json.Marshal(InvokeRequest{InvokeId: "12345", Payload: "{\"counter\":0}"})
_, err := http.Post(invokeUrl, "application/json", bytes.NewReader(invokeRequest))
if err != nil {
log.Fatal(err)
}
}()
}
}
func invokeResponseHandler(w http.ResponseWriter, r *http.Request) {
invokeId := chi.URLParam(r, "invoke_id")
log.Println(invokeId)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Error(err)
}
log.Println("result: " + string(bodyBytes))
}
func invokeErrorHandler(w http.ResponseWriter, r *http.Request) {
invokeId := chi.URLParam(r, "invoke_id")
log.Println(invokeId)
}