Unverified Commit e4579db6 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #661 from dennisjenkins75/master

Reformatted with 'clang-format' with default options.
parents c7e9ac4c 2447ef55
...@@ -8,13 +8,13 @@ ...@@ -8,13 +8,13 @@
#include <pistache/description.h> #include <pistache/description.h>
#include <pistache/http_header.h> #include <pistache/http_header.h>
#include <sstream>
#include <algorithm> #include <algorithm>
#include <sstream>
namespace Pistache { namespace Pistache {
namespace Rest { namespace Rest {
const char* schemeString(Scheme scheme) { const char *schemeString(Scheme scheme) {
switch (scheme) { switch (scheme) {
#define SCHEME(e, str) \ #define SCHEME(e, str) \
case Scheme::e: \ case Scheme::e: \
...@@ -28,59 +28,39 @@ const char* schemeString(Scheme scheme) { ...@@ -28,59 +28,39 @@ const char* schemeString(Scheme scheme) {
namespace Schema { namespace Schema {
Contact::Contact( Contact::Contact(std::string name, std::string url, std::string email)
std::string name, std::string url, std::string email) : name(std::move(name)), url(std::move(url)), email(std::move(email)) {}
: name(std::move(name))
, url(std::move(url)) License::License(std::string name, std::string url)
, email(std::move(email)) : name(std::move(name)), url(std::move(url)) {}
{ }
Info::Info(std::string title, std::string version, std::string description)
License::License( : title(std::move(title)), version(std::move(version)),
std::string name, std::string url) description(std::move(description)), termsOfService(), contact(),
: name(std::move(name)) license() {}
, url(std::move(url))
{ } PathDecl::PathDecl(std::string value, Http::Method method)
: value(std::move(value)), method(method) {}
Info::Info(
std::string title, std::string version, std::string description)
: title(std::move(title))
, version(std::move(version))
, description(std::move(description))
, termsOfService()
, contact()
, license()
{ }
PathDecl::PathDecl(
std::string value, Http::Method method)
: value(std::move(value))
, method(method)
{ }
Path::Path(std::string value, Http::Method method, std::string description) Path::Path(std::string value, Http::Method method, std::string description)
: value(std::move(value)) : value(std::move(value)), method(method),
, method(method) description(std::move(description)), hidden(false), pc(), parameters(),
, description(std::move(description)) responses(), handler() {}
, hidden(false)
, pc() std::string Path::swaggerFormat(const std::string &path) {
, parameters() if (path.empty())
, responses() return "";
, handler() if (path[0] != '/')
{ } throw std::invalid_argument("Invalid path, should start with a '/'");
std::string
Path::swaggerFormat(const std::string& path) {
if (path.empty()) return "";
if (path[0] != '/') throw std::invalid_argument("Invalid path, should start with a '/'");
/* @CodeDup: re-use the private Fragment class of Router */ /* @CodeDup: re-use the private Fragment class of Router */
auto isPositional = [](const std::string& fragment) { auto isPositional = [](const std::string &fragment) {
if (fragment[0] == ':') if (fragment[0] == ':')
return std::make_pair(true, fragment.substr(1)); return std::make_pair(true, fragment.substr(1));
return std::make_pair(false, std::string()); return std::make_pair(false, std::string());
}; };
auto isOptional = [](const std::string& fragment) { auto isOptional = [](const std::string &fragment) {
auto pos = fragment.find('?'); auto pos = fragment.find('?');
// @Validation the '?' should be the last character // @Validation the '?' should be the last character
return std::make_pair(pos != std::string::npos, pos); return std::make_pair(pos != std::string::npos, pos);
...@@ -111,14 +91,15 @@ Path::swaggerFormat(const std::string& path) { ...@@ -111,14 +91,15 @@ Path::swaggerFormat(const std::string& path) {
} }
for (size_t i = 0; i < fragments.size() - 1; ++i) { for (size_t i = 0; i < fragments.size() - 1; ++i) {
const auto& frag = fragments[i]; const auto &frag = fragments[i];
processFragment(frag); processFragment(frag);
oss << '/'; oss << '/';
} }
const auto& last = fragments.back(); const auto &last = fragments.back();
if (last.empty()) oss << '/'; if (last.empty())
oss << '/';
else { else {
processFragment(last); processFragment(last);
} }
...@@ -126,35 +107,30 @@ Path::swaggerFormat(const std::string& path) { ...@@ -126,35 +107,30 @@ Path::swaggerFormat(const std::string& path) {
return oss.str(); return oss.str();
} }
bool bool PathGroup::hasPath(const std::string &name, Http::Method method) const {
PathGroup::hasPath(const std::string& name, Http::Method method) const {
auto group = paths(name); auto group = paths(name);
auto it = std::find_if(std::begin(group), std::end(group), [&](const Path& p) { auto it = std::find_if(std::begin(group), std::end(group),
return p.method == method; [&](const Path &p) { return p.method == method; });
});
return it != std::end(group); return it != std::end(group);
} }
bool bool PathGroup::hasPath(const Path &path) const {
PathGroup::hasPath(const Path& path) const {
return hasPath(path.value, path.method); return hasPath(path.value, path.method);
} }
PathGroup::Group PathGroup::Group PathGroup::paths(const std::string &name) const {
PathGroup::paths(const std::string& name) const {
auto it = groups_.find(name); auto it = groups_.find(name);
if (it == std::end(groups_)) if (it == std::end(groups_))
return PathGroup::Group { }; return PathGroup::Group{};
return it->second; return it->second;
} }
Optional<Path> Optional<Path> PathGroup::path(const std::string &name,
PathGroup::path(const std::string& name, Http::Method method) const { Http::Method method) const {
auto group = paths(name); auto group = paths(name);
auto it = std::find_if(std::begin(group), std::end(group), [&](const Path& p) { auto it = std::find_if(std::begin(group), std::end(group),
return p.method == method; [&](const Path &p) { return p.method == method; });
});
if (it != std::end(group)) { if (it != std::end(group)) {
return Optional<Path>(Some(*it)); return Optional<Path>(Some(*it));
...@@ -162,255 +138,205 @@ PathGroup::path(const std::string& name, Http::Method method) const { ...@@ -162,255 +138,205 @@ PathGroup::path(const std::string& name, Http::Method method) const {
return Optional<Path>(None()); return Optional<Path>(None());
} }
PathGroup::group_iterator PathGroup::group_iterator PathGroup::add(Path path) {
PathGroup::add(Path path) {
if (hasPath(path)) if (hasPath(path))
return PathGroup::group_iterator { }; return PathGroup::group_iterator{};
auto &group = groups_[path.value]; auto &group = groups_[path.value];
return group.insert(group.end(), std::move(path)); return group.insert(group.end(), std::move(path));
} }
PathGroup::const_iterator PathGroup::const_iterator PathGroup::begin() const { return groups_.begin(); }
PathGroup::begin() const {
return groups_.begin();
}
PathGroup::const_iterator PathGroup::const_iterator PathGroup::end() const { return groups_.end(); }
PathGroup::end() const {
return groups_.end();
}
PathGroup::flat_iterator PathGroup::flat_iterator PathGroup::flatBegin() const {
PathGroup::flatBegin() const {
return makeFlatMapIterator(groups_, begin()); return makeFlatMapIterator(groups_, begin());
} }
PathGroup::flat_iterator PathGroup::flat_iterator PathGroup::flatEnd() const {
PathGroup::flatEnd() const {
return makeFlatMapIterator(groups_, end()); return makeFlatMapIterator(groups_, end());
} }
PathBuilder::PathBuilder(Path* path) PathBuilder::PathBuilder(Path *path) : path_(path) {}
: path_(path)
{ }
SubPath::SubPath( SubPath::SubPath(std::string prefix, PathGroup *paths)
std::string prefix, PathGroup* paths) : prefix(std::move(prefix)), parameters(), paths(paths) {}
: prefix(std::move(prefix))
, parameters()
, paths(paths)
{ }
PathBuilder PathBuilder SubPath::route(const std::string &name, Http::Method method,
SubPath::route(const std::string &name, Http::Method method, std::string description) { std::string description) {
auto fullPath = prefix + name; auto fullPath = prefix + name;
Path path(std::move(fullPath), method, std::move(description)); Path path(std::move(fullPath), method, std::move(description));
std::copy(std::begin(parameters), std::end(parameters), std::back_inserter(path.parameters)); std::copy(std::begin(parameters), std::end(parameters),
std::back_inserter(path.parameters));
auto it = paths->add(std::move(path)); auto it = paths->add(std::move(path));
return PathBuilder(&*it); return PathBuilder(&*it);
} }
PathBuilder PathBuilder SubPath::route(PathDecl fragment, std::string description) {
SubPath::route(PathDecl fragment, std::string description) { return route(std::move(fragment.value), fragment.method,
return route(std::move(fragment.value), fragment.method, std::move(description)); std::move(description));
} }
SubPath SubPath SubPath::path(const std::string &prefix) {
SubPath::path(const std::string &prefix) {
return SubPath(this->prefix + prefix, paths); return SubPath(this->prefix + prefix, paths);
} }
Parameter::Parameter( Parameter::Parameter(std::string name, std::string description)
std::string name, std::string description) : name(std::move(name)), description(std::move(description)),
: name(std::move(name)) required(true), type() {}
, description(std::move(description))
, required(true)
, type()
{ }
Response::Response( Response::Response(Http::Code statusCode, std::string description)
Http::Code statusCode, std::string description) : statusCode(statusCode), description(std::move(description)) {}
: statusCode(statusCode)
, description(std::move(description))
{ }
ResponseBuilder::ResponseBuilder( ResponseBuilder::ResponseBuilder(Http::Code statusCode, std::string description)
Http::Code statusCode, std::string description) : response_(statusCode, std::move(description)) {}
: response_(statusCode, std::move(description))
{ }
InfoBuilder::InfoBuilder(Info* info) InfoBuilder::InfoBuilder(Info *info) : info_(info) {}
: info_(info)
{ }
InfoBuilder& InfoBuilder &InfoBuilder::termsOfService(std::string value) {
InfoBuilder::termsOfService(std::string value) {
info_->termsOfService = std::move(value); info_->termsOfService = std::move(value);
return *this; return *this;
} }
InfoBuilder& InfoBuilder &InfoBuilder::contact(std::string name, std::string url,
InfoBuilder::contact(std::string name, std::string url, std::string email) { std::string email) {
info_->contact = Some(Contact(std::move(name), std::move(url), std::move(email))); info_->contact =
Some(Contact(std::move(name), std::move(url), std::move(email)));
return *this; return *this;
} }
InfoBuilder& InfoBuilder &InfoBuilder::license(std::string name, std::string url) {
InfoBuilder::license(std::string name, std::string url) {
info_->license = Some(License(std::move(name), std::move(url))); info_->license = Some(License(std::move(name), std::move(url)));
return *this; return *this;
} }
} // namespace Schema } // namespace Schema
Description::Description( Description::Description(std::string title, std::string version,
std::string title, std::string version, std::string description) std::string description)
: info_(std::move(title), std::move(version), std::move(description)) : info_(std::move(title), std::move(version), std::move(description)),
, host_() host_(), basePath_(), schemes_(), pc(), paths_() {}
, basePath_()
, schemes_()
, pc()
, paths_()
{
}
Schema::InfoBuilder Schema::InfoBuilder Description::info() {
Description::info() {
Schema::InfoBuilder builder(&info_); Schema::InfoBuilder builder(&info_);
return builder; return builder;
} }
Description& Description &Description::host(std::string value) {
Description::host(std::string value) {
host_ = std::move(value); host_ = std::move(value);
return *this; return *this;
} }
Description& Description &Description::basePath(std::string value) {
Description::basePath(std::string value) {
basePath_ = std::move(value); basePath_ = std::move(value);
return *this; return *this;
} }
Schema::PathDecl Schema::PathDecl Description::options(std::string name) {
Description::options(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Options); return Schema::PathDecl(std::move(name), Http::Method::Options);
} }
Schema::PathDecl Schema::PathDecl Description::get(std::string name) {
Description::get(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Get); return Schema::PathDecl(std::move(name), Http::Method::Get);
} }
Schema::PathDecl Schema::PathDecl Description::post(std::string name) {
Description::post(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Post); return Schema::PathDecl(std::move(name), Http::Method::Post);
} }
Schema::PathDecl Schema::PathDecl Description::head(std::string name) {
Description::head(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Head); return Schema::PathDecl(std::move(name), Http::Method::Head);
} }
Schema::PathDecl Schema::PathDecl Description::put(std::string name) {
Description::put(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Put); return Schema::PathDecl(std::move(name), Http::Method::Put);
} }
Schema::PathDecl Schema::PathDecl Description::patch(std::string name) {
Description::patch(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Patch); return Schema::PathDecl(std::move(name), Http::Method::Patch);
} }
Schema::PathDecl Schema::PathDecl Description::del(std::string name) {
Description::del(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Delete); return Schema::PathDecl(std::move(name), Http::Method::Delete);
} }
Schema::PathDecl Schema::PathDecl Description::trace(std::string name) {
Description::trace(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Trace); return Schema::PathDecl(std::move(name), Http::Method::Trace);
} }
Schema::PathDecl Schema::PathDecl Description::connect(std::string name) {
Description::connect(std::string name) {
return Schema::PathDecl(std::move(name), Http::Method::Connect); return Schema::PathDecl(std::move(name), Http::Method::Connect);
} }
Schema::SubPath Schema::SubPath Description::path(std::string name) {
Description::path(std::string name) {
return Schema::SubPath(std::move(name), &paths_); return Schema::SubPath(std::move(name), &paths_);
} }
Schema::PathBuilder Schema::PathBuilder Description::route(std::string name, Http::Method method,
Description::route(std::string name, Http::Method method, std::string description) { std::string description) {
auto it = paths_.emplace(std::move(name), method, std::move(description)); auto it = paths_.emplace(std::move(name), method, std::move(description));
return Schema::PathBuilder(&*it); return Schema::PathBuilder(&*it);
} }
Schema::PathBuilder Schema::PathBuilder Description::route(Schema::PathDecl fragment,
Description::route(Schema::PathDecl fragment, std::string description) { std::string description) {
return route(std::move(fragment.value), fragment.method, std::move(description)); return route(std::move(fragment.value), fragment.method,
std::move(description));
} }
Schema::ResponseBuilder Schema::ResponseBuilder Description::response(Http::Code statusCode,
Description::response(Http::Code statusCode, std::string description) { std::string description) {
Schema::ResponseBuilder builder(statusCode, std::move(description)); Schema::ResponseBuilder builder(statusCode, std::move(description));
return builder; return builder;
} }
Swagger& Swagger &Swagger::uiPath(std::string path) {
Swagger::uiPath(std::string path) {
uiPath_ = std::move(path); uiPath_ = std::move(path);
return *this; return *this;
} }
Swagger& Swagger &Swagger::uiDirectory(std::string dir) {
Swagger::uiDirectory(std::string dir) {
uiDirectory_ = std::move(dir); uiDirectory_ = std::move(dir);
return *this; return *this;
} }
Swagger& Swagger &Swagger::apiPath(std::string path) {
Swagger::apiPath(std::string path) {
apiPath_ = std::move(path); apiPath_ = std::move(path);
return *this; return *this;
} }
Swagger& Swagger &Swagger::serializer(Swagger::Serializer serialize) {
Swagger::serializer(Swagger::Serializer serialize) {
serializer_ = std::move(serialize); serializer_ = std::move(serialize);
return *this; return *this;
} }
void void Swagger::install(Rest::Router &router) {
Swagger::install(Rest::Router& router) {
Route::Handler uiHandler = [=](const Rest::Request& req, Http::ResponseWriter response) { Route::Handler uiHandler = [=](const Rest::Request &req,
Http::ResponseWriter response) {
auto res = req.resource(); auto res = req.resource();
/* /*
* @Export might be useful for routing also. Make it public or merge it with the Fragment class * @Export might be useful for routing also. Make it public or merge it with
* the Fragment class
*/ */
struct Path { struct Path {
explicit Path(const std::string& value) explicit Path(const std::string &value)
: value(value) : value(value), trailingSlashValue(value) {
, trailingSlashValue(value)
{
if (trailingSlashValue.empty() || trailingSlashValue.back() != '/') if (trailingSlashValue.empty() || trailingSlashValue.back() != '/')
trailingSlashValue += '/'; trailingSlashValue += '/';
} }
static bool hasTrailingSlash(const Rest::Request& req) { static bool hasTrailingSlash(const Rest::Request &req) {
auto res_ = req.resource(); auto res_ = req.resource();
return res_.back() == '/'; return res_.back() == '/';
} }
std::string stripPrefix(const Rest::Request& req) { std::string stripPrefix(const Rest::Request &req) {
auto res_ = req.resource(); auto res_ = req.resource();
if (!res_.compare(0, value.size(), value)) { if (!res_.compare(0, value.size(), value)) {
return res_.substr(value.size()); return res_.substr(value.size());
...@@ -418,7 +344,7 @@ Swagger::install(Rest::Router& router) { ...@@ -418,7 +344,7 @@ Swagger::install(Rest::Router& router) {
return res_; return res_;
} }
bool matches(const Rest::Request& req) const { bool matches(const Rest::Request &req) const {
auto res_ = req.resource(); auto res_ = req.resource();
if (value == res_) if (value == res_)
return true; return true;
...@@ -429,19 +355,21 @@ Swagger::install(Rest::Router& router) { ...@@ -429,19 +355,21 @@ Swagger::install(Rest::Router& router) {
return false; return false;
} }
bool isPrefix(const Rest::Request& req) { bool isPrefix(const Rest::Request &req) {
auto res_ = req.resource(); auto res_ = req.resource();
return !res_.compare(0, value.size(), value); return !res_.compare(0, value.size(), value);
} }
const std::string& withTrailingSlash() const { const std::string &withTrailingSlash() const {
return trailingSlashValue; return trailingSlashValue;
} }
std::string join(const std::string& value) const { std::string join(const std::string &value) const {
std::string val; std::string val;
if (value[0] == '/') val = value.substr(1); if (value[0] == '/')
else val = value; val = value.substr(1);
else
val = value;
return trailingSlashValue + val; return trailingSlashValue + val;
} }
...@@ -455,9 +383,7 @@ Swagger::install(Rest::Router& router) { ...@@ -455,9 +383,7 @@ Swagger::install(Rest::Router& router) {
if (ui.matches(req)) { if (ui.matches(req)) {
if (!Path::hasTrailingSlash(req)) { if (!Path::hasTrailingSlash(req)) {
response response.headers().add<Http::Header::Location>(uiPath_ + '/');
.headers()
.add<Http::Header::Location>(uiPath_ + '/');
response.send(Http::Code::Moved_Permanently); response.send(Http::Code::Moved_Permanently);
} else { } else {
...@@ -465,8 +391,7 @@ Swagger::install(Rest::Router& router) { ...@@ -465,8 +391,7 @@ Swagger::install(Rest::Router& router) {
Http::serveFile(response, index); Http::serveFile(response, index);
} }
return Route::Result::Ok; return Route::Result::Ok;
} } else if (ui.isPrefix(req)) {
else if (ui.isPrefix(req)) {
auto file = ui.stripPrefix(req); auto file = ui.stripPrefix(req);
auto path = uiDir.join(file); auto path = uiDir.join(file);
Http::serveFile(response, path); Http::serveFile(response, path);
...@@ -474,7 +399,8 @@ Swagger::install(Rest::Router& router) { ...@@ -474,7 +399,8 @@ Swagger::install(Rest::Router& router) {
} }
else if (res == apiPath_) { else if (res == apiPath_) {
response.send(Http::Code::Ok, serializer_(description_), MIME(Application, Json)); response.send(Http::Code::Ok, serializer_(description_),
MIME(Application, Json));
return Route::Result::Ok; return Route::Result::Ok;
} }
...@@ -484,6 +410,5 @@ Swagger::install(Rest::Router& router) { ...@@ -484,6 +410,5 @@ Swagger::install(Rest::Router& router) {
router.addCustomHandler(uiHandler); router.addCustomHandler(uiHandler);
} }
} // namespace Rest } // namespace Rest
} // namespace Pistache } // namespace Pistache
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment