xxxxxxxxxx
import vibe.vibe;
import std.stdio;
import std.datetime;
import vibe.core.core;
//string test_url = "http://127.0.0.1:8081/hck";
string [] test_urls = ["http://127.0.0.1:8081/hck","http://127.0.0.1:8081/hck2","http://127.0.0.1:8081/hck3"];
struct MyUrl
{
string url;
string status;
}
MyUrl myUrl;
MyUrl [] myUrls;
shared static this()
{
foreach(url;test_urls)
{
myUrl.url = url;
myUrls ~= myUrl;
}
setTimer(2.seconds, toDelegate(&processing), true);
//setTimer(2.seconds, toDelegate(&getServiceStatus), true);
}
void main()
{
listenHTTP(":8080", &handleRequest);
runApplication();
}
void handleRequest(HTTPServerRequest req, HTTPServerResponse res)
{
}
void processing()
{
writeln("processing");
foreach(ref url; myUrls)
{
runWorkerTask(&getServiceStatus, url);
writefln("url: %s, status: %s", url.url, url.status);
}
}
void getServiceStatus(ref MyUrl url) {
requestHTTP(url.url,
(scope req) {
req.method = HTTPMethod.GET;
},
(scope res) {
Json serviseAnswer = parseJsonString(res.bodyReader.readAllUTF8()); // status and message
url.status = serviseAnswer["status"].toString();
}
);
}