Last active
February 27, 2018 08:59
-
-
Save qiujianzhong/4d675adb3f61c0d9f51f6a6daf4c96a4 to your computer and use it in GitHub Desktop.
qiniu ufop-demo
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 ( | |
"bufio" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
"strings" | |
"text/template" | |
"time" | |
) | |
//代码中有很多的bug 欢迎查找..... | |
var BOOTS_INSTANCE_ID string | |
//截取字符串 start 起点下标 end 终点下标(不包括) | |
func Substr(str string, start int, end int) string { | |
rs := []rune(str) | |
length := len(rs) | |
if start < 0 || start > length { | |
return "" | |
} | |
if end < 0 || end > length { | |
return "" | |
} | |
return string(rs[start:end]) | |
} | |
func handler(rw http.ResponseWriter, req *http.Request) { | |
var err error | |
defer func() { | |
if err != nil { | |
http.Error(rw, err.Error(), 500) | |
} | |
}() | |
defer req.Body.Close() | |
var body []byte | |
fmt.Printf("req is :%v\n", req) | |
tempUrl := req.URL.String() | |
fmt.Println("req url is :%s\n", tempUrl) | |
start := strings.Index(tempUrl, "%2F") | |
end := strings.Index(tempUrl, "&") | |
fmt.Printf("start:%d,end:%d\n", start, end) | |
rawStr := Substr(tempUrl, start, end) | |
resultStr := strings.Trim(strings.Replace(rawStr, "%2F", "/", 5), "/") | |
fmt.Printf("raw string is:%s,after replace is:%s\n", rawStr, resultStr) | |
params := strings.Split(resultStr, "/") | |
fmt.Printf("params list is:%v\n,length is:%d\n", params, len(params)) | |
if len(params) < 4 { | |
rw.WriteHeader(400) | |
rw.Write([]byte("params is invalid , please input ufopname/code/200/st/3 \n")) | |
return | |
} | |
code, err := strconv.Atoi(params[1]) | |
if err != nil { | |
rw.WriteHeader(400) | |
rw.Write([]byte("code is invalid, code:" + fmt.Sprintf("%s", params[1]) + "\n")) | |
return | |
} | |
sleepTime, err := strconv.Atoi(params[3]) | |
if err != nil { | |
rw.WriteHeader(400) | |
rw.Write([]byte("sleeptime is invalid, sleepTime:" + fmt.Sprintf("%s", params[3]) + "\n")) | |
return | |
} | |
time.Sleep(time.Duration(sleepTime) * time.Millisecond) | |
code=code/100 | |
if code/200 == 1 { | |
rw.WriteHeader(200) | |
fmt.Printf("code get 2xx, and return status code is 200\n") | |
} else if code/300 == 1 { | |
rw.WriteHeader(301) | |
fmt.Printf("code get 3xx, and return status code is 301\n") | |
} else if code/400 == 1 { | |
rw.WriteHeader(400) | |
fmt.Printf("code get 4xx, and return status code is 400\n") | |
} else if code/500 == 1 { | |
rw.WriteHeader(500) | |
fmt.Printf("code get 5xx, and return status code is 500\n") | |
} | |
tpl, err := template.New("res").Parse(` | |
req body: | |
------------- | |
{{.body}} | |
time: {{.time}} | |
`) | |
if err != nil { | |
panic("") | |
} | |
brw := bufio.NewWriter(rw) | |
length := len(body) | |
tpl.Execute(brw, map[string]interface{}{ | |
"body": string(body), | |
"length": length, | |
"time": time.Now(), | |
}) | |
brw.Flush() | |
reqdata := fmt.Sprintf("\nBOOTS_INSTANCE_ID is: %s \n\nreq data is:%v\n", BOOTS_INSTANCE_ID,req) | |
rw.Write([]byte(reqdata)) | |
} | |
func main() { | |
port := os.Getenv("PORT_HTTP") | |
BOOTS_INSTANCE_ID = os.Getenv("BOOTS_INSTANCE_ID") | |
if port == "" { | |
port = "9100" | |
} | |
http.HandleFunc("/", handler) | |
log.Fatalln(http.ListenAndServe("0.0.0.0:"+port, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ufop-demo/Dockerfile
FROM golang:1.6
ADD . /go/src/qiniu.com/ufop/example/demo
RUN go install qiniu.com/ufop/example/demo
ENTRYPOINT /go/bin/demo