-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module sampple.com | ||
|
||
go 1.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type configuration struct { | ||
Query string | ||
Server_fqdn []string | ||
Request_Timeout_in_seconds int64 | ||
} | ||
|
||
type esQuery struct { | ||
queryUrl string | ||
responseFilename string | ||
requestTimeout int64 | ||
} | ||
|
||
func getEsQueryList(conf configuration) []esQuery { | ||
|
||
esRequests := make([]esQuery, len(conf.Server_fqdn)) | ||
|
||
currentTime := time.Now() | ||
for i := 0; i < len(conf.Server_fqdn); i++ { | ||
|
||
filename := strings.Split(conf.Server_fqdn[i], ".")[0] + "_" + currentTime.Format("20060102150405") + ".json" | ||
esRequests[i] = esQuery{ | ||
queryUrl: "https://" + conf.Server_fqdn[i] + "/" + conf.Query, | ||
requestTimeout: conf.Request_Timeout_in_seconds, | ||
responseFilename: filename, | ||
} | ||
} | ||
return esRequests | ||
} | ||
|
||
func getConfiguration() configuration { | ||
file, err_file_open := os.Open("es_query_conf.json") | ||
|
||
if err_file_open != nil { | ||
fmt.Println("error:", err_file_open) | ||
} | ||
defer file.Close() | ||
|
||
decoder := json.NewDecoder(file) | ||
conf := configuration{} | ||
|
||
err := decoder.Decode(&conf) | ||
if err != nil { | ||
fmt.Println("error:", err) | ||
} | ||
return conf | ||
} | ||
|
||
func getHttpResponse(esRequestInfo esQuery, respStatus chan<- string) { | ||
|
||
fmt.Println("HTTP GET:", esRequestInfo.queryUrl) | ||
resp, err := http.Get(esRequestInfo.queryUrl) | ||
if err != nil { | ||
//panic(err) | ||
respStatus <- "Error connecting to host" | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
// fmt.Println("Response status:", resp.Status) | ||
|
||
out, err := os.Create(esRequestInfo.responseFilename) | ||
if err != nil { | ||
// panic? | ||
fmt.Println("error while writing file: ", esRequestInfo.responseFilename) | ||
} | ||
defer out.Close() | ||
io.Copy(out, resp.Body) | ||
respStatus <- resp.Status | ||
} | ||
|
||
func getResponseData(esRequestInfo esQuery, respStatus <-chan string, completionStatus chan<- string) { | ||
select { | ||
case result := <-respStatus: | ||
fmt.Println(result) | ||
break | ||
case <-time.After(time.Duration(esRequestInfo.requestTimeout) * time.Second): | ||
fmt.Println("timedout: ", esRequestInfo) | ||
} | ||
completionStatus <- "completed" | ||
} | ||
|
||
func main() { | ||
conf := getConfiguration() | ||
esRequests := getEsQueryList(conf) | ||
|
||
responseStatusChannel := make(chan string, len(esRequests)) | ||
waitForCompletionChannel := make(chan string, len(esRequests)) | ||
|
||
for _, esRequest := range esRequests { | ||
go getHttpResponse(esRequest, responseStatusChannel) | ||
go getResponseData(esRequest, responseStatusChannel, waitForCompletionChannel) | ||
} | ||
|
||
for i := 0; i < len(esRequests); i++ { | ||
<-waitForCompletionChannel | ||
} | ||
fmt.Println("completed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"query": "values", | ||
"server_fqdn": [ | ||
"gobyexample.com", | ||
"gobyexample.com", | ||
"gobyexample.com" | ||
], | ||
"request_timeout_in_seconds": 60 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package main | ||
|
||
func Add(x, y int) int { | ||
return x + y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestAdder(t *testing.T) { | ||
actual := Add(3, 2) | ||
expected := 5 | ||
if actual != expected { | ||
t.Errorf("got %q and want %q", actual, expected) | ||
} | ||
actual = Add(3, 5) | ||
expected = 9 | ||
if actual != expected { | ||
t.Errorf("got %d and want %d", actual, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters