QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 <QFontDatabase>
30 #include <QString>
31 
33 {
34 #ifdef Q_OS_WIN
35  if ( FCGX_IsCGI() )
36  return FCGI_Accept();
37  else
38  return FCGX_Accept( &FCGI_stdin->fcgx_stream, &FCGI_stdout->fcgx_stream, &FCGI_stderr->fcgx_stream, &environ );
39 #else
40  return FCGI_Accept();
41 #endif
42 }
43 
44 int main( int argc, char *argv[] )
45 {
46  // Test if the environ variable DISPLAY is defined
47  // if it's not, the server is running in offscreen mode
48  // Qt supports using various QPA (Qt Platform Abstraction) back ends
49  // for rendering. You can specify the back end to use with the environment
50  // variable QT_QPA_PLATFORM when invoking a Qt-based application.
51  // Available platform plugins are: directfbegl, directfb, eglfs, linuxfb,
52  // minimal, minimalegl, offscreen, wayland-egl, wayland, xcb.
53  // https://www.ics.com/blog/qt-tips-and-tricks-part-1
54  // http://doc.qt.io/qt-5/qpa.html
55  const char *display = getenv( "DISPLAY" );
56  bool withDisplay = true;
57  if ( !display )
58  {
59  withDisplay = false;
60  qputenv( "QT_QPA_PLATFORM", "offscreen" );
61  QgsMessageLog::logMessage( "DISPLAY not set, running in offscreen mode, all printing capabilities will not be available.", "Server", Qgis::Info );
62  }
63  // since version 3.0 QgsServer now needs a qApp so initialize QgsApplication
64  QgsApplication app( argc, argv, withDisplay, QString(), QStringLiteral( "server" ) );
65  QgsServer server;
66 #ifdef HAVE_SERVER_PYTHON_PLUGINS
67  server.initPython();
68 #endif
69 
70 #ifdef Q_OS_WIN
71  // Initialize font database before fcgi_accept.
72  // When using FCGI with IIS, environment variables (QT_QPA_FONTDIR in this case) are lost after fcgi_accept().
73  QFontDatabase fontDB;
74 #endif
75 
76  // Starts FCGI loop
77  while ( fcgi_accept() >= 0 )
78  {
79  QgsFcgiServerRequest request;
80  QgsFcgiServerResponse response( request.method() );
81  if ( ! request.hasError() )
82  {
83  server.handleRequest( request, response );
84  }
85  else
86  {
87  response.sendError( 400, "Bad request" );
88  }
89  }
90  app.exitQgis();
91  return 0;
92 }
93 
main
int main(int argc, char *argv[])
Definition: qgis_map_serv.cpp:44
QgsServer::handleRequest
void handleRequest(QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project=nullptr)
Handles the request.
Definition: qgsserver.cpp:297
QgsFcgiServerResponse::sendError
void sendError(int code, const QString &message) override
Send error This method delegates error handling at the server level.
Definition: qgsfcgiserverresponse.cpp:65
QgsFcgiServerRequest
Class defining fcgi request.
Definition: qgsfcgiserverrequest.h:32
QgsFcgiServerResponse
Class defining fcgi response.
Definition: qgsfcgiserverresponse.h:36
qgsapplication.h
Qgis::Info
@ Info
Definition: qgis.h:103
QgsServerRequest::method
QgsServerRequest::Method method() const
Definition: qgsserverrequest.cpp:80
QgsApplication::exitQgis
static void exitQgis()
deletes provider registry and map layer registry
Definition: qgsapplication.cpp:1283
qgsserver.h
QgsFcgiServerRequest::hasError
bool hasError() const
Returns true if an error occurred during initialization.
Definition: qgsfcgiserverrequest.h:42
fcgi_accept
int fcgi_accept()
Definition: qgis_map_serv.cpp:32
QgsApplication
Definition: qgsapplication.h:78
QgsServer
Definition: qgsserver.h:48
qgsfcgiserverresponse.h
QgsMessageLog::logMessage
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).
Definition: qgsmessagelog.cpp:27
qgsfcgiserverrequest.h