示例#1
0
int HttpServer::start()
{
  HttpServer* svr = HttpServer::getInstance();

  QString ip = svr->m_GlobalConfig["bindIp"].toString("0.0.0.0").trimmed();
  auto port = svr->m_GlobalConfig["bindPort"].toInt(8080);

  auto callback = [svr](QttpRequest& req, QttpResponse& resp) {
                    HttpEvent* event = new HttpEvent(&req, &resp);
                    QCoreApplication::postEvent(svr, event);
                  };

  native::http::Qttp server;
  auto result = server.listen(ip.toStdString(), port, callback);

  if(!result)
  {
    LOG_ERROR("Unable to bind to" << ip << ":" << port);

    if(svr->m_ServerErrorCallback)
    {
      svr->m_ServerErrorCallback();
    }

    LOG_FATAL(ip << ":" << port << " " << SERVER_ERROR_MSG);
    return 1;
  }

  LOG_INFO("Server pid" << QCoreApplication::applicationPid() <<
           "running at" << ip << port);

  return native::run();
}