2013-06-21 17:21:11 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, The pgAdmin Development Team
|
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
// Server.cpp - Thread in which the web server will run.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "pgAdmin4.h"
|
|
|
|
|
|
|
|
// Must be before QT
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
// QT headers
|
2013-06-21 17:32:32 -05:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
2013-06-21 17:21:11 -05:00
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
// App headers
|
|
|
|
#include "Server.h"
|
|
|
|
|
2013-10-04 11:12:10 -05:00
|
|
|
Server::Server(quint16 port)
|
2014-12-16 06:53:09 -06:00
|
|
|
{
|
2013-10-04 11:12:10 -05:00
|
|
|
// Appserver port
|
|
|
|
m_port = port;
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
// Initialise Python
|
2013-06-21 18:49:40 -05:00
|
|
|
Py_SetProgramName(PGA_APP_NAME.toUtf8().data());
|
2013-06-21 17:21:11 -05:00
|
|
|
Py_Initialize();
|
2014-12-16 06:53:09 -06:00
|
|
|
|
|
|
|
// Setup the search path
|
|
|
|
QSettings settings;
|
|
|
|
QString python_path = settings.value("PythonPath").toString();
|
|
|
|
|
|
|
|
if (python_path.length() > 0)
|
|
|
|
{
|
2016-01-12 07:50:49 -06:00
|
|
|
// Split the path setting into individual entries
|
|
|
|
QStringList path_list = python_path.split(";", QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
// Get the current path
|
2014-12-16 06:53:09 -06:00
|
|
|
PyObject* sysPath = PySys_GetObject((char*)"path");
|
2016-01-12 07:50:49 -06:00
|
|
|
|
|
|
|
// Add new additional path elements
|
|
|
|
for (int i = 0; i < path_list.size(); ++i)
|
|
|
|
PyList_Append(sysPath, PyString_FromString(path_list.at(i).toUtf8().data()));
|
2014-12-16 06:53:09 -06:00
|
|
|
}
|
2013-06-21 17:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
|
|
|
// Shutdown Python
|
|
|
|
Py_Finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Server::Init()
|
|
|
|
{
|
|
|
|
// Find the webapp
|
|
|
|
QStringList paths;
|
|
|
|
paths.append("../web/"); // Windows/Linux source tree
|
|
|
|
paths.append("../../../../web/"); // Mac source tree (in the app bundle)
|
|
|
|
paths.append(""); // Should be last!
|
|
|
|
|
|
|
|
for (int i = 0; i < paths.size(); ++i)
|
|
|
|
{
|
|
|
|
QDir dir(QCoreApplication::applicationDirPath() + "/" + paths[i]);
|
|
|
|
m_appfile = dir.canonicalPath() + "/pgAdmin4.py";
|
|
|
|
|
|
|
|
if (QFile::exists(m_appfile))
|
|
|
|
{
|
|
|
|
qDebug() << "Webapp path: " << m_appfile;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!QFile::exists(m_appfile))
|
|
|
|
{
|
2013-10-04 12:16:31 -05:00
|
|
|
setError(tr("Failed to locate pgAdmin4.py, terminating server thread."));
|
2013-06-21 17:21:11 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Server::run()
|
|
|
|
{
|
|
|
|
// Open the application code and run it.
|
|
|
|
FILE *cp = fopen(m_appfile.toUtf8().data(), "r");
|
|
|
|
if (!cp)
|
|
|
|
{
|
2013-10-04 12:16:31 -05:00
|
|
|
setError(QString(tr("Failed to open the application file: %1, server thread exiting.")).arg(m_appfile));
|
2013-06-21 17:21:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-04 11:12:10 -05:00
|
|
|
// Set the port number
|
|
|
|
PyRun_SimpleString(QString("PGADMIN_PORT = %1").arg(m_port).toLatin1());
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
if (PyRun_SimpleFile(cp, m_appfile.toUtf8().data()) != 0)
|
2013-10-04 12:16:31 -05:00
|
|
|
setError(tr("Failed to launch the application server, server thread exiting."));
|
2013-06-21 17:21:11 -05:00
|
|
|
|
|
|
|
fclose(cp);
|
|
|
|
}
|