当前位置: 首页>>代码示例>>C++>>正文


C++ HttpServer::initialize方法代码示例

本文整理汇总了C++中HttpServer::initialize方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpServer::initialize方法的具体用法?C++ HttpServer::initialize怎么用?C++ HttpServer::initialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpServer的用法示例。


在下文中一共展示了HttpServer::initialize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
开发者ID:supamii,项目名称:QttpServer,代码行数:16,代码来源:monkeytest.cpp

示例2: 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();
}
开发者ID:supamii,项目名称:QttpServer,代码行数:24,代码来源:main.cpp

示例3: initTestCase

void QttpTest::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";
  });

  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);
//.........这里部分代码省略.........
开发者ID:supamii,项目名称:QttpServer,代码行数:101,代码来源:qttptest.cpp


注:本文中的HttpServer::initialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。