From: Waldemar Kozaczuk <jwkozac...@gmail.com> Committer: Waldemar Kozaczuk <jwkozac...@gmail.com> Branch: master
add simple golang httpclient app Signed-off-by: Waldemar Kozaczuk <jwkozac...@gmail.com> --- diff --git a/golang-httpclient/Makefile b/golang-httpclient/Makefile --- a/golang-httpclient/Makefile +++ b/golang-httpclient/Makefile @@ -0,0 +1,9 @@ +.PHONY: module +module: httpclient + echo '/httpclient: $${MODULE_DIR}/httpclient' > usr.manifest + +httpclient: httpclient.go + go build -buildmode=pie -ldflags "-linkmode external" -o httpclient httpclient.go + +clean: + rm -f httpclient usr.manifest diff --git a/golang-httpclient/httpclient.go b/golang-httpclient/httpclient.go --- a/golang-httpclient/httpclient.go +++ b/golang-httpclient/httpclient.go @@ -0,0 +1,60 @@ +package main + +import ( + "bufio" + "fmt" + "net/http" + "os" + "time" + "strconv" +) + +func makeRequest(url string, beforeSleepSec int, afterSleepSec int) { + time.Sleep(time.Duration(beforeSleepSec) * time.Second) + + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + fmt.Println("Response status:", resp.Status) + + scanner := bufio.NewScanner(resp.Body) + for i := 0; scanner.Scan() && i < 5; i++ { + fmt.Println(scanner.Text()) + } + + if err := scanner.Err(); err != nil { + panic(err) + } + + time.Sleep(time.Duration(afterSleepSec) * time.Second) +} + +func main() { + if (len(os.Args) < 2) { + fmt.Println("Usage: httpclient <url> [count] [before_sleep_sec] [after_sleep_sec]") + os.Exit(-1); + } + url := os.Args[1] + + count := 1 + beforeSleepSec := 0 + afterSleepSec := 0 + + if (len(os.Args) >= 3) { + count, _ = strconv.Atoi(os.Args[2]) + } + + if (len(os.Args) >= 4) { + beforeSleepSec, _ = strconv.Atoi(os.Args[3]) + } + + if (len(os.Args) >= 5) { + afterSleepSec, _ = strconv.Atoi(os.Args[4]) + } + + for i := 0; i < count; i++ { + makeRequest(url, beforeSleepSec, afterSleepSec) + } +} diff --git a/golang-httpclient/module.py b/golang-httpclient/module.py --- a/golang-httpclient/module.py +++ b/golang-httpclient/module.py @@ -0,0 +1,3 @@ +from osv.modules import api + +default = api.run(cmdline="/httpclient.so") -- You received this message because you are subscribed to the Google Groups "OSv Development" group. To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/osv-dev/0000000000001c067e05e483ed2b%40google.com.