Files

106 lines
3.5 KiB
C++
Raw Permalink Normal View History

2020-09-02 14:03:01 +02:00
/*
* This file is part of the Flowee project
2021-02-06 23:07:07 +01:00
* Copyright (C) 2019-2021 Tom Zander <tom@flowee.org>
2020-09-02 14:03:01 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QStandardPaths>
#include <FloweeServiceApplication.h>
#include <WorkerThreads.h>
#include <httpengine/server.h>
#include <Message.h>
#include "RestService.h"
#define PORT 3200
class Server : public HttpEngine::Server
{
public:
explicit Server(const std::function<void(HttpEngine::WebRequest*)> &handler)
: m_func(handler)
{
}
HttpEngine::WebRequest *createRequest(qintptr socketDescriptor) override {
return new RestServiceWebRequest(socketDescriptor, m_func);
}
void setProxy(RestService *proxy) { m_handler = proxy; }
private:
RestService *m_handler = nullptr;
std::function<void(HttpEngine::WebRequest*)> m_func;
};
Q_DECLARE_METATYPE(Message)
int main(int argc, char **argv)
{
FloweeServiceApplication app(argc, argv);
app.setOrganizationName("flowee");
app.setOrganizationDomain("flowee.org");
app.setApplicationName("rest-service");
QCommandLineParser parser;
parser.setApplicationDescription("REST service");
parser.addHelpOption();
QCommandLineOption conf(QStringList() << "conf", "config file", "FILENAME");
parser.addOption(conf);
app.addServerOptions(parser);
parser.process(app.arguments());
2026-05-22 00:12:45 +02:00
app.setup("restservice.log");
2020-09-02 14:03:01 +02:00
qRegisterMetaType<Message>();
RestService handler;
2020-11-16 23:09:17 +01:00
// become a server
2020-09-02 14:03:01 +02:00
Server server(std::bind(&RestService::onIncomingConnection, &handler, std::placeholders::_1));
server.setProxy(&handler);
2020-11-16 23:09:17 +01:00
int rc = app.bindTo(&server, PORT);
if (rc != 0)
return rc;
2020-11-16 22:45:46 +01:00
Q_ASSERT(server.isListening());
2020-09-02 14:03:01 +02:00
try {
auto ep = app.serverAddressFromArguments(1235);
if (!ep.hostname.empty())
handler.addHub(ep);
} catch (const std::exception &e) {
logFatal() << e;
return 1;
}
QString confFile;
if (parser.isSet(conf)) {
confFile = parser.value(conf);
} else {
2021-02-06 23:07:07 +01:00
// the next one expects [configs]/flowee/rest-service/rest-service.conf
2020-09-02 14:03:01 +02:00
confFile = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "rest-service.conf");
2021-02-06 23:07:07 +01:00
if (confFile.isEmpty()) // the next one expects [configs]/flowee/rest-service.conf
confFile = QStandardPaths::locate(QStandardPaths::ConfigLocation, "flowee/rest-service.conf");
2020-09-02 14:03:01 +02:00
if (confFile.isEmpty()) {
logCritical() << "No config file (rest-service.conf) found, assuming defaults and no indexer";
2021-02-26 15:32:31 +01:00
for (auto &dir : QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation)) {
2020-09-02 14:03:01 +02:00
logInfo() << " - not found in" << dir + '/';
}
}
}
handler.setConfigFile(confFile.toStdString());
QObject::connect (&app, SIGNAL(reparseConfig()), &handler, SLOT(onReparseConfig()));
return app.exec();
}