generated from simplism-registry/tpl-go-function
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (46 loc) · 1.17 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
// main package
package main
import (
"encoding/json"
"strings"
"github.com/extism/go-pdk"
)
// RequestData structure (from the request)
type RequestData struct {
Body string `json:"body"`
Header map[string][]string `json:"header"`
Method string `json:"method"`
URI string `json:"uri"`
}
// ResponseData structure (for the response)
type ResponseData struct {
Body string `json:"body"`
Header map[string][]string `json:"header"`
Code int `json:"code"`
}
//export handle
func handle() {
// read request data from the memory
input := pdk.Input()
var requestData RequestData
json.Unmarshal(input, &requestData)
cow := []string{
`^__^`,
`(oo)\_______`,
`(__)\ )\/\`,
` ||----w |`,
` || ||`,
requestData.Body,
}
responseData := ResponseData{
Body: strings.Join(cow, "\n"),
Header: map[string][]string{"Content-Type": {"text/plain; charset=utf-8"}},
Code: 200,
}
// response to Json string
jsonResponse, _ := json.Marshal(responseData)
// copy output to host memory
mem := pdk.AllocateBytes(jsonResponse)
pdk.OutputMemory(mem)
}
func main() {}