Created
October 11, 2017 09:43
-
-
Save s-aska/49772254edc4235211c5f3e925fa598b to your computer and use it in GitHub Desktop.
azure-functions-cli(nodejs) で text/html がエラーになるので proxy 書いた
This file contains hidden or 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
package main | |
import ( | |
"io" | |
"net/http" | |
"strings" | |
) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
res, err := Proxy(r) | |
if err != nil { | |
http.Error(w, err.Error(), 400) | |
return | |
} | |
defer res.Body.Close() | |
if strings.HasPrefix(res.Header.Get("Content-Type"), "text") { | |
res.Header.Set("Content-Type", "text/html") | |
} | |
for key := range res.Header { | |
val := res.Header.Get(key) | |
w.Header().Set(key, val) | |
} | |
w.WriteHeader(res.StatusCode) | |
io.Copy(w, res.Body) | |
}) | |
http.ListenAndServe(":17071", nil) | |
} | |
// Proxy ... | |
func Proxy(r *http.Request) (*http.Response, error) { | |
req, err := http.NewRequest(r.Method, "http://localhost:7071"+r.URL.Path, r.Body) | |
if err != nil { | |
return nil, err | |
} | |
for key := range r.Header { | |
val := r.Header.Get(key) | |
req.Header.Set(key, val) | |
} | |
client := &http.Client{} | |
return client.Do(req) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment