Commit 5bcd8e30 authored by Pavel Vainerman's avatar Pavel Vainerman

(LogDB): реализовал вывод html-странички со списком

log-серверов доступных для подключения через websocket
parent dbfe112f
......@@ -641,6 +641,7 @@ void LogDB::handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPSer
using Poco::Net::HTTPResponse;
std::ostream& out = resp.send();
resp.setContentType("text/json");
try
......@@ -661,6 +662,15 @@ void LogDB::handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPSer
std::vector<std::string> seg;
uri.getPathSegments(seg);
// проверка подключения к страничке со списком websocket-ов
if( seg.size() >= 1 && seg[0] == "ws" )
{
httpWebSocketPage(out, req, resp);
out.flush();
return;
}
// example: http://host:port/api/version/logdb/..
if( seg.size() < 4
|| seg[0] != "api"
......@@ -1258,5 +1268,60 @@ void LogDB::LogWebSocket::waitCompletion()
finish.wait(lk);
}
// -----------------------------------------------------------------------------
void LogDB::httpWebSocketPage( std::ostream& ostr, Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
{
using Poco::Net::HTTPResponse;
resp.setChunkedTransferEncoding(true);
resp.setContentType("text/html");
// code base on example from
// https://github.com/pocoproject/poco/blob/developNet/samples/WebSocketServer/src/WebSocketServer.cpp
ostr << "<html>" << endl;
ostr << "<head>" << endl;
ostr << "<title>" << myname << " log servers list</title>" << endl;
ostr << "<script type=\"text/javascript\">" << endl;
ostr << "function WebSocketCreate(logname)" << endl;
ostr << "{" << endl;
ostr << " if (\"WebSocket\" in window)" << endl;
ostr << " {" << endl;
ostr << " var ws = new WebSocket(\"ws://" << req.serverAddress().toString() << "/logdb/ws/\" + logname);" << endl;
ostr << " var l = document.getElementById('logname');" << endl;
ostr << " l.innerHTML = logname" << endl;
ostr << " ws.onmessage = function(evt)" << endl;
ostr << " {" << endl;
ostr << " var p = document.getElementById('logs');" << endl;
ostr << " if( evt.data != '.' ) {" << endl;
ostr << " p.innerHTML = p.innerHTML + \"</br>\"+evt.data" << endl;
ostr << " }" << endl;
ostr << " };" << endl;
ostr << " ws.onclose = function()" << endl;
ostr << " { " << endl;
ostr << " alert(\"WebSocket closed.\");" << endl;
ostr << " };" << endl;
ostr << " }" << endl;
ostr << " else" << endl;
ostr << " {" << endl;
ostr << " alert(\"This browser does not support WebSockets.\");" << endl;
ostr << " }" << endl;
ostr << "}" << endl;
ostr << "</script>" << endl;
ostr << "</head>" << endl;
ostr << "<body>" << endl;
ostr << " <h1>" << myname << " WebSocket Server:</h1>" << endl;
ostr << "<ul>" << endl;
for( const auto& l : logservers )
ostr << " <li><a href=\"javascript:WebSocketCreate('" << l->name << "')\">" << l->name << "</a></li>" << endl;
ostr << "</ul>" << endl;
ostr << "<h4><div id='logname'></div></h4>" << endl;
ostr << "<div id='logs'></div>" << endl;
ostr << "</body>" << endl;
}
// -----------------------------------------------------------------------------
#endif
// -----------------------------------------------------------------------------
......@@ -130,6 +130,7 @@ namespace uniset
\todo WebSocket: сделать ограничение на максимальное количество соединений
\todo utils: написать конвертор обычных uniset-логов в БД.
\todo db: возможно всё-таки стоит парсить логи на предмет loglevel, и тогда уж и дату с временем вынимать
\todo web: генерировать html-страничку со списком подключения к логам с использованием готового шаблона
*/
class LogDB:
public EventLoopServer
......@@ -180,6 +181,7 @@ namespace uniset
Poco::JSON::Object::Ptr httpGetList( const Poco::URI::QueryParameters& p );
Poco::JSON::Object::Ptr httpGetLogs( const Poco::URI::QueryParameters& p );
Poco::JSON::Object::Ptr httpGetCount( const Poco::URI::QueryParameters& p );
void httpWebSocketPage( std::ostream& out, Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
// формирование условия where для строки XX[m|h|d|M]
// XX m - минут, h-часов, d-дней, M - месяцев
......
......@@ -105,12 +105,12 @@ LogInfo makeLogInfo( const std::string& name )
void parseLine( SQLiteInterface* db, const LogInfo& loginfo, const std::string& line )
{
// 28/07/2017 10:55:19( info): QG3(AutomatTransientMode): ждем отключения автомата
// 28/07/2017 10:55:19( info): text...text...more text
static const std::regex re("[::space::]{0,}"
"(\\d{2})[/-]{1}(\\d{2})[/-]{1}(\\d{4})"
"(\\d{2})[/-]{1}(\\d{2})[/-]{1}(\\d{4})" //date
" "
"(\\d{2}:\\d{2}:\\d{2})"
"(\\d{2}:\\d{2}:\\d{2})" // time
// "(.*)$"
);
......
......@@ -16,7 +16,7 @@
// ws.send("Hello, world!");
// };
ws.onmessage = ws.onmessage = function(evt)
ws.onmessage = function(evt)
{
var p = document.getElementById('logs');
if( evt.data != '.' ) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment