-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathGoCustomHandlers.go
213 lines (182 loc) · 5.97 KB
/
GoCustomHandlers.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
)
type ReturnValue struct {
Data string
}
type InvokeResponse struct {
Outputs map[string]interface{}
Logs []string
ReturnValue interface{}
}
type InvokeResponseStringReturnValue struct {
Outputs map[string]interface{}
Logs []string
ReturnValue string
}
type InvokeRequest struct {
Data map[string]interface{}
Metadata map[string]interface{}
}
func queueTriggerHandler(w http.ResponseWriter, r *http.Request) {
var invokeReq InvokeRequest
d := json.NewDecoder(r.Body)
decodeErr := d.Decode(&invokeReq)
if decodeErr != nil {
http.Error(w, decodeErr.Error(), http.StatusBadRequest)
return
}
fmt.Println("The JSON data is:invokeReq metadata......")
fmt.Println(invokeReq.Metadata)
fmt.Println("The JSON data is:invokeReq data......")
fmt.Println(invokeReq.Data)
returnValue := "HelloWorld"
invokeResponse := InvokeResponse{Logs: []string{"test log1", "test log2"}, ReturnValue: returnValue}
js, err := json.Marshal(invokeResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func blobTriggerHandler(w http.ResponseWriter, r *http.Request) {
var invokeReq InvokeRequest
d := json.NewDecoder(r.Body)
decodeErr := d.Decode(&invokeReq)
if decodeErr != nil {
// bad JSON or unrecognized json field
http.Error(w, decodeErr.Error(), http.StatusBadRequest)
return
}
fmt.Println("The JSON data is:invokeReq metadata......")
fmt.Println(invokeReq.Metadata)
returnValue := invokeReq.Data["triggerBlob"]
invokeResponse := InvokeResponse{Logs: []string{"test log1", "test log2"}, ReturnValue: returnValue}
js, err := json.Marshal(invokeResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func queueTriggerWithOutputsHandler(w http.ResponseWriter, r *http.Request) {
var invokeReq InvokeRequest
d := json.NewDecoder(r.Body)
decodeErr := d.Decode(&invokeReq)
if decodeErr != nil {
// bad JSON or unrecognized json field
http.Error(w, decodeErr.Error(), http.StatusBadRequest)
return
}
fmt.Println("The JSON data is:invokeReq metadata......")
fmt.Println(invokeReq.Metadata)
fmt.Println("The JSON data is:invokeReq data......")
fmt.Println(invokeReq.Data)
returnValue := 100
outputs := make(map[string]interface{})
outputs["output1"] = "output from go"
invokeResponse := InvokeResponse{outputs, []string{"test log1", "test log2"}, returnValue}
js, err := json.Marshal(invokeResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func httpTriggerHandler(w http.ResponseWriter, r *http.Request) {
t := time.Now()
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Year())
ua := r.Header.Get("User-Agent")
fmt.Printf("user agent is: %s \n", ua)
invocationid := r.Header.Get("X-Azure-Functions-InvocationId")
fmt.Printf("invocationid is: %s \n", invocationid)
//w.Write([]byte("Hello World from go worker:pgopa"))
returnValue := ReturnValue{Data: "return val"}
outputs := make(map[string]interface{})
outputs["output"] = "Mark Taylor"
outputs["output2"] = map[string]interface{}{
"home": "123-466-799",
"office": "564-987-654",
}
invokeResponse := InvokeResponse{outputs, []string{"test log1", "test log2"}, returnValue}
js, err := json.Marshal(invokeResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func httpTriggerHandlerStringReturnValue(w http.ResponseWriter, r *http.Request) {
t := time.Now()
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Year())
ua := r.Header.Get("User-Agent")
fmt.Printf("user agent is: %s \n", ua)
invocationid := r.Header.Get("X-Azure-Functions-InvocationId")
fmt.Printf("invocationid is: %s \n", invocationid)
outputs := make(map[string]interface{})
outputs["output"] = "Mark Taylor"
outputs["output2"] = map[string]interface{}{
"home": "123-466-799",
"office": "564-987-654",
}
headers := make(map[string]interface{})
headers["header1"] = "header1Val"
headers["header2"] = "header2Val"
res := make(map[string]interface{})
res["statusCode"] = "201"
res["body"] = "my world"
res["headers"] = headers
outputs["res"] = res
invokeResponse := InvokeResponseStringReturnValue{outputs, []string{"test log1", "test log2"}, "Hello,World"}
js, err := json.Marshal(invokeResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func simpleHttpTriggerHandler(w http.ResponseWriter, r *http.Request) {
t := time.Now()
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Year())
ua := r.Header.Get("User-Agent")
fmt.Printf("user agent is: %s \n", ua)
invocationid := r.Header.Get("X-Azure-Functions-InvocationId")
fmt.Printf("invocationid is: %s \n", invocationid)
queryParams := r.URL.Query()
for k, v := range queryParams {
fmt.Println("k:", k, "v:", v)
}
w.Write([]byte("Hello World from go worker"))
}
func main() {
customHandlerPort, exists := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT")
if exists {
fmt.Println("FUNCTIONS_CUSTOMHANDLER_PORT: " + customHandlerPort)
}
mux := http.NewServeMux()
mux.HandleFunc("/HttpTriggerStringReturnValue", httpTriggerHandlerStringReturnValue)
mux.HandleFunc("/QueueTrigger", queueTriggerHandler)
mux.HandleFunc("/BlobTrigger", blobTriggerHandler)
mux.HandleFunc("/QueueTriggerWithOutputs", queueTriggerWithOutputsHandler)
mux.HandleFunc("/api/SimpleHttpTrigger", simpleHttpTriggerHandler)
mux.HandleFunc("/api/SimpleHttpTriggerWithReturn", simpleHttpTriggerHandler)
fmt.Println("Go server Listening...on FUNCTIONS_CUSTOMHANDLER_PORT:", customHandlerPort)
log.Fatal(http.ListenAndServe(":"+customHandlerPort, mux))
}