Files

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

2019-09-23 11:07:45 +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>
2019-09-23 11:07:45 +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 "BitcoreProxy.h"
#define PORT 3000
class Server : public HttpEngine::Server
{
public:
2020-08-26 13:05:55 +02:00
explicit Server(const std::function<void(HttpEngine::WebRequest*)> &handler)
2019-09-23 11:07:45 +02:00
: m_func(handler)
{
}
HttpEngine::WebRequest *createRequest(qintptr socketDescriptor) override {
return new BitcoreWebRequest(socketDescriptor, m_func);
}
void setProxy(BitcoreProxy *proxy) { m_handler = proxy; }
private:
2020-08-26 13:05:55 +02:00
BitcoreProxy *m_handler = nullptr;
2019-09-23 11:07:45 +02:00
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("bitcore-proxy");
QCommandLineParser parser;
parser.setApplicationDescription("BitCore proxy server");
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("bitcore-proxy.log");
2019-09-23 11:07:45 +02:00
qRegisterMetaType<Message>();
BitcoreProxy handler;
2020-11-16 23:09:17 +01:00
// become a server
2019-09-23 11:07:45 +02:00
Server server(std::bind(&BitcoreProxy::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;
Q_ASSERT(server.isListening());
2019-09-23 11:07:45 +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/bitcore-proxy/bitcore-proxy.conf
2019-09-23 11:07:45 +02:00
confFile = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "bitcore-proxy.conf");
2021-02-06 23:07:07 +01:00
if (confFile.isEmpty()) // the next one expects [configs]/flowee/bitcore-proxy.conf
confFile = QStandardPaths::locate(QStandardPaths::ConfigLocation, "flowee/bitcore-proxy.conf");
2019-09-23 11:07:45 +02:00
if (confFile.isEmpty()) {
logCritical() << "No config file (bitcore-proxy.conf) found, assuming defaults and no indexer";
2020-12-25 23:51:06 +01:00
for (auto &dir : QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation)) {
2019-09-23 11:07:45 +02:00
logInfo() << " - not found in" << dir + '/';
}
}
}
handler.setConfigFile(confFile.toStdString());
2019-09-23 11:07:45 +02:00
QObject::connect (&app, SIGNAL(reparseConfig()), &handler, SLOT(onReparseConfig()));
2019-09-23 11:07:45 +02:00
return app.exec();
}