From ef19ed40f2c3d1aba7d6f582a8023114fe49130f Mon Sep 17 00:00:00 2001 From: dvdrw Date: Fri, 29 Sep 2023 00:53:18 +0200 Subject: [PATCH] feat: feed data to upstream over REST instead of printing to stdout --- main/main.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/main/main.go b/main/main.go index 5698889..f3dabff 100644 --- a/main/main.go +++ b/main/main.go @@ -1,12 +1,16 @@ package main import ( + "bytes" + "encoding/json" "fmt" - "git.dvdrw.dev/nsmarter/scraper/scraper" "log" + "net/http" "os" "strconv" "strings" + + "git.dvdrw.dev/nsmarter/scraper/scraper" ) var Log = log.Default() @@ -20,6 +24,7 @@ var chunkSize = 5 var apiEndpoint string = "https://online.nsmart.rs/publicapi/v1/announcement/announcement.php" var apiKey string var limitQuerySize = 0 +var fillerUrl string = "localhost" func parseEnvars() { for _, e := range os.Environ() { @@ -47,6 +52,8 @@ func parseEnvars() { apiEndpoint = pair[1] case "API_KEY": apiKey = pair[1] + case "FILLER_URL": + fillerUrl = pair[1] } } } @@ -88,5 +95,22 @@ func main() { for r := range results { fmt.Printf("Received data: %#v\n", r) + + json, err := json.Marshal(r) + if err != nil { + fmt.Print("Couldn't serialise struct to JSON: ", err) + } + + request, err := http.NewRequest("POST", fillerUrl+"/v1/submit", bytes.NewBuffer(json)) + request.Header.Set("Content-Type", "application/json; charset=UTF-8") + + go (func(req *http.Request) { + res, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Print("Error while sending data to filler: ", err) + } else if res.StatusCode != 200 { + fmt.Print("Non-200 while sending data to filler!") + } + })(request) } }