diff options
Diffstat (limited to 'datastore-leveldb/src/web.cpp')
-rw-r--r-- | datastore-leveldb/src/web.cpp | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/datastore-leveldb/src/web.cpp b/datastore-leveldb/src/web.cpp index d938a66..cbadc57 100644 --- a/datastore-leveldb/src/web.cpp +++ b/datastore-leveldb/src/web.cpp @@ -11,13 +11,23 @@ static inline uint64_t now() { std::chrono::high_resolution_clock::now().time_since_epoch()).count(); } +// uint64_t is long inline void strToL(unsigned long int *l, std::string s) { *l = std::stoul(s); } +// uint64_t is long long inline void strToL(unsigned long long int *l, std::string s) { *l = std::stoull(s); } +static inline void reply_header(struct mg_connection *conn, const bool ok, const char *msg, const char *extra = "") { + mg_printf(conn, + "HTTP/1.1 %s %s\r\n" + "%s", + (ok) ? "200" : "500", + msg, + extra); +} static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) { if (key.size() != 20+3) @@ -36,9 +46,9 @@ void web_handle_api_value(const boost::cmatch &match, struct mg_connection *conn std::string value(buf, bytesRead); if (db_insert(sensor, timestamp, value)) { - mg_printf(conn, "HTTP/1.1 200 OK Value received\r\n\r\n"); + reply_header(conn, true, "OK Value received", "\r\n"); } else { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); } } @@ -52,9 +62,9 @@ void web_handle_api_value_timestamp(const boost::cmatch &match, struct mg_connec std::string value(buf, bytesRead); if (db_insert(sensor, timestamp, value)) { - mg_printf(conn, "HTTP/1.1 200 OK Value received\r\n\r\n"); + reply_header(conn, true, "OK Value received", "\r\n"); } else { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); } } @@ -90,20 +100,16 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn std::string sensor(match[1].str()); uint64_t start; strToL(&start, match[2].str()); uint64_t end; strToL(&end, match[3].str()); - std::string key_start(std::move(make_key(start))); - std::string key_end(std::move(make_key(end))); + std::string key_start(std::move(db_make_key(start))); + std::string key_end(std::move(db_make_key(end))); - leveldb::DB *db = getDB(sensor); + leveldb::DB *db = db_get(sensor); if (db == nullptr) { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n" - "\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); return; } - mg_printf(conn, - "HTTP/1.1 200 OK\r\n" - "Content-Type: application/json\r\n" - "\r\n"); + reply_header(conn, true, "OK", "Content-Type: application/json\r\n\r\n"); std::ostringstream out; out << "{\"sensor\":\"" << sensor << "\", " @@ -134,17 +140,13 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection uint64_t end; strToL(&end, match[3].str()); uint64_t size; strToL(&size, match[4].str()); - leveldb::DB *db = getDB(sensor); + leveldb::DB *db = db_get(sensor); if (db == nullptr) { - mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n" - "\r\n"); + reply_header(conn, false, "Internal Error", "\r\n"); return; } - mg_printf(conn, - "HTTP/1.1 200 OK\r\n" - "Content-Type: application/json\r\n" - "\r\n"); + reply_header(conn, true, "OK", "Content-Type: application/json\r\n\r\n"); uint64_t step = std::max((uint64_t)1, (end-start) / size); @@ -161,7 +163,7 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection std::ostringstream outbuf; leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); for (uint64_t key = start; key < end; key += step) { - it->Seek(make_key(key)); + it->Seek(db_make_key(key)); if (!it->Valid()) continue; if (first) first = false; |