Created
August 13, 2018 04:03
-
-
Save nguyenvanduocit/d82538a5fc8800ec0b6d99506a22b573 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (h *Handler) Handle(w http.ResponseWriter, r *http.Request){ | |
cmd := exec.Command(h.Command) | |
cmd.Dir = h.WorkingDir | |
cmd.Env = append(h.Env, fmt.Sprintf("FN_REQUEST_URL=%s", r.URL.RequestURI())) | |
requestContent, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
fmt.Fprint(w, err) | |
return | |
} | |
defer r.Body.Close() | |
requestBody := utils.JsonIn{ | |
Body: string(requestContent), | |
ContentType: r.Header.Get("Content-Type"), | |
Protocol: utils.CallRequestHTTP{ | |
RequestURL: r.RequestURI, | |
Method: r.Method, | |
Headers: r.Header, | |
}, | |
} | |
bRequestBody, err := json.Marshal(requestBody) | |
if err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
fmt.Fprint(w, err) | |
return | |
} | |
cmd.Stdin = strings.NewReader(string(bRequestBody)) | |
var out bytes.Buffer | |
cmd.Stdout = &out | |
if err := cmd.Run(); err != nil { | |
w.WriteHeader(http.StatusInternalServerError) | |
fmt.Fprint(w, err) | |
return | |
} | |
var response utils.JsonOut | |
if err := json.Unmarshal(out.Bytes(), &response); err != nil { | |
fmt.Fprint(w, err) | |
return | |
} | |
for headerKey, headerValues := range response.Protocol.Headers { | |
for _, value := range headerValues { | |
w.Header().Add(headerKey, value) | |
} | |
} | |
w.WriteHeader(response.Protocol.StatusCode) | |
fmt.Fprint(w, response.Body) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "/location/geocode", | |
"endpoint": "/location/geocode", | |
"Env": [ | |
"CLIENT=mapbox", | |
"MAPBOX_ACCESS_TOKEN=abc", | |
"FN_FORMAT=json", | |
"FN_TYPE=sync" | |
], | |
"working_dir": "/Users/duocnguyen/go/src/code.go1.com.au/microservices/location", | |
"command": "/usr/local/bin/go run /Users/duocnguyen/go/src/code.go1.com.au/microservices/location/geocode/func.go" | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment