Adapt worker server for base64 client response

This commit is contained in:
Bilal
2020-05-07 10:03:03 +02:00
parent 6011863525
commit a77b46aad6

View File

@@ -2,10 +2,12 @@ package workerserver
import ( import (
"bufio" "bufio"
b64 "encoding/base64"
"gitlab.com/saburly/kiviscraplib/structures" "gitlab.com/saburly/kiviscraplib/structures"
"log" "log"
"math/rand" "math/rand"
"net" "net"
"strings"
"time" "time"
) )
@@ -53,6 +55,8 @@ func handleConnection(conn net.Conn) {
Req: make(chan structures.Request), Req: make(chan structures.Request),
} }
log.Printf("New worker client connected from %s\n", clientAddr)
workers <- workerDescription // add new worker to the end of the queue workers <- workerDescription // add new worker to the end of the queue
for { for {
request := <-workerDescription.Req request := <-workerDescription.Req
@@ -65,7 +69,7 @@ func handleConnection(conn net.Conn) {
return // don't return worker to the queue - not healthy return // don't return worker to the queue - not healthy
} }
bufferBytes, err := bufio.NewReader(conn).ReadBytes('\n') bufferString, err := bufio.NewReader(conn).ReadString('\n')
if err != nil { if err != nil {
log.Println("client left..") log.Println("client left..")
conn.Close() conn.Close()
@@ -73,10 +77,12 @@ func handleConnection(conn net.Conn) {
return // don't return worker to the queue - not healthy return // don't return worker to the queue - not healthy
} }
decodedPageBody, _ := b64.StdEncoding.DecodeString(strings.TrimSpace(bufferString))
log.Printf("Recieving response from %s\n", clientAddr) log.Printf("Recieving response from %s\n", clientAddr)
response := structures.Response{ response := structures.Response{
Url: request.Url, Url: request.Url,
Content: bufferBytes, Content: decodedPageBody,
Err: nil, Err: nil,
} }