Commit f33fae22 authored by octal's avatar octal

Routes can now be hidden from the swagger json file. Also added basePath

parent 07c3163f
...@@ -61,12 +61,14 @@ private: ...@@ -61,12 +61,14 @@ private:
auto backendErrorResponse = auto backendErrorResponse =
desc.response(Http::Code::Internal_Server_Error, "An error occured with the backend"); desc.response(Http::Code::Internal_Server_Error, "An error occured with the backend");
desc.schemes("http"); desc.schemes(Rest::Scheme::Http);
desc.basePath("/v1");
desc desc
.route(desc.get("/ready")) .route(desc.get("/ready"))
.bind(&Generic::handleReady) .bind(&Generic::handleReady)
.response(Http::Code::Ok, "Response to the /ready call"); .response(Http::Code::Ok, "Response to the /ready call")
.hide();
auto versionPath = desc.path("/v1"); auto versionPath = desc.path("/v1");
......
...@@ -50,10 +50,22 @@ namespace Type { ...@@ -50,10 +50,22 @@ namespace Type {
} // namespace Type } // namespace Type
enum class Flag { #define SCHEMES \
Optional, Required SCHEME(Http , "http") \
SCHEME(Https, "https") \
SCHEME(Ws , "ws") \
SCHEME(Wss , "wss") \
enum class Scheme {
#define SCHEME(e, _) e,
SCHEMES
#undef SCHEME
}; };
const char* schemeString(Scheme scheme);
namespace Schema { namespace Schema {
namespace Traits { namespace Traits {
...@@ -253,6 +265,7 @@ struct Path { ...@@ -253,6 +265,7 @@ struct Path {
std::string value; std::string value;
Http::Method method; Http::Method method;
std::string description; std::string description;
bool hidden;
std::vector<Http::Mime::MediaType> produceMimes; std::vector<Http::Mime::MediaType> produceMimes;
std::vector<Http::Mime::MediaType> consumeMimes; std::vector<Http::Mime::MediaType> consumeMimes;
...@@ -317,7 +330,17 @@ struct Path { ...@@ -317,7 +330,17 @@ struct Path {
class PathGroup { class PathGroup {
public: public:
typedef std::unordered_map<std::string, std::vector<Path>> Map; struct Group : public std::vector<Path> {
bool isHidden() const {
if (empty()) return false;
return std::all_of(begin(), end(), [](const Path& path) {
return path.hidden;
});
}
};
typedef std::unordered_map<std::string, Group> Map;
typedef Map::iterator iterator; typedef Map::iterator iterator;
typedef Map::const_iterator const_iterator; typedef Map::const_iterator const_iterator;
...@@ -330,7 +353,7 @@ public: ...@@ -330,7 +353,7 @@ public:
bool hasPath(const std::string& name, Http::Method method) const; bool hasPath(const std::string& name, Http::Method method) const;
bool hasPath(const Path& path) const; bool hasPath(const Path& path) const;
std::vector<Path> paths(const std::string& name) const; Group paths(const std::string& name) const;
Optional<Path> path(const std::string& name, Http::Method method) const; Optional<Path> path(const std::string& name, Http::Method method) const;
group_iterator add(Path path); group_iterator add(Path path);
...@@ -347,21 +370,32 @@ public: ...@@ -347,21 +370,32 @@ public:
flat_iterator flatEnd() const; flat_iterator flatEnd() const;
template<typename Writer> template<typename Writer>
void serialize(Writer& writer, Format format = Format::Default) const { void serialize(
Writer& writer, const std::string& prefix, Format format = Format::Default) const {
writer.String("paths"); writer.String("paths");
writer.StartObject(); writer.StartObject();
{ {
for (const auto& group: groups) { for (const auto& group: groups) {
if (group.second.isHidden()) continue;
std::string name(group.first);
if (!prefix.empty()) {
if (!name.compare(0, prefix.size(), prefix)) {
name = name.substr(prefix.size());
}
}
if (format == Format::Default) { if (format == Format::Default) {
writer.String(group.first.c_str()); writer.String(name.c_str());
} else { } else {
auto swaggerPath = Path::swaggerFormat(group.first); auto swaggerPath = Path::swaggerFormat(name);
writer.String(swaggerPath.c_str()); writer.String(swaggerPath.c_str());
} }
writer.StartObject(); writer.StartObject();
{ {
for (const auto& path: group.second) { for (const auto& path: group.second) {
path.serialize(writer); if (!path.hidden)
path.serialize(writer);
} }
} }
writer.EndObject(); writer.EndObject();
...@@ -436,6 +470,10 @@ struct PathBuilder { ...@@ -436,6 +470,10 @@ struct PathBuilder {
return *this; return *this;
} }
PathBuilder&
hide(bool value = true) {
path_->hidden = value;
}
private: private:
Path* path_; Path* path_;
...@@ -468,11 +506,11 @@ public: ...@@ -468,11 +506,11 @@ public:
Schema::InfoBuilder info(); Schema::InfoBuilder info();
Description& host(std::string value); Description& host(std::string value);
template<typename... Scheme> Description& basePath(std::string value);
Description& schemes(Scheme... schemes) { template<typename... Schemes>
// @Improve: try to statically assert that every Scheme is convertible to string Description& schemes(Schemes... schemes) {
const std::string s[] = { std::string(schemes)... }; const Scheme s[] = { schemes... };
std::copy(std::begin(s), std::end(s), std::back_inserter(schemes_)); std::copy(std::begin(s), std::end(s), std::back_inserter(schemes_));
return *this; return *this;
} }
...@@ -500,18 +538,22 @@ public: ...@@ -500,18 +538,22 @@ public:
writer.String("host"); writer.String("host");
writer.String(host_.c_str()); writer.String(host_.c_str());
} }
if (!basePath_.empty()) {
writer.String("basePath");
writer.String(basePath_.c_str());
}
if (!schemes_.empty()) { if (!schemes_.empty()) {
writer.String("schemes"); writer.String("schemes");
writer.StartArray(); writer.StartArray();
{ {
for (const auto& scheme: schemes_) { for (const auto& scheme: schemes_) {
writer.String(scheme.c_str()); writer.String(schemeString(scheme));
} }
} }
writer.EndArray(); writer.EndArray();
} }
paths_.serialize(writer, Schema::PathGroup::Format::Swagger); paths_.serialize(writer, basePath_, Schema::PathGroup::Format::Swagger);
} }
writer.EndObject(); writer.EndObject();
} }
...@@ -519,7 +561,8 @@ public: ...@@ -519,7 +561,8 @@ public:
private: private:
Schema::Info info_; Schema::Info info_;
std::string host_; std::string host_;
std::vector<std::string> schemes_; std::string basePath_;
std::vector<Scheme> schemes_;
Schema::PathGroup paths_; Schema::PathGroup paths_;
}; };
......
...@@ -14,6 +14,18 @@ namespace Net { ...@@ -14,6 +14,18 @@ namespace Net {
namespace Rest { namespace Rest {
const char* schemeString(Scheme scheme) {
switch (scheme) {
#define SCHEME(e, str) \
case Scheme::e: \
return str;
SCHEMES
#undef SCHEME
}
return nullptr;
}
namespace Schema { namespace Schema {
Contact::Contact( Contact::Contact(
...@@ -47,6 +59,7 @@ Path::Path( ...@@ -47,6 +59,7 @@ Path::Path(
: value(std::move(value)) : value(std::move(value))
, method(method) , method(method)
, description(std::move(description)) , description(std::move(description))
, hidden(false)
{ } { }
std::string std::string
...@@ -121,11 +134,11 @@ PathGroup::hasPath(const Path& path) const { ...@@ -121,11 +134,11 @@ PathGroup::hasPath(const Path& path) const {
return hasPath(path.value, path.method); return hasPath(path.value, path.method);
} }
std::vector<Path> 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 std::vector<Path> { }; return PathGroup::Group { };
return it->second; return it->second;
} }
...@@ -263,6 +276,12 @@ Description::host(std::string value) { ...@@ -263,6 +276,12 @@ Description::host(std::string value) {
return *this; return *this;
} }
Description&
Description::basePath(std::string value) {
basePath_ = std::move(value);
return *this;
}
Schema::PathFragment Schema::PathFragment
Description::get(std::string name) { Description::get(std::string name) {
return Schema::PathFragment(std::move(name), Http::Method::Get); return Schema::PathFragment(std::move(name), Http::Method::Get);
......
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