code to embed webUI server in the main executable (initial version, not yet working)

This commit is contained in:
csoler 2019-11-10 17:38:16 +01:00
parent aabba04be9
commit 6603dbd913
No known key found for this signature in database
GPG key ID: 7BCA522266C0804C
17 changed files with 355 additions and 111 deletions

View file

@ -159,6 +159,12 @@ PUBLIC_HEADERS = retroshare/rsdisc.h \
retroshare/rsservicecontrol.h \
retroshare/rsgxsdistsync.h
rs_webui {
PUBLIC_HEADERS += retroshare/rswebui.h
SOURCES += rsserver/p3webui.cc
HEADERS += rsserver/p3webui.h
}
HEADERS += plugins/pluginmanager.h \
plugins/dlfcn_win32.h \
rsitems/rspluginitems.h \

View file

@ -0,0 +1,202 @@
/*******************************************************************************
* libretroshare/src/rsserver: p3webui.cc *
* *
* libretroshare: retroshare core library *
* *
* Copyright 2019-2019 Cyril Soler *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "p3webui.h"
#include <thread>
#include <iostream>
#include <fstream>
#include <memory>
#include <chrono>
#include <cstdlib>
#include <restbed>
#include "util/rsthreads.h"
#include "util/rsdebug.h"
#include "retroshare/rswebui.h"
#define TEXT_HTML 0
#define TEXT_CSS 1
#define TEXT_SVG 2
#define DEBUG_RS_WEBUI 1
RsWebUI *rsWebUI = new p3WebUI;
static constexpr char *mime_types[3] = {
"text/html",
"text/css",
"image/svg+xml"
};
template<int MIME_TYPE_INDEX> class handler
{
public:
static void get_handler( const std::shared_ptr< restbed::Session > session )
{
const auto request = session->get_request( );
const std::string filename = request->get_path_parameter( "filename" );
const std::string directory = request->get_path_parameter( "dir" );
std::string resource_filename = _base_directory + directory + "/" + filename;
std::cerr << "Reading file: \"" << resource_filename << "\"" << std::endl;
std::ifstream stream( resource_filename, std::ifstream::in );
if ( stream.is_open( ) )
{
const std::string body = std::string( std::istreambuf_iterator< char >( stream ), std::istreambuf_iterator< char >( ) );
std::cerr << " body length=" << body.length() << std::endl;
const std::multimap< std::string, std::string > headers
{
{ "Content-Type", mime_types[MIME_TYPE_INDEX] },
{ "Content-Length", std::to_string( body.length( ) ) }
};
session->close( restbed::OK, body, headers );
}
else
{
std::cerr << "Could not open file " << resource_filename << std::endl;
session->close( restbed::NOT_FOUND );
}
}
static std::string _base_directory ;
};
template<int MIME_TYPE_INDEX> std::string handler<MIME_TYPE_INDEX>::_base_directory = "/home/csoler/Desktop/Code/retroshare/RSNewWebUI/webui/";
static void service_ready_handler( restbed::Service& )
{
fprintf( stderr, "Hey! The service is up and running." );
}
class WebUIThread: public RsThread
{
public:
WebUIThread()
{
_service = std::make_shared<restbed::Service>();
}
void runloop() override
{
auto resource1 = std::make_shared< restbed::Resource >( );
resource1->set_paths( {
"/{filename: index.html}",
"/{filename: app.js}",
}
);
resource1->set_method_handler( "GET", handler<TEXT_HTML>::get_handler );
auto resource2 = std::make_shared< restbed::Resource >();
resource2->set_paths( {
"/{dir: css]/{filename: fontawesome.css}",
"/{dir: css}/{filename: solid.css}",
"/{filename: app.css}",
} );
resource2->set_method_handler( "GET", handler<TEXT_CSS>::get_handler );
auto resource3 = std::make_shared< restbed::Resource >();
resource3->set_paths( {
"/{filename: retroshare.svg}",
} );
resource3->set_method_handler( "GET", handler<TEXT_SVG>::get_handler );
auto settings = std::make_shared< restbed::Settings >( );
settings->set_port( 1984 );
settings->set_default_header( "Connection", "close" );
_service->publish( resource1 );
_service->publish( resource2 );
_service->publish( resource3 );
_service->set_ready_handler( service_ready_handler );
_service->start( settings );
}
void stop()
{
_service->stop();
RsThread::ask_for_stop();
while(isRunning())
sleep(1);
}
private:
std::shared_ptr<restbed::Service> _service;
};
p3WebUI::p3WebUI()
{
_webui_thread = new WebUIThread;
}
p3WebUI::~p3WebUI()
{
while(_webui_thread->isRunning())
{
stop();
std::cerr << "Deleting webUI object while webUI thread is still running. Trying shutdown...." << std::endl;
rstime::rs_usleep(1000*1000);
}
delete _webui_thread;
}
bool p3WebUI::restart()
{
RsDbg() << "Restarting web interface listening on port " << _listening_port << std::endl;
if(_webui_thread->isRunning())
_webui_thread->stop();
_webui_thread->start();
return true;
}
bool p3WebUI::stop()
{
_webui_thread->stop();
return true;
}
void p3WebUI::setHtmlFilesDirectory(const std::string& html_dir)
{
_html_files_directory = html_dir;
}
void p3WebUI::setListeningPort(uint16_t port)
{
_listening_port = port;
if(_webui_thread->isRunning())
restart();
}
int p3WebUI::status() const
{
if(_webui_thread->isRunning())
return WEBUI_STATUS_RUNNING;
else
return WEBUI_STATUS_NOT_RUNNING;
}

View file

@ -0,0 +1,49 @@
/*******************************************************************************
* libretroshare/src/rsserver: p3webui.h *
* *
* libretroshare: retroshare core library *
* *
* Copyright 2019-2019 Cyril Soler *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include <string>
#include "retroshare/rswebui.h"
class WebUIThread;
class p3WebUI: public RsWebUI
{
public:
p3WebUI();
virtual ~p3WebUI();
virtual bool restart() override;
virtual bool stop() override;
virtual void setHtmlFilesDirectory(const std::string& html_dir) override;
virtual void setListeningPort(uint16_t port) override;
virtual int status() const override;
private:
WebUIThread *_webui_thread;
std::string _html_files_directory;
uint16_t _listening_port;
};