本文整理汇总了C++中HttpServer::startServer方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpServer::startServer方法的具体用法?C++ HttpServer::startServer怎么用?C++ HttpServer::startServer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpServer
的用法示例。
在下文中一共展示了HttpServer::startServer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: initTestCase
void MonkeyTest::initTestCase()
{
HttpServer* httpSvr = TestUtils::setUp(this);
QVERIFY(httpSvr != nullptr);
QVERIFY(httpSvr->initialize() == true);
auto action = httpSvr->createAction([](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
json["response"] = "C++ FTW";
});
action->registerRoute(HttpMethod::GET, "something");
httpSvr->startServer("127.0.0.1", 8080);
QTest::qWait(1000);
}
示例2: main
int main(int argc, char **argv)
{
int port = 0;
int nCPU = 0;
char* rootDir = 0;
int c = 0;
while ((c = getopt (argc, argv, "p:n:r:")) != -1) {
switch(c) {
case 'p':
port = atoi(optarg);
if(port < 1 || port > 65535) {
std::cout << "Wrong port: " << port << std::endl;
return 0;
}
break;
case 'n':
nCPU = atoi(optarg);
if(nCPU < 1) {
std::cout << "Wrong nCPU: " << nCPU << std::endl;
return 0;
}
break;
case 'r':
rootDir = optarg;
if(rootDir[strlen(rootDir) - 1] == '/') {
rootDir[strlen(rootDir) - 1] = '\0';
}
break;
}
}
HttpServer* httpServer = 0;
try {
HttpServer* httpServer = new HttpServer();
httpServer->startServer(port, nCPU, rootDir);
}
catch(std::exception& err) {
std::cout << err.what() << std::endl;
}
delete httpServer;
return 0;
}
示例3: main
int main(int argc, char **argv)
{
if(argc == 1)
{
// sample code to create http server
HttpServer httpServer;
int res = httpServer.startServer();
if (res == -1)
{
return res;
}
}else if(argc == 3){
// sample code to create http client
string ip = argv[1];
int port = stoi(string(argv[2]));
HttpClient httpClient(ip, port);
string response = httpClient.requestToHttpServer();
cout << "Received string "<< response << endl;
}
return 0;
}
示例4: main
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
HttpServer* svr = HttpServer::getInstance();
svr->initialize();
auto helloworld = svr->createAction([](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
json["response"] = "Hello World!";
});
helloworld->registerRoute(HttpMethod::GET, "/helloworld");
auto echobody = svr->createAction([](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
json["response"] = data.getRequest().getJson();
});
echobody->registerRoute(HttpMethod::POST, "/echobody");
svr->startServer();
return app.exec();
}
示例5: initTestCase
//.........这里部分代码省略.........
QVERIFY(httpSvr != nullptr);
QVERIFY(httpSvr->initialize() == true);
auto action = httpSvr->createAction("", [](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
json["response"] = "C++ FTW";
});
httpSvr->addProcessor<SampleProcessor>();
action = httpSvr->createAction("echo", [](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
auto& query = data.getRequest().getQuery();
json["response"] = "C++ FTW " + query.queryItemValue("id");
});
auto result = httpSvr->registerRoute("get", "", "/echo/:id");
QVERIFY(result == true);
result = httpSvr->registerRoute("get", "echo", "/echo/:id/data");
QVERIFY(result == true);
action = httpSvr->createAction("echobody", [](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
json["response"] = data.getRequest().getJson();
});
result = httpSvr->registerRoute("post", "echobody", "/echobody");
QVERIFY(result == true);
// Uses the action interface.
QVERIFY(httpSvr->addAction<SampleActionWithHttpMethods>().get() != nullptr);
result = httpSvr->registerRoute("get", "sampleWithHttpMethods", "/http");
QVERIFY(result == true);
result = httpSvr->registerRoute("post", "sampleWithHttpMethods", "/http");
QVERIFY(result == true);
result = httpSvr->registerRoute("put", "sampleWithHttpMethods", "/http");
QVERIFY(result == true);
result = httpSvr->registerRoute("delete", "sampleWithHttpMethods", "/http");
QVERIFY(result == true);
// Uses the action interface.
QString param = "param";
QVERIFY((httpSvr->addAction<ActionWithParameter, QString>(param)).get() != nullptr);
result = httpSvr->registerRoute("get", "sampleWithParameter", "/sampleWithParameter");
QVERIFY(result == true);
// Uses the action interface.
QVERIFY((httpSvr->addAction<SampleAction>()).get() != nullptr);
result = httpSvr->registerRoute("get", "sample", "/sample");
QVERIFY(result == true);
result = httpSvr->registerRoute("get", "sample", "/sample2");
QVERIFY(result == true);
// Uses a raw std::function based callback.
action = httpSvr->createAction("test", [](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
json["response"] = "Test C++ FTW";
// NOTE: This terminates early so we should not expect any post-processing.
data.getResponse().finish();
});
QVERIFY((action.get() != nullptr));
result = httpSvr->registerRoute("get", "test", "/test");
QVERIFY(result == true);
result = httpSvr->registerRoute("get", "test", "/test2");
QVERIFY(result == true);
action = httpSvr->createAction("terminates", [](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
json["response"] = "Test C++ FTW";
// NOTE: This terminates early so we should not expect any post-processing.
data.getResponse().terminate();
});
QVERIFY((action.get() != nullptr));
result = httpSvr->registerRoute("get", "terminates", "/terminates");
QVERIFY(result == true);
action = httpSvr->createAction("regex", [](HttpData& data) {
QJsonObject& json = data.getResponse().getJson();
QString name = data.getRequest().getJson()["name"].toString();
json["response"] = name;
});
result = httpSvr->registerRoute(qttp::GET, "regex", "/regex/:name([A-Za-z]+)");
QVERIFY(result == true);
httpSvr->startServer("127.0.0.1", 8080);
QTest::qWait(1000);
}