summaryrefslogtreecommitdiff
path: root/datastore-leveldb/src/web.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'datastore-leveldb/src/web.cpp')
-rw-r--r--datastore-leveldb/src/web.cpp27
1 files changed, 15 insertions, 12 deletions
diff --git a/datastore-leveldb/src/web.cpp b/datastore-leveldb/src/web.cpp
index 2adf8eb..efff6b9 100644
--- a/datastore-leveldb/src/web.cpp
+++ b/datastore-leveldb/src/web.cpp
@@ -23,7 +23,7 @@ static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) {
const boost::regex web_handle_api_value_R(
- "/api/value/([a-zA-Z0-9]+)/([0-9]+)");
+ "/api/value/([a-zA-Z0-9\\.]+)/([0-9]+)");
void web_handle_api_value(const boost::cmatch &match, struct mg_connection *conn) {
std::string sensor(match[1].str());
uint64_t timestamp = std::stoul(match[2].str());
@@ -69,7 +69,7 @@ static inline void print_json_tuple(struct mg_connection *conn,
const boost::regex web_handle_api_range_R(
- "/api/range/([a-zA-Z0-9]+)/([0-9]+)/([0-9]+)");
+ "/api/range/([a-zA-Z0-9\\.]+)/([0-9]+)/([0-9]+)");
void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn) {
std::string sensor(match[1].str());
uint64_t start = std::stoul(match[2].str());
@@ -86,11 +86,13 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
- "Content-Type: application/json; encoding=UTF-8\r\n"
+ "Content-Type: application/json\r\n"
"\r\n");
std::ostringstream out;
- out << "{'sensor':'" << sensor << "', 'error':null, 'data':[";
+ out << "{\"sensor\":\"" << sensor << "\", "
+ << "\"error\":null, "
+ << "\"data\":[";
mg_write(conn, out.str().c_str(), out.str().size());
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
@@ -109,7 +111,7 @@ void web_handle_api_range(const boost::cmatch &match, struct mg_connection *conn
}
const boost::regex web_handle_api_range_size_R(
- "/api/range/([a-zA-Z0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)");
+ "/api/range/([a-zA-Z0-9\\.]+)/([0-9]+)/([0-9]+)/([0-9]+)");
void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection *conn) {
std::string sensor(match[1].str());
uint64_t start = std::stoul(match[2].str());
@@ -125,17 +127,17 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
- "Content-Type: application/json; encoding=UTF-8\r\n"
+ "Content-Type: application/json\r\n"
"\r\n");
uint64_t step = std::max((uint64_t)1, (end-start) / size);
std::ostringstream out;
- out << "{'sensor':'" << sensor << "', "
- << "'error':null, "
- << "'size':" << size << ", "
- << "'step':" << step << ", "
- << "'data':[";
+ out << "{\"sensor\":\"" << sensor << "\", "
+ << "\"error\":null, "
+ << "\"size\":" << size << ", "
+ << "\"step\":" << step << ", "
+ << "\"data\":[";
mg_write(conn, out.str().c_str(), out.str().size());
bool first = true;
@@ -144,6 +146,7 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
for (uint64_t key = start; key <= end; key += step) {
it->Seek(make_key(key));
+ if (!it->Valid()) continue;
if (first) first = false;
else outbuf << ",";
@@ -152,6 +155,6 @@ void web_handle_api_range_size(const boost::cmatch &match, struct mg_connection
outbuf.seekp(0);
actual_size++;
}
- mg_printf(conn, "], 'actual_size':%ld}\r\n", actual_size);
+ mg_printf(conn, "], \"actual_size\":%ld}\r\n", actual_size);
delete it;
}