Created
October 6, 2016 14:12
-
-
Save esehara/0a0308c5f24506a4ca8cd57c609bacd7 to your computer and use it in GitHub Desktop.
Goで簡単なエコーサーバーを作る ref: http://qiita.com/esehara@github/items/34f8675c61169da06d73
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"net/url" | |
"io/ioutil" | |
) | |
func getRSS(s string) string { | |
u, _ := url.Parse(s) | |
q := u.Query() | |
for k, _ := range u.Query() { q.Del(k) } | |
q.Set("hoge", "fuga") | |
u.RawQuery = q.Encode() | |
return u.String() | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
url := getRSS(r.Form["u"][0]) | |
resp, _ := http.Get(url) | |
defer resp.Body.Close() | |
byteArray, _ := ioutil.ReadAll(resp.Body) | |
fmt.Fprint(w, string(byteArray)) | |
} | |
func main() { | |
fmt.Println("Start Server ...") | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment