7 |
#include "Logger.h" |
#include "Logger.h" |
8 |
#include "version.h" |
#include "version.h" |
9 |
#include "Common.h" |
#include "Common.h" |
10 |
|
#include "ConfigFile.h" |
11 |
#include <sstream> |
#include <sstream> |
12 |
|
#include <stdlib.h> |
13 |
|
|
14 |
void show_secret(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) { |
void show_secret(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) { |
15 |
Common* cmn = Common::instance(); |
Common* cmn = Common::instance(); |
38 |
|
|
39 |
void show_index(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) { |
void show_index(struct mg_connection *conn, const struct mg_request_info *request_info, void *user_data) { |
40 |
mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"); |
mg_printf(conn, "%s", "HTTP/1.1 200 OK\r\n"); |
41 |
mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n"); |
mg_printf(conn, "%s", "Content-Type: text/html\r\n\r\n"); |
42 |
|
|
43 |
mg_printf(conn, "%s", "<html><body><h2>smsdaemon</h2>\n"); |
mg_printf(conn, "%s", "<html><body><h2>smsdaemon</h2>\n"); |
44 |
mg_printf(conn, "%s", "<a href='/status'>smsdaemon status</a>\n"); |
mg_printf(conn, "%s", "<a href='/status'>smsdaemon status</a>\n"); |
46 |
} |
} |
47 |
|
|
48 |
void EmbeddedHttp::startServer() { |
void EmbeddedHttp::startServer() { |
49 |
Logger::logMessage("Starting mongoose embedded http engine"); |
std::string portStr = Common::instance()->GetConfigfile()->GetValue("smsdaemon", "httpport"); |
50 |
|
int port = atoi( portStr.c_str() ); |
|
m_context = mg_start(); |
|
|
mg_set_option(m_context, "ports", "8080"); |
|
|
mg_set_uri_callback(m_context, "/", &show_index, 0); |
|
|
mg_set_uri_callback(m_context, "/status", &show_secret, 0); |
|
51 |
|
|
52 |
|
if (port > 0) { |
53 |
|
std::ostringstream oss; |
54 |
|
oss << "Starting mongoose embedded http engine (listenin on port " << port << ")"; |
55 |
|
Logger::logMessage( oss.str() ); |
56 |
|
|
57 |
|
oss.str(""); //empty string stream |
58 |
|
oss << port; |
59 |
|
|
60 |
|
m_context = mg_start(); |
61 |
|
mg_set_option(m_context, "ports", oss.str().c_str() ); |
62 |
|
mg_set_uri_callback(m_context, "/", &show_index, 0); |
63 |
|
mg_set_uri_callback(m_context, "/status", &show_secret, 0); |
64 |
|
} else { |
65 |
|
Logger::logMessage( "Mongoose http disabled" ); |
66 |
|
m_context = 0; |
67 |
|
} |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
void EmbeddedHttp::stopServer() { |
void EmbeddedHttp::stopServer() { |
72 |
mg_stop(m_context); |
if (m_context != 0) { |
73 |
|
mg_stop(m_context); |
74 |
|
} |
75 |
} |
} |