Files
less_shitty_proxyjs/main.cpp

153 lines
5.4 KiB
C++
Raw Permalink Normal View History

2025-07-16 20:25:01 +02:00
#include <QCoreApplication>
#include <QCommandLineParser>
#include "webserver.h"
#ifdef FEATURE_REDIS
#include <hiredis/async.h>
#include "redisqtadapter.h"
#endif
2025-07-16 20:25:01 +02:00
#include <QTcpServer>
#include <QDebug>
int main(int argc, char *argv[])
{
qSetMessagePattern("%{time dd.MM.yyyy HH:mm:ss.zzz} "
"["
"%{if-debug}D%{endif}"
"%{if-info}I%{endif}"
"%{if-warning}W%{endif}"
"%{if-critical}C%{endif}"
"%{if-fatal}F%{endif}"
"] "
"%{function}(): "
"%{message}");
QCoreApplication app{argc, argv};
QCoreApplication::setApplicationName("less_shitty_proxyjs");
QCoreApplication::setApplicationVersion("1.0");
QHostAddress listenAddress = QHostAddress::Any;
2025-07-16 20:25:01 +02:00
uint16_t port = 8000;
QString identity = "test";
QString redisPassword;
2025-07-16 20:25:01 +02:00
{
QCommandLineParser parser;
parser.setApplicationDescription("Test helper");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption listenAddrOption{QStringList{ "l", "listenAddress" },
QCoreApplication::translate("main", "The address to listen on (Can be \"Any\" \"Localhost\" or any valid ip address)."),
QCoreApplication::translate("main", "listenAddress")};
parser.addOption(listenAddrOption);
QCommandLineOption portOption{QStringList{ "p", "port" },
QCoreApplication::translate("main", "The port to listen on (will only listen on localhost)."),
QCoreApplication::translate("main", "port")};
parser.addOption(portOption);
QCommandLineOption identityOption{QStringList{ "i", "identity" },
QCoreApplication::translate("main", "The name of this service instance"),
QCoreApplication::translate("main", "identity")};
parser.addOption(identityOption);
QCommandLineOption redisPasswordOption{QStringList{ "r", "redisPassword" },
QCoreApplication::translate("main", "The redis password"),
QCoreApplication::translate("main", "redisPassword")};
parser.addOption(redisPasswordOption);
2025-07-16 20:25:01 +02:00
parser.process(app);
if (parser.isSet(listenAddrOption))
{
if (const auto &str = parser.value(listenAddrOption); str == "Any")
listenAddress = QHostAddress::Any;
else if (str == "AnyIPv4")
listenAddress = QHostAddress::AnyIPv4;
else if (str == "AnyIPv6")
listenAddress = QHostAddress::AnyIPv6;
else if (str == "LocalHost")
listenAddress = QHostAddress::LocalHost;
else if (str == "LocalHostIPv6")
listenAddress = QHostAddress::LocalHostIPv6;
else
listenAddress = QHostAddress{str};
}
if (parser.isSet(portOption))
2025-07-16 20:25:01 +02:00
{
const auto &str = parser.value(portOption);
2025-07-16 20:25:01 +02:00
bool ok;
port = str.toInt(&ok);
2025-07-16 20:25:01 +02:00
if (!ok)
{
qCritical("could not parse port: %s", qPrintable(str));
2025-07-16 20:25:01 +02:00
return -1;
}
}
if (parser.isSet(identityOption))
identity = parser.value(identityOption);
if (parser.isSet(redisPasswordOption))
redisPassword = parser.value(redisPasswordOption);
2025-07-16 20:25:01 +02:00
}
qSetMessagePattern(QString{"%{time dd.MM.yyyy HH:mm:ss.zzz} %0 "
"["
"%{if-debug}D%{endif}"
"%{if-info}I%{endif}"
"%{if-warning}W%{endif}"
"%{if-critical}C%{endif}"
"%{if-fatal}F%{endif}"
"] "
"%{function}(): "
"%{message}"}.arg(identity));
#ifdef FEATURE_REDIS
redisAsyncContext * const redis = redisAsyncConnect("localhost", 6379);
if (redis->err)
qFatal("error with redis connection: %s", redis->errstr);
RedisQtAdapter redisAdapter;
redisAdapter.setContext(redis);
qDebug() << "redis AUTH" << redisPassword;
redisAsyncCommand(redis, NULL, NULL, "AUTH %s",
redisPassword.toUtf8().constData());
redisAsyncCommand(redis, NULL, NULL, "SET %s %s",
QString{"traefik/http/services/proxyjs_%0/loadbalancer/servers/0/url"}.arg(identity).toUtf8().constData(),
QString{"http://localhost:%0"}.arg(port).toUtf8().constData());
qDebug() << "redis connected";
#endif
WebServer server{identity
#ifdef FEATURE_REDIS
, redis
#endif
};
2025-07-16 20:25:01 +02:00
QTcpServer tcpServer;
if (!tcpServer.listen(listenAddress, port))
2025-07-16 20:25:01 +02:00
{
qCritical("failed to start listening on addr %s port %hu: %s", qPrintable(listenAddress.toString()), port, qPrintable(tcpServer.errorString()));
2025-07-16 20:25:01 +02:00
return -1;
}
if (!server.bind(&tcpServer))
{
qCritical("failed to webserver on socket!");
return -2;
}
qDebug() << "server started on" << listenAddress << "port" << port;
2025-07-16 20:25:01 +02:00
auto result = app.exec();
qDebug() << "bey bey!";
return result;
}