Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]: make request in multithreading depending of a word list #4

Closed
wants to merge 10 commits into from
40 changes: 40 additions & 0 deletions cmd/callWorker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"time"
)

type forceData struct {
worker int
wordList string
url string
}

func executeQueryFromFile(data forceData, currentPath chan string) {
for taskData := range currentPath{
queryExecute(data, taskData, "POST")
}
}

func mainRequest(data forceData) {
channel := make(chan string)
wordArray := GetFileContent("../wordList/rootList")

for i := 0 ;i < data.worker; i++ {
go executeQueryFromFile(data, channel)
}
for i := 0; i < len(wordArray); i++ {
channel <- wordArray[i]
}
time.Sleep(1 * time.Second)
close(channel)
}

func main () {
data := forceData {
worker: 3,
wordList: "../wordList/rootList",
url: "http://localhost:3333",
}
mainRequest(data);
}
18 changes: 18 additions & 0 deletions cmd/getFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"os"
"fmt"
"log"
"strings"
)

func GetFileContent(filePath string) []string{
body, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
dataTab := strings.Split(string(body), "\n")
fmt.Println(dataTab[0])
return dataTab
}
35 changes: 35 additions & 0 deletions cmd/queryExecute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"log"
"io/ioutil"
"net/http"
)

func queryExecute(data forceData, path string, method string) {

client := &http.Client{}
req, err := http.NewRequest(method, data.url + path, nil)
if err != nil {
log.Fatal(err)
}

q := req.URL.Query()

req.URL.RawQuery = q.Encode()

resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

fmt.Println(string(body))
}
32 changes: 32 additions & 0 deletions src/query/callWorker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"time"
)

type forceData struct {
worker int
wordList string
url string
}

func executeQueryFromFile(data forceData, currentPath chan string) {
for taskData := range currentPath{
queryExecute(data, taskData, "POST")
}
}

func mainRequest(data forceData) {
channel := make(chan string)
wordArray := GetFileContent("../wordList/rootList")

for i := 0 ;i < data.worker; i++ {
go executeQueryFromFile(data, channel)
}
for i := 0; i < len(wordArray); i++ {
channel <- wordArray[i]
}
time.Sleep(1 * time.Second)
close(channel)
}

35 changes: 35 additions & 0 deletions src/query/queryExecute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"log"
"io/ioutil"
"net/http"
)

func queryExecute(data forceData, path string, method string) {

client := &http.Client{}
req, err := http.NewRequest(method, data.url + path, nil)
if err != nil {
log.Fatal(err)
}

q := req.URL.Query()

req.URL.RawQuery = q.Encode()

resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

fmt.Println(string(body))
}
18 changes: 18 additions & 0 deletions src/utils/getFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"os"
"fmt"
"log"
"strings"
)

func GetFileContent(filePath string) []string{
body, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
dataTab := strings.Split(string(body), "\n")
fmt.Println(dataTab[0])
return dataTab
}
Loading