Skip to content

Instantly share code, notes, and snippets.

@bagobor
Created October 21, 2013 20:53
Show Gist options
  • Save bagobor/7090810 to your computer and use it in GitHub Desktop.
Save bagobor/7090810 to your computer and use it in GitHub Desktop.
embeded webserver test
#include "./mongoose/mongoose.h"
// url, query string
typedef std::function<void(const char*, const char*)> request_handler_t;
typedef std::map<std::string, request_handler_t> req_handlers_t;
req_handlers_t req_handlers;
static int begin_request_handler(struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn);
const char* uri = request_info->uri;
const char* query_string = request_info->query_string;
auto res = req_handlers.find(uri);
if (res != req_handlers.end()) {
res->second(uri, query_string);
return 1;
}
return 0;
//if (!request_info->query_string)
char content[100];
// Prepare the message we're going to send
int content_length = sprintf_s(content, sizeof(content),
"Hello from mongoose! Remote port: %d",
request_info->remote_port);
// Send HTTP reply to the client
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %d\r\n" // Always set Content-Length
"\r\n"
"%s",
content_length, content);
// Returning non-zero tells mongoose that our function has replied to
// the client, and mongoose should not send client any more data.
return 1;
};
int main( int argc, char** argv )
{
req_handlers["/some/test/url/"] = [=](const char* uri, const char* query_string) {
//do somthing here
};
struct mg_callbacks web_callbacks = {0};
web_callbacks.begin_request = &begin_request_handler;
const char *options[] = {
"document_root", "./www", // path for local files
"listening_ports", "8080",
NULL
};
mg_context * contex = mg_start(&web_callbacks, NULL, options);
{
// app logic here
}
mg_stop(contex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment