Created
February 22, 2018 13:33
-
-
Save liangchaoboy/57bdb97e73cbcd7f524b04259f344932 to your computer and use it in GitHub Desktop.
qiniuproxy_log
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" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
) | |
func main() { | |
fileName := "listen.log" | |
logFile, errlog := os.Create(fileName) | |
if errlog != nil { | |
log.Fatalln("open file error !") | |
} | |
mainBugLog := log.New(logFile, "[Debug]", log.LstdFlags) | |
mainBugLog.Println("main message here") | |
fmt.Println("come on...") | |
http.HandleFunc("/xunlei_qiniu", ProxyServer) | |
err := http.ListenAndServe(":80", nil) | |
if err != nil { | |
log.Fatal("ListenAndServe:", err) | |
} | |
} | |
// proxyServer | |
func ProxyServer(w http.ResponseWriter, req *http.Request) { | |
fileName := "ProxyServer.log" | |
logFile, errlog := os.Create(fileName) | |
if errlog != nil { | |
log.Fatalln("open file error !") | |
} | |
proxyBugLog := log.New(logFile, "[Debug]", log.LstdFlags) | |
proxyBugLog.Println("ProxyServer message here") | |
proxyBugLog.Println(req.Method) | |
proxyBugLog.Println(req.Body) | |
rb, _ := ioutil.ReadAll(req.Body) | |
str := strings.Replace(string(rb), "ret=", "", 1) | |
proxyBugLog.Println(str) | |
httpDo(str) | |
} | |
func httpDo(str string) { | |
fileName := "httpDo.log" | |
logFile, errlog := os.Create(fileName) | |
if errlog != nil { | |
log.Fatalln("open file error !") | |
} | |
httpDoBugLog := log.New(logFile, "[Debug]", log.LstdFlags) | |
client := &http.Client{} | |
req, err := http.NewRequest("POST", "http://qiniu.com/path_qiniu", strings.NewReader(str)) | |
if err != nil { | |
httpDoBugLog.Println(err) | |
} | |
req.Header.Set("Content-Type", "text/plain") | |
resp, err := client.Do(req) | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
httpDoBugLog.Println(string(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment