feat: feed data to upstream over REST instead of printing to stdout

This commit is contained in:
dvdrw 2023-09-29 00:53:18 +02:00
parent bcfeabc9c1
commit ef19ed40f2
Signed by: dvdrw
GPG Key ID: 4756FA53D8797D7F
1 changed files with 25 additions and 1 deletions

View File

@ -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)
}
}