Files
thehub/rest-service/main.cpp
T
2020-09-20 21:12:51 +02:00

119 lines
3.8 KiB
C++

/*
* This file is part of the Flowee project
* Copyright (C) 2019-2020 Tom Zander <tomz@freedommail.ch>
*
* 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());
app.setup("restservice.log", parser.value(conf));
qRegisterMetaType<Message>();
RestService handler;
Server server(std::bind(&RestService::onIncomingConnection, &handler, std::placeholders::_1));
server.setProxy(&handler);
// become a server
bool success = false;
for (auto ep : app.bindingEndPoints(parser, 1234, FloweeServiceApplication::LocalhostAsDefault)) {
logCritical().nospace() << "Trying to bind to " << ep.address().to_string().c_str() << ":" << ep.port();
try {
if (!server.listen(QHostAddress(QString::fromStdString(ep.address().to_string())), ep.port())) {
logCritical() << " Failed to listen on interface";
} else {
success = true;
}
} catch (std::exception &e) {
logCritical() << " nope, not binding there due to:" << e;
}
}
if (!success) {
logFatal() << "Please pass --bind to tell me which network to listen to";
return 1;
}
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 {
confFile = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "rest-service.conf");
if (confFile.isEmpty()) {
logCritical() << "No config file (rest-service.conf) found, assuming defaults and no indexer";
for (auto dir : QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation)) {
logInfo() << " - not found in" << dir + '/';
}
}
}
handler.setConfigFile(confFile.toStdString());
QObject::connect (&app, SIGNAL(reparseConfig()), &handler, SLOT(onReparseConfig()));
return app.exec();
}