diff options
author | Ebus-at-dockstar <ebus@dockstar> | 2014-09-18 18:01:01 +0200 |
---|---|---|
committer | Ebus-at-dockstar <ebus@dockstar> | 2014-09-18 18:01:01 +0200 |
commit | ced20d7fc3ee98005ea8a2d09d2a0d99361ff0a4 (patch) | |
tree | 56dd9df62c3adbb6d186930e52a6a8a3fc2a953e /datastore-leveldb/src | |
parent | 87e143e62d853ec60771141cadb2a6b1294e9de6 (diff) | |
download | ebus-alt-ced20d7fc3ee98005ea8a2d09d2a0d99361ff0a4.tar.gz ebus-alt-ced20d7fc3ee98005ea8a2d09d2a0d99361ff0a4.zip |
add /api/list
Diffstat (limited to 'datastore-leveldb/src')
-rw-r--r-- | datastore-leveldb/src/db.cpp | 18 | ||||
-rw-r--r-- | datastore-leveldb/src/db.h | 3 | ||||
-rw-r--r-- | datastore-leveldb/src/server.cpp | 3 | ||||
-rw-r--r-- | datastore-leveldb/src/web.cpp | 22 | ||||
-rw-r--r-- | datastore-leveldb/src/web.h | 3 |
5 files changed, 49 insertions, 0 deletions
diff --git a/datastore-leveldb/src/db.cpp b/datastore-leveldb/src/db.cpp index e307535..ce57772 100644 --- a/datastore-leveldb/src/db.cpp +++ b/datastore-leveldb/src/db.cpp @@ -5,6 +5,10 @@ #include <mutex> #include <unordered_map> #include <sstream> +extern "C" { +#include <dirent.h> +} + static std::unordered_map<std::string,leveldb::DB*> dbs; static std::mutex getDBmutex; @@ -72,3 +76,17 @@ void db_close() { std::cout << std::endl; } +std::list<std::string> * db_list() { + std::list<std::string> * list = new std::list<std::string>(); + struct dirent * dp; + DIR* dirp = opendir("data"); + if (dirp != NULL) { + while ((dp = readdir(dirp)) != NULL) { + if (dp->d_name[0] != '.') + list->push_back(std::string(dp->d_name)); + } + (void)closedir(dirp); + } + return list; +} + diff --git a/datastore-leveldb/src/db.h b/datastore-leveldb/src/db.h index 5e31b00..ed03d4b 100644 --- a/datastore-leveldb/src/db.h +++ b/datastore-leveldb/src/db.h @@ -2,6 +2,7 @@ #define HAVE_DB_H #include <leveldb/db.h> +#include <list> leveldb::DB *db_get(std::string& name); @@ -9,6 +10,8 @@ bool db_insert(std::string& name, const uint64_t timestamp, std::string& value); void db_close(); +std::list<std::string> * db_list(); + std::string db_make_key(const uint64_t timestamp); #endif /*HAVE_DB_H*/ diff --git a/datastore-leveldb/src/server.cpp b/datastore-leveldb/src/server.cpp index 83f4cdb..3fc0181 100644 --- a/datastore-leveldb/src/server.cpp +++ b/datastore-leveldb/src/server.cpp @@ -95,6 +95,9 @@ int main(int argc, char **argv) { web_handler.push_front(std::make_pair( web_handle_api_range_size_R, web_handle_api_range_size)); + web_handler.push_front(std::make_pair( + web_handle_api_list_R, + web_handle_api_list)); // Signals: handle C-c struct sigaction action; diff --git a/datastore-leveldb/src/web.cpp b/datastore-leveldb/src/web.cpp index 503e3aa..3485539 100644 --- a/datastore-leveldb/src/web.cpp +++ b/datastore-leveldb/src/web.cpp @@ -177,3 +177,25 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection mg_write(conn, outbuf.str().c_str(), outbuf.tellp()); delete it; } + + +const boost::regex web_handle_api_list_R( + "/api/list"); +void web_handle_api_list(const boost::cmatch &match, struct mg_connection *conn) { + reply_header(conn, true, "OK", "Content-Type: application/json\r\n\r\n"); + mg_write(conn, "[", 1); + + std::list<std::string> * list = db_list(); + for (std::list<std::string>::iterator it = list->begin(); + it != list->end(); ++it) { + if (it != list->begin()) { + mg_write(conn, ",\"", 2); + } else { + mg_write(conn, "\"", 1); + } + mg_write(conn, (*it).c_str(), (*it).size()); + mg_write(conn, "\"\n", 2); + } + delete(list); + mg_write(conn, "]", 1); +} diff --git a/datastore-leveldb/src/web.h b/datastore-leveldb/src/web.h index a9d4593..9ee2cdd 100644 --- a/datastore-leveldb/src/web.h +++ b/datastore-leveldb/src/web.h @@ -18,4 +18,7 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn extern const boost::regex web_handle_api_range_size_R; void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection *conn); +extern const boost::regex web_handle_api_list_R; +void web_handle_api_list(const boost::cmatch &match, struct mg_connection *conn); + #endif/*HAVE_WEB_H*/ |