Merge branch 'amnon/adding_api' of github.com:cloudius-systems/seastar-dev into db

Initial RESTful API, from Amnon.
This commit is contained in:
Avi Kivity
2015-04-13 19:00:40 +03:00
7 changed files with 126 additions and 1 deletions

View File

@@ -0,0 +1,29 @@
{
"apiVersion":"0.0.1",
"swaggerVersion":"1.2",
"basePath":"{{Protocol}}://{{Host}}",
"resourcePath":"/storage_service",
"produces":[
"application/json"
],
"apis":[
{
"path":"/storage_service/hostid/local",
"operations":[
{
"method":"GET",
"summary":"Returns the local host id",
"type":"string",
"nickname":"local_hostid",
"produces":[
"application/json"
],
"parameters":[
{
}
]
}
]
}
]
}

24
api/api.cc Normal file
View File

@@ -0,0 +1,24 @@
/*
* Copyright 2015 Cloudius Systems
*/
#include "api.hh"
#include "http/api_docs.hh"
#include "storage_service.hh"
namespace api {
future<> set_server(http_context& ctx) {
auto rb = std::make_shared < api_registry_builder > ("api/api-doc/");
return ctx.http_server.set_routes(rb->set_api_doc()).then([&ctx, rb] {
ctx.http_server.set_routes(rb->register_function("storage_service",
"The storage service API"))
.then([&ctx] {
return set_storage_service(ctx);
});
});
}
}

20
api/api.hh Normal file
View File

@@ -0,0 +1,20 @@
/*
* Copyright 2015 Cloudius Systems
*/
#ifndef API_API_HH_
#define API_API_HH_
#include "http/httpd.hh"
namespace api {
struct http_context {
http_server_control http_server;
};
future<> set_server(http_context& ctx);
}
#endif /* API_API_HH_ */

18
api/storage_service.cc Normal file
View File

@@ -0,0 +1,18 @@
/*
* Copyright 2015 Cloudius Systems
*/
#include "storage_service.hh"
#include "api/api-doc/storage_service.json.hh"
namespace api {
future<> set_storage_service(http_context& ctx) {
return ctx.http_server.set_routes([] (routes& r) {
httpd::storage_service_json::local_hostid.set(r, [](const_req req) {
return "";
});
});
}
}

16
api/storage_service.hh Normal file
View File

@@ -0,0 +1,16 @@
/*
* Copyright 2015 Cloudius Systems
*/
#ifndef API_STORAGE_SERVICE_HH_
#define API_STORAGE_SERVICE_HH_
#include "api.hh"
namespace api {
future<> set_storage_service(http_context& ctx);
}
#endif /* API_APP_HH_ */

View File

@@ -259,6 +259,11 @@ http = ['http/transformers.cc',
'http/request_parser.rl',
'http/api_docs.cc',
]
api = ['api/api.cc',
'api/api-doc/storage_service.json',
'api/storage_service.cc',
]
defines = []
libs = '-laio -lboost_program_options -lboost_system -lstdc++ -lm -lboost_unit_test_framework -lboost_thread -lcryptopp -lrt'
hwloc_libs = '-lhwloc -lnuma -lpciaccess -lxml2 -lz'
@@ -370,7 +375,7 @@ urchin_core = (['database.cc',
deps = {
'libseastar.a' : core + libnet,
'seastar.pc': [],
'seastar': ['main.cc'] + urchin_core,
'seastar': ['main.cc'] + http + api + urchin_core,
'tests/test-reactor': ['tests/test-reactor.cc'] + core,
'apps/httpd/httpd': ['apps/httpd/demo.json', 'apps/httpd/main.cc'] + http + libnet + core,
'apps/memcached/memcached': ['apps/memcached/memcache.cc'] + memcache_base,

13
main.cc
View File

@@ -8,6 +8,8 @@
#include "core/distributed.hh"
#include "thrift/server.hh"
#include "transport/server.hh"
#include "http/httpd.hh"
#include "api/api.hh"
namespace bpo = boost::program_options;
@@ -16,18 +18,21 @@ int main(int ac, char** av) {
app.add_options()
("cql-port", bpo::value<uint16_t>()->default_value(9042), "CQL port")
("thrift-port", bpo::value<uint16_t>()->default_value(9160), "Thrift port")
("api-port", bpo::value<uint16_t>()->default_value(10000), "Http Rest API port")
("datadir", bpo::value<std::string>()->default_value("/var/lib/cassandra/data"), "data directory");
auto server = std::make_unique<distributed<thrift_server>>();
distributed<database> db;
distributed<cql3::query_processor> qp;
service::storage_proxy proxy{db};
api::http_context ctx;
return app.run(ac, av, [&] {
auto&& config = app.configuration();
uint16_t thrift_port = config["thrift-port"].as<uint16_t>();
uint16_t cql_port = config["cql-port"].as<uint16_t>();
sstring datadir = config["datadir"].as<std::string>();
uint16_t api_port = config["api-port"].as<uint16_t>();
return db.start().then([datadir, &db] {
engine().at_exit([&db] { return db.stop(); });
@@ -49,6 +54,14 @@ int main(int ac, char** av) {
}).then([thrift_port] {
std::cout << "Thrift server listening on port " << thrift_port << " ...\n";
});
}).then([&db, api_port, &ctx]{
ctx.http_server.start().then([api_port, &ctx] {
return set_server(ctx);
}).then([&ctx, api_port] {
ctx.http_server.listen(api_port);
}).then([api_port] {
std::cout << "Seastar HTTP server listening on port " << api_port << " ...\n";
});
}).or_terminate();
});
}