peerplays_migrated/programs/full_web_node/main.cpp

65 lines
2.1 KiB
C++
Raw Normal View History

2015-08-12 18:56:33 +00:00
#include "BlockChain.hpp"
#include <fc/thread/thread.hpp>
2015-08-12 18:56:33 +00:00
#include <fc/thread/future.hpp>
#include <fc/network/http/server.hpp>
#include <fc/network/ip.hpp>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
2015-08-12 18:56:33 +00:00
#include <QQmlContext>
#include <QtWebEngine>
#include <QtQml>
int main(int argc, char *argv[])
{
fc::thread::current().set_name("main");
QGuiApplication app(argc, argv);
app.setApplicationName("Graphene Client");
app.setApplicationDisplayName(app.applicationName());
app.setOrganizationDomain("cryptonomex.org");
app.setOrganizationName("Cryptonomex, Inc.");
2015-08-12 18:56:33 +00:00
QThread chainThread;
fc::promise<BlockChain*>::ptr chain = new fc::promise<BlockChain*>;
QObject::connect(&chainThread, &QThread::started, [&app, &chain] {
chain->set_value(new BlockChain);
});
chainThread.start();
QtWebEngine::initialize();
2015-08-12 18:56:33 +00:00
fc::http::server webGuiServer;
fc::thread serverThread("HTTP server thread");
serverThread.async([&webGuiServer] {
webGuiServer.listen(fc::ip::endpoint::from_string("127.0.0.1:8080"));
webGuiServer.on_request([](const fc::http::request& request, const fc::http::server::response& response) {
QString path = QStringLiteral("/Users/nathan/graphene-ui/web/bundle") + QString::fromStdString(request.path);
if (path.endsWith('/'))
path.append(QStringLiteral("index.html"));
QFile file(path);
if (file.exists()) {
file.open(QIODevice::ReadOnly);
auto buffer = file.readAll();
response.set_status(fc::http::reply::OK);
response.set_length(buffer.size());
response.write(buffer.data(), buffer.size());
} else {
response.set_status(fc::http::reply::NotFound);
response.set_length(18);
response.write("I can't find that.", 18);
}
});
});
QQmlApplicationEngine engine;
2015-08-12 18:56:33 +00:00
auto chainPtr = chain->wait();
engine.rootContext()->setContextProperty(QStringLiteral("blockChain"), chainPtr);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
2015-08-12 18:56:33 +00:00
auto ret = app.exec();
chainPtr->deleteLater();
chainThread.exit(ret);
return ret;
}