diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2013-04-18 17:34:27 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2013-04-18 17:34:27 +0200 |
commit | 87f4134201f0a0840415aab531e32ca714bc7a36 (patch) | |
tree | cf148ecc810f92ca2e4c177498a56130806ed4e4 /datastore-leveldb/src/server.cpp | |
parent | 7c8e064216cc672557cbf3892ff090490505e408 (diff) | |
download | ebus-alt-87f4134201f0a0840415aab531e32ca714bc7a36.tar.gz ebus-alt-87f4134201f0a0840415aab531e32ca714bc7a36.zip |
leveldb geht so
Diffstat (limited to 'datastore-leveldb/src/server.cpp')
-rw-r--r-- | datastore-leveldb/src/server.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/datastore-leveldb/src/server.cpp b/datastore-leveldb/src/server.cpp new file mode 100644 index 0000000..3000a25 --- /dev/null +++ b/datastore-leveldb/src/server.cpp @@ -0,0 +1,78 @@ +#include <csignal> +#include <forward_list> +#include <functional> +#include <chrono> +#include <thread> + +#include <boost/regex.hpp> +#include "mongoose.h" + +#include "db.h" +#include "web.h" + +typedef void(web_handler_t)(const boost::cmatch&, struct mg_connection *conn); + +std::forward_list<std::pair<boost::regex,std::function<web_handler_t>>> web_handler; + +struct mg_context *ctx; + +int begin_request_handler(struct mg_connection *conn) { + boost::cmatch match; + + const struct mg_request_info *request_info = mg_get_request_info(conn); + + for (auto item = web_handler.begin(); item != web_handler.end(); ++item) { + if (boost::regex_match(request_info->uri, match, (*item).first)) { + std::cout << request_info->request_method << " " + << request_info->uri << std::endl; + (*item).second(match, conn); + return 1; + } + } + + return 0; +} + +struct sigaction old_action; + +// Stop the server. +void sigint_handler(int s) { + mg_stop(ctx); + closeDB(); + exit(0); +} + +int main(int argc, char **argv) { + struct mg_callbacks callbacks; + const char *options[] = { + "listening_ports", "8080", + "document_root", "wwwroot", + NULL}; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.begin_request = begin_request_handler; + + // Routing + web_handler.push_front(std::make_pair( + web_handle_api_value_R, + web_handle_api_value)); + web_handler.push_front(std::make_pair( + web_handle_api_range_R, + web_handle_api_range)); + web_handler.push_front(std::make_pair( + web_handle_api_range_size_R, + web_handle_api_range_size)); + + + // Signals: handle C-c + struct sigaction action; + memset(&action, 0, sizeof(action)); + action.sa_handler = &sigint_handler; + sigaction(SIGINT, &action, &old_action); + + // Start the web server. + ctx = mg_start(&callbacks, NULL, options); + while (1) + std::this_thread::sleep_for(std::chrono::seconds(1)); + return 1; +} |