QGIS API Documentation  3.6.0-Noosa (5873452)
qgis_map_serv.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgs_map_serv.cpp
3  A server application supporting WMS / WFS / WCS
4  -------------------
5  begin : July 04, 2006
6  copyright : (C) 2006 by Marco Hugentobler & Ionut Iosifescu Enescu
7  email : marco dot hugentobler at karto dot baug dot ethz dot ch
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 //for CMAKE_INSTALL_PREFIX
20 #include "qgsconfig.h"
21 #include "qgsserver.h"
22 #include "qgsfcgiserverresponse.h"
23 #include "qgsfcgiserverrequest.h"
24 #include "qgsapplication.h"
25 
26 #include <fcgi_stdio.h>
27 #include <cstdlib>
28 
29 #include <QString>
30 
32 {
33 #ifdef Q_OS_WIN
34  if ( FCGX_IsCGI() )
35  return FCGI_Accept();
36  else
37  return FCGX_Accept( &FCGI_stdin->fcgx_stream, &FCGI_stdout->fcgx_stream, &FCGI_stderr->fcgx_stream, &environ );
38 #else
39  return FCGI_Accept();
40 #endif
41 }
42 
43 int main( int argc, char *argv[] )
44 {
45  // Test if the environ variable DISPLAY is defined
46  // if it's not, the server is running in offscreen mode
47  // Qt supports using various QPA (Qt Platform Abstraction) back ends
48  // for rendering. You can specify the back end to use with the environment
49  // variable QT_QPA_PLATFORM when invoking a Qt-based application.
50  // Available platform plugins are: directfbegl, directfb, eglfs, linuxfb,
51  // minimal, minimalegl, offscreen, wayland-egl, wayland, xcb.
52  // https://www.ics.com/blog/qt-tips-and-tricks-part-1
53  // http://doc.qt.io/qt-5/qpa.html
54  const char *display = getenv( "DISPLAY" );
55  bool withDisplay = true;
56  if ( !display )
57  {
58  withDisplay = false;
59  qputenv( "QT_QPA_PLATFORM", "offscreen" );
60  QgsMessageLog::logMessage( "DISPLAY not set, running in offscreen mode, all printing capabilities will not be available.", "Server", Qgis::Info );
61  }
62  // since version 3.0 QgsServer now needs a qApp so initialize QgsApplication
63  QgsApplication app( argc, argv, withDisplay, QString(), QStringLiteral( "server" ) );
64  QgsServer server;
65 #ifdef HAVE_SERVER_PYTHON_PLUGINS
66  server.initPython();
67 #endif
68  // Starts FCGI loop
69  while ( fcgi_accept() >= 0 )
70  {
71  QgsFcgiServerRequest request;
72  QgsFcgiServerResponse response( request.method() );
73  if ( ! request.hasError() )
74  {
75  server.handleRequest( request, response );
76  }
77  else
78  {
79  response.sendError( 400, "Bad request" );
80  }
81  }
82  app.exitQgis();
83  return 0;
84 }
85 
Extends QApplication to provide access to QGIS specific resources such as theme paths, database paths etc.
int fcgi_accept()
int main(int argc, char *argv[])
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Class defining fcgi response.
bool hasError() const
Returns true if an error occurred during initialization.
The QgsServer class provides OGC web services.
Definition: qgsserver.h:48
Class defining fcgi request.
static void exitQgis()
deletes provider registry and map layer registry
QgsServerRequest::Method method() const
void handleRequest(QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project=nullptr)
Handles the request.
Definition: qgsserver.cpp:266