From faf3e42698f19aa08136ca4512f81dea83ce51b2 Mon Sep 17 00:00:00 2001 From: Martin Olivier Date: Tue, 8 Feb 2022 12:58:42 +0100 Subject: [PATCH 01/23] feat: dylib updated to 1.8.1 --- .gitmodules | 2 +- include/dylib | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index b06ff0a..a8bbe75 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "include/dylib"] path = include/dylib url = https://github.com/martin-olivier/dylib - branch = v1.7.1 + branch = v1.8.1 diff --git a/include/dylib b/include/dylib index 3c748b8..d1fb984 160000 --- a/include/dylib +++ b/include/dylib @@ -1 +1 @@ -Subproject commit 3c748b832d205bab19b0994d288a9a659aa6d996 +Subproject commit d1fb98415a7324e5579e738fe02723d7efabe236 From 930541d1c722344ecec17b934735e971e9487ad8 Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 8 Feb 2022 16:55:49 +0100 Subject: [PATCH 02/23] fix: Config, raw pointers to shared pointers --- include/ziapi/Config.hpp | 5 +++-- tests/Config.cpp | 17 +++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/include/ziapi/Config.hpp b/include/ziapi/Config.hpp index 4ae6336..978ec5a 100644 --- a/include/ziapi/Config.hpp +++ b/include/ziapi/Config.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -28,10 +29,10 @@ using Null = std::nullptr_t; using String = std::string; -using Array = std::vector; +using Array = std::vector>; /// Datatype for json/yaml-like data -using Dict = std::unordered_map; +using Dict = std::unordered_map>; using NodeVariant = std::variant; diff --git a/tests/Config.cpp b/tests/Config.cpp index 2bfc653..f7c9299 100644 --- a/tests/Config.cpp +++ b/tests/Config.cpp @@ -37,13 +37,10 @@ TEST(Config, SimpleBool) TEST(Config, SimpleArray) { - Node node_1(10); - Node node_2("Hello world"); - Node node_3(14.5f); Node array({ - &node_1, - &node_2, - &node_3, + std::make_shared(10), + std::make_shared("Hello world"), + std::make_shared(14.5f), }); ASSERT_EQ(array.AsArray()[0]->AsInt(), 10); @@ -55,7 +52,7 @@ TEST(Config, SimpleDict) { Node modules_count(10); Node dict = { - {"modules_count", &modules_count}, + {"modules_count", std::make_shared(modules_count)}, }; ASSERT_EQ(dict.AsDict()["modules_count"]->AsInt(), 10); @@ -65,13 +62,13 @@ TEST(Config, NestedAccess) { Node root("/var/www"); Node root_node({ - {"root", &root}, + {"root", std::make_shared(root)}, }); Node modules_node({ - {"directoryListing", &root_node}, + {"directoryListing", std::make_shared(root_node)}, }); Node cfg({ - {"modules", &modules_node}, + {"modules", std::make_shared(modules_node)}, }); ASSERT_EQ(cfg["modules"]["directoryListing"]["root"].AsString(), "/var/www"); From f145a3ca0ab39db63c166087a966baf0289f40df Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 8 Feb 2022 17:04:45 +0100 Subject: [PATCH 03/23] fix: Http.hpp comment line 74 (\'responses\' -> \'requests\') --- include/ziapi/Http.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ziapi/Http.hpp b/include/ziapi/Http.hpp index 6d67ad8..6485289 100644 --- a/include/ziapi/Http.hpp +++ b/include/ziapi/Http.hpp @@ -71,7 +71,7 @@ class IResponseInputQueue { }; /** - * IRequestOutputQueue is a consumer-only container for HTTP responses. + * IRequestOutputQueue is a consumer-only container for HTTP requests. */ class IRequestOutputQueue { public: From 8927caa3ec3dd0a474ac73d7daf3c958f0233c2e Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 8 Feb 2022 19:35:03 +0100 Subject: [PATCH 04/23] fix: Version.hpp (major, minor) -> (major, minor, patch) --- docs/examples/DIRECTORY_LISTING.md | 4 +- docs/examples/REDIRECTION.md | 4 +- docs/guides/MODULES_101.md | 4 +- examples/dylib/Module.cpp | 4 +- .../modules/compressor/CompressorModule.hpp | 4 +- .../decompressor/DecompressorModule.hpp | 4 +- .../DirectoryListingModule.hpp | 4 +- examples/modules/logger/LoggerModule.hpp | 4 +- examples/modules/redirection/Module.hpp | 4 +- include/dylib | 2 +- include/ziapi/Config.hpp | 20 +++++ include/ziapi/Version.hpp | 34 ++++---- tests/Compressor.cpp | 4 +- tests/Config.cpp | 21 +---- tests/Decompressor.cpp | 4 +- tests/Version.cpp | 78 ++++++++++--------- 16 files changed, 104 insertions(+), 95 deletions(-) diff --git a/docs/examples/DIRECTORY_LISTING.md b/docs/examples/DIRECTORY_LISTING.md index 2c5ce3f..b5618f7 100644 --- a/docs/examples/DIRECTORY_LISTING.md +++ b/docs/examples/DIRECTORY_LISTING.md @@ -26,9 +26,9 @@ class DirectoryListingModule : public ziapi::IHandlerModule { public: void Init(const ziapi::config::Node &cfg) override {} - [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return {1, 0}; } + [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return {3, 0, 0}; } - [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return {1, 0}; } + [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return {3, 0, 0}; } [[nodiscard]] const char *GetName() const noexcept override { return "DirectoryListing"; } diff --git a/docs/examples/REDIRECTION.md b/docs/examples/REDIRECTION.md index 8107158..e0ec1ea 100644 --- a/docs/examples/REDIRECTION.md +++ b/docs/examples/REDIRECTION.md @@ -29,9 +29,9 @@ class PhpCgiModule : public ziapi::IHandlerModule { public: void Init(const ziapi::config::Node &cfg) override {} - [[nodiscard]] Version GetVersion() const noexcept { return {0, 1}; } + [[nodiscard]] Version GetVersion() const noexcept { return {3, 0, 0}; } - [[nodiscard]] Version GetCompatibleApiVersion() const noexcept { return {0, 1}; } + [[nodiscard]] Version GetCompatibleApiVersion() const noexcept { return {3, 0, 0}; } [[nodiscard]] const char *GetName() const noexcept { return "Redirection Module"; } diff --git a/docs/guides/MODULES_101.md b/docs/guides/MODULES_101.md index 5899cef..7afecc6 100644 --- a/docs/guides/MODULES_101.md +++ b/docs/guides/MODULES_101.md @@ -82,9 +82,9 @@ public: void Init(const ziapi::config::Node &) override {} - ziapi::Version GetVersion() const noexcept override { return {1, 0}; } + ziapi::Version GetVersion() const noexcept override { return {3, 0, 0}; } - ziapi::Version GetCompatibleApiVersion() const noexcept override { return {1, 0}; } + ziapi::Version GetCompatibleApiVersion() const noexcept override { return {3, 0, 0}; } [[nodiscard]] virtual const char *GetName() const noexcept override { return "module_name"; } diff --git a/examples/dylib/Module.cpp b/examples/dylib/Module.cpp index 446d210..9bf4758 100644 --- a/examples/dylib/Module.cpp +++ b/examples/dylib/Module.cpp @@ -10,9 +10,9 @@ class Module : public ziapi::IModule { void Init(const ziapi::config::Node &) override {} - ziapi::Version GetVersion() const noexcept override { return {1, 0}; } + ziapi::Version GetVersion() const noexcept override { return {3, 0, 0}; } - ziapi::Version GetCompatibleApiVersion() const noexcept override { return {1, 0}; } + ziapi::Version GetCompatibleApiVersion() const noexcept override { return {3, 0, 0}; } [[nodiscard]] virtual const char *GetName() const noexcept override { return "module_name"; } diff --git a/examples/modules/compressor/CompressorModule.hpp b/examples/modules/compressor/CompressorModule.hpp index 52e96a1..e0ebc02 100644 --- a/examples/modules/compressor/CompressorModule.hpp +++ b/examples/modules/compressor/CompressorModule.hpp @@ -7,9 +7,9 @@ class CompressorModule : public ziapi::IPostProcessorModule { // Don't need anything to configure in this implementation } - [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return ziapi::Version{1, 0}; } + [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return ziapi::Version{3, 0, 0}; } - [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return ziapi::Version{1, 0}; } + [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return ziapi::Version{3, 0, 0}; } [[nodiscard]] const char *GetName() const noexcept override { return "CompressorModule"; } diff --git a/examples/modules/decompressor/DecompressorModule.hpp b/examples/modules/decompressor/DecompressorModule.hpp index 214937b..430cc35 100644 --- a/examples/modules/decompressor/DecompressorModule.hpp +++ b/examples/modules/decompressor/DecompressorModule.hpp @@ -9,9 +9,9 @@ class DecompressorModule : public ziapi::IPreProcessorModule { // Don't need anything to configure in this implementation } - [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return ziapi::Version{1, 0}; } + [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return ziapi::Version{3, 0, 0}; } - [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return ziapi::Version{1, 0}; } + [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return ziapi::Version{3, 0, 0}; } [[nodiscard]] const char *GetName() const noexcept override { return "DecompressorModule"; } diff --git a/examples/modules/directory-listing/DirectoryListingModule.hpp b/examples/modules/directory-listing/DirectoryListingModule.hpp index 06dc0e3..a996ff1 100644 --- a/examples/modules/directory-listing/DirectoryListingModule.hpp +++ b/examples/modules/directory-listing/DirectoryListingModule.hpp @@ -14,9 +14,9 @@ class DirectoryListingModule : public ziapi::IHandlerModule { root_ = cfg.AsDict()["modules"]->AsDict()["directoryListing"]->AsDict()["root"]->AsString(); } - [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return {1, 0}; } + [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return {3, 0, 0}; } - [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return {1, 0}; } + [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return {3, 0, 0}; } [[nodiscard]] const char *GetName() const noexcept override { return "DirectoryListing"; } diff --git a/examples/modules/logger/LoggerModule.hpp b/examples/modules/logger/LoggerModule.hpp index 76d4814..e99293a 100644 --- a/examples/modules/logger/LoggerModule.hpp +++ b/examples/modules/logger/LoggerModule.hpp @@ -8,9 +8,9 @@ class LoggerModule : virtual public ziapi::IPreProcessorModule, public ziapi::IP public: void Init(const ziapi::config::Node &config) {} - [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return {1, 0}; } + [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return {3, 0, 0}; } - [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return {1, 0}; } + [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept override { return {3, 0, 0}; } [[nodiscard]] const char *GetName() const noexcept override { return "LoggerModule"; } diff --git a/examples/modules/redirection/Module.hpp b/examples/modules/redirection/Module.hpp index 8a2de2e..2a4ac8f 100644 --- a/examples/modules/redirection/Module.hpp +++ b/examples/modules/redirection/Module.hpp @@ -34,9 +34,9 @@ class RedirectionModule : public ziapi::IHandlerModule { return 0.9f; } - [[nodiscard]] ziapi::Version GetVersion() const noexcept { return {0, 1}; } + [[nodiscard]] ziapi::Version GetVersion() const noexcept { return {3, 0, 0}; } - [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept { return {0, 1}; } + [[nodiscard]] ziapi::Version GetCompatibleApiVersion() const noexcept { return {3, 0, 0}; } [[nodiscard]] const char *GetName() const noexcept { return "Redirection Module"; } diff --git a/include/dylib b/include/dylib index d1fb984..3c748b8 160000 --- a/include/dylib +++ b/include/dylib @@ -1 +1 @@ -Subproject commit d1fb98415a7324e5579e738fe02723d7efabe236 +Subproject commit 3c748b832d205bab19b0994d288a9a659aa6d996 diff --git a/include/ziapi/Config.hpp b/include/ziapi/Config.hpp index 978ec5a..d92af70 100644 --- a/include/ziapi/Config.hpp +++ b/include/ziapi/Config.hpp @@ -40,6 +40,26 @@ struct Node : public NodeVariant { public: using NodeVariant::NodeVariant; + static Node FromArray(const std::initializer_list &values) + { + Array arr; + + for (auto &value : values) { + arr.push_back(std::make_shared(value)); + } + return arr; + } + + static Node FromDict(const std::initializer_list> &values) + { + Dict dict; + + for (auto &value : values) { + dict[value.first] = std::make_shared(value.second); + } + return dict; + } + /// Used to construct a Node from a Dict Node(const std::initializer_list &values) : NodeVariant(Dict(values)){}; diff --git a/include/ziapi/Version.hpp b/include/ziapi/Version.hpp index 81aaffb..d2ff460 100644 --- a/include/ziapi/Version.hpp +++ b/include/ziapi/Version.hpp @@ -6,39 +6,37 @@ namespace ziapi { * Semantic versioning structure with a major and a minor version */ struct Version { - Version(int major, int minor) : major(major), minor(minor) {} + Version(int major, int minor, int patch) : major(major), minor(minor), patch(patch) {} inline bool operator==(const Version &other) const noexcept { - return major == other.major && minor == other.minor; + return major == other.major && minor == other.minor && patch == other.patch; } inline bool operator!=(const Version &other) const noexcept { return !(*this == other); } - inline bool operator>(const Version &other) const noexcept - { - if (major > other.major) - return true; - if (major < other.major) - return false; - return minor > other.minor; - } + inline bool operator>(const Version &other) const noexcept { return (*this >= other) && (*this != other); } - inline bool operator<(const Version &other) const noexcept + inline bool operator<(const Version &other) const noexcept { return !(*this > other) && other != *this; } + + inline bool operator>=(const Version &other) const noexcept { - if (major < other.major) - return true; - if (major > other.major) - return false; - return minor < other.minor; + return ( + major == other.major + ? minor == other.minor + ? patch == other.patch + ? true + : patch > other.patch + : minor > other.minor + : major > other.major + ); } - inline bool operator>=(const Version &other) const noexcept { return (*this > other) || (*this == other); } - inline bool operator<=(const Version &other) const noexcept { return (*this < other) || (*this == other); } unsigned int major; unsigned int minor; + unsigned int patch; }; } // namespace ziapi diff --git a/tests/Compressor.cpp b/tests/Compressor.cpp index 0251ef3..4e6ba9e 100644 --- a/tests/Compressor.cpp +++ b/tests/Compressor.cpp @@ -9,8 +9,8 @@ TEST(Compressor, UtilsInfo) ASSERT_EQ(std::string(compressor.GetName()), std::string("CompressorModule")); ASSERT_EQ(std::string(compressor.GetDescription()), std::string("Compress the response body before sending it back to the network")); - ASSERT_EQ(compressor.GetCompatibleApiVersion(), (ziapi::Version{1, 0})); - ASSERT_EQ(compressor.GetVersion(), (ziapi::Version{1, 0})); + ASSERT_EQ(compressor.GetCompatibleApiVersion(), (ziapi::Version{3, 0, 0})); + ASSERT_EQ(compressor.GetVersion(), (ziapi::Version{3, 0, 0})); } TEST(Compressor, compressionRate) diff --git a/tests/Config.cpp b/tests/Config.cpp index f7c9299..10c4c97 100644 --- a/tests/Config.cpp +++ b/tests/Config.cpp @@ -37,11 +37,7 @@ TEST(Config, SimpleBool) TEST(Config, SimpleArray) { - Node array({ - std::make_shared(10), - std::make_shared("Hello world"), - std::make_shared(14.5f), - }); + auto array = Node::FromArray({10, "Hello world", 14.5f}); ASSERT_EQ(array.AsArray()[0]->AsInt(), 10); ASSERT_EQ(array.AsArray()[1]->AsString(), "Hello world"); @@ -51,24 +47,15 @@ TEST(Config, SimpleArray) TEST(Config, SimpleDict) { Node modules_count(10); - Node dict = { - {"modules_count", std::make_shared(modules_count)}, - }; + auto dict = Node::FromDict({{"modules_count", modules_count}}); ASSERT_EQ(dict.AsDict()["modules_count"]->AsInt(), 10); } TEST(Config, NestedAccess) { - Node root("/var/www"); - Node root_node({ - {"root", std::make_shared(root)}, - }); - Node modules_node({ - {"directoryListing", std::make_shared(root_node)}, - }); - Node cfg({ - {"modules", std::make_shared(modules_node)}, + auto cfg = Node::FromDict({ + {"modules", Node::FromDict({{"directoryListing", Node::FromDict({{"root", "/var/www"}})}})}, }); ASSERT_EQ(cfg["modules"]["directoryListing"]["root"].AsString(), "/var/www"); diff --git a/tests/Decompressor.cpp b/tests/Decompressor.cpp index 1c9d8aa..843a2de 100644 --- a/tests/Decompressor.cpp +++ b/tests/Decompressor.cpp @@ -9,8 +9,8 @@ TEST(Decompressor, UtilsInfo) ASSERT_EQ(std::string(decompressor.GetName()), std::string("DecompressorModule")); ASSERT_EQ(std::string(decompressor.GetDescription()), std::string("Decompress the response body before sending it to the module pipeline")); - ASSERT_EQ(decompressor.GetCompatibleApiVersion(), (ziapi::Version{1, 0})); - ASSERT_EQ(decompressor.GetVersion(), (ziapi::Version{1, 0})); + ASSERT_EQ(decompressor.GetCompatibleApiVersion(), (ziapi::Version{3, 0, 0})); + ASSERT_EQ(decompressor.GetVersion(), (ziapi::Version{3, 0, 0})); } TEST(Decompressor, Decompression) diff --git a/tests/Version.cpp b/tests/Version.cpp index 745a5d3..1075297 100644 --- a/tests/Version.cpp +++ b/tests/Version.cpp @@ -4,108 +4,112 @@ TEST(Version, equal) { - ziapi::Version a{1, 0}; - ziapi::Version b{1, 0}; + ziapi::Version a{1, 0, 3}; + ziapi::Version b{1, 0, 3}; ASSERT_TRUE(a == b); - a = ziapi::Version{1, 1}; - b = ziapi::Version{1, 0}; + a = ziapi::Version{1, 1, 3}; + b = ziapi::Version{1, 0, 3}; ASSERT_FALSE(a == b); - a = ziapi::Version{2, 0}; - b = ziapi::Version{1, 1}; + a = ziapi::Version{2, 0, 3}; + b = ziapi::Version{1, 1, 3}; ASSERT_FALSE(a == b); } TEST(Version, not_equal) { - ziapi::Version a{1, 0}; - ziapi::Version b{1, 0}; + ziapi::Version a{1, 0, 0}; + ziapi::Version b{1, 0, 0}; ASSERT_FALSE(a != b); - a = ziapi::Version{1, 1}; - b = ziapi::Version{1, 0}; + a = ziapi::Version{1, 1, 0}; + b = ziapi::Version{1, 0, 0}; ASSERT_TRUE(a != b); - a = ziapi::Version{2, 0}; - b = ziapi::Version{1, 1}; + a = ziapi::Version{2, 0, 0}; + b = ziapi::Version{1, 1, 0}; ASSERT_TRUE(a != b); } TEST(Version, greater) { - ziapi::Version a{1, 0}; - ziapi::Version b{1, 0}; + ziapi::Version a{1, 0, 3}; + ziapi::Version b{1, 0, 3}; + std::cout << (a > b) << std::endl; ASSERT_FALSE(a > b); - a = ziapi::Version{1, 1}; - b = ziapi::Version{1, 0}; + a = ziapi::Version{1, 1, 2}; + b = ziapi::Version{1, 1, 0}; + std::cout << (a > b) << std::endl; ASSERT_TRUE(a > b); - a = ziapi::Version{2, 0}; - b = ziapi::Version{3, 1}; + a = ziapi::Version{2, 0, 2}; + b = ziapi::Version{3, 1, 2}; + std::cout << (a > b) << std::endl; ASSERT_FALSE(a > b); } TEST(Version, less) { - ziapi::Version a{1, 0}; - ziapi::Version b{1, 0}; + ziapi::Version a{1, 0, 1}; + ziapi::Version b{1, 0, 0}; ASSERT_FALSE(a < b); - a = ziapi::Version{1, 1}; - b = ziapi::Version{3, 4}; + a = ziapi::Version{1, 1, 0}; + b = ziapi::Version{3, 4, 0}; ASSERT_TRUE(a < b); - a = ziapi::Version{2, 0}; - b = ziapi::Version{1, 0}; + a = ziapi::Version{2, 0, 1}; + b = ziapi::Version{1, 0, 0}; ASSERT_FALSE(a < b); } TEST(Version, greater_or_equal) { - ziapi::Version a{1, 0}; - ziapi::Version b{1, 0}; + ziapi::Version a{1, 0, 0}; + ziapi::Version b{1, 0, 0}; ASSERT_TRUE(a >= b); - a = ziapi::Version{3, 1}; - b = ziapi::Version{1, 4}; + a = ziapi::Version{3, 1, 0}; + b = ziapi::Version{1, 4, 0}; ASSERT_TRUE(a >= b); - a = ziapi::Version{1, 6}; - b = ziapi::Version{3, 6}; + a = ziapi::Version{1, 6, 0}; + b = ziapi::Version{3, 6, 1}; ASSERT_FALSE(a >= b); } TEST(Version, less_or_equal) { - ziapi::Version a{1, 0}; - ziapi::Version b{1, 0}; + ziapi::Version a{1, 0, 0}; + ziapi::Version b{1, 0, 0}; ASSERT_TRUE(a <= b); - a = ziapi::Version{1, 1}; - b = ziapi::Version{3, 1}; + a = ziapi::Version{1, 1, 2}; + b = ziapi::Version{3, 1, 0}; ASSERT_TRUE(a <= b); - a = ziapi::Version{2, 4}; - b = ziapi::Version{1, 7}; + a = ziapi::Version{2, 4, 42}; + b = ziapi::Version{1, 7, 0}; + ASSERT_FALSE(a < b); ASSERT_FALSE(a <= b); -} \ No newline at end of file +} From dc9d74c64108b4e2cb041779bfcd8717029a7b9d Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 8 Feb 2022 19:44:34 +0100 Subject: [PATCH 05/23] fix: Config documentation --- docs/guides/CONFIG.md | 73 +++++++++++++++++++++++++++------------- include/ziapi/Config.hpp | 6 ++-- tests/Config.cpp | 8 ++--- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/docs/guides/CONFIG.md b/docs/guides/CONFIG.md index fb0a9ab..f951f84 100644 --- a/docs/guides/CONFIG.md +++ b/docs/guides/CONFIG.md @@ -45,20 +45,31 @@ using Node = ziapi::config::Node; Node obj( { - {"sources", new Node({ - new Node("/bin/ls"), - new Node(10), - new Node(nullptr), - new Node(10.5) + {"sources", std::make_shared({ + std::make_shared("/bin/ls"), + std::make_shared(10), + std::make_shared(nullptr), + std::make_shared(10.5) }) }, - {"modules", new Node({ - {"directoryListing", new Node({ - {"root", new Node("/var/www")} + {"modules", std::make_shared({ + {"directoryListing", std::make_shared({ + {"root", std::make_shared("/var/www")} })} })} } ); +// or + +auto obj = Node::MakeDict({ + {"sources", Node::MakeArray({ "/bin/ls", 10, nullptr, 10.5 })}, + {"modules", Node::MakeDict({ + {"directoryListing", Node::MakeDict({ + {"root", "/var/www"} + })} + })} +}) + ``` # ## Data Types @@ -72,7 +83,7 @@ example of a json containing an `undefined` ``` transcription with our `Config.hpp` ```cpp -new ziapi::config::Node(ziapi::config::Undefined{}) +ziapi::config::Node(ziapi::config::Undefined{}) ``` @@ -85,7 +96,7 @@ null ``` transcription with our `Config.hpp` ```cpp -new ziapi::config::Node(nullptr) +ziapi::config::Node(nullptr) ``` ### bool @@ -97,7 +108,7 @@ true ``` transcription with our `Config.hpp` ```cpp -new ziapi::config::Node(true) +ziapi::config::Node(true) ``` @@ -110,7 +121,7 @@ example of a json containing an `int` ``` transcription with our `Config.hpp` ```cpp -new ziapi::config::Node(5) +ziapi::config::Node(5) ``` @@ -124,7 +135,7 @@ example of a json containing a `double` transcription with our `Config.hpp` ```cpp -new ziapi::config::Node(4.2) +ziapi::config::Node(4.2) ``` @@ -137,7 +148,7 @@ example of a json containing an `String` ``` transcription with our `Config.hpp` ```cpp -new ziapi::config::Node("Hello World!") +ziapi::config::Node("Hello World!") ``` @@ -153,11 +164,15 @@ transcription with our `Config.hpp` ```cpp using Node = ziapi::config::Node; -new Node({ - new Node("Hello"), - new Node("World"), - new Node("!") -}) +Node({ + std::make_shared("Hello"), + std::make_shared("World"), + std::make_shared("!") +}); + +// or + +Node::MakeArray({ "Hello", "World", "!" }); ```n ### Dict @@ -177,11 +192,21 @@ transcription with our `Config.hpp` ```cpp using Node = ziapi::config::Node; -new Node({ - {"age", new Node(19)}, - {"first_name", new Node("Charlie")}, - {"last_name", new Node("Chou")}, - {"is_sexy", new Node(true)} +Node({ + {"age", std::make_shared(19)}, + {"first_name", std::make_shared("Charlie")}, + {"last_name", std::make_shared("Chou")}, + {"is_sexy", std::make_shared(true)} }) + +// or + +Node::MakeDict({ + {"age", 19}, + {"first_name", "Charlie"}, + {"last_name", "Chou"}, + {"is_sexy", true}, +}) + ``` diff --git a/include/ziapi/Config.hpp b/include/ziapi/Config.hpp index d92af70..f7fcc47 100644 --- a/include/ziapi/Config.hpp +++ b/include/ziapi/Config.hpp @@ -40,7 +40,8 @@ struct Node : public NodeVariant { public: using NodeVariant::NodeVariant; - static Node FromArray(const std::initializer_list &values) + /// Simpler way to construct a node from a Array, it instantiate std::shared_ptr by itself + static Node MakeArray(const std::initializer_list &values) { Array arr; @@ -50,7 +51,8 @@ struct Node : public NodeVariant { return arr; } - static Node FromDict(const std::initializer_list> &values) + /// Simpler way to construct a node from a Dict, it instantiate std::shared_ptr by itself + static Node MakeDict(const std::initializer_list> &values) { Dict dict; diff --git a/tests/Config.cpp b/tests/Config.cpp index 10c4c97..e550f0f 100644 --- a/tests/Config.cpp +++ b/tests/Config.cpp @@ -37,7 +37,7 @@ TEST(Config, SimpleBool) TEST(Config, SimpleArray) { - auto array = Node::FromArray({10, "Hello world", 14.5f}); + auto array = Node::MakeArray({10, "Hello world", 14.5f}); ASSERT_EQ(array.AsArray()[0]->AsInt(), 10); ASSERT_EQ(array.AsArray()[1]->AsString(), "Hello world"); @@ -47,15 +47,15 @@ TEST(Config, SimpleArray) TEST(Config, SimpleDict) { Node modules_count(10); - auto dict = Node::FromDict({{"modules_count", modules_count}}); + auto dict = Node::MakeDict({{"modules_count", modules_count}}); ASSERT_EQ(dict.AsDict()["modules_count"]->AsInt(), 10); } TEST(Config, NestedAccess) { - auto cfg = Node::FromDict({ - {"modules", Node::FromDict({{"directoryListing", Node::FromDict({{"root", "/var/www"}})}})}, + auto cfg = Node::MakeDict({ + {"modules", Node::MakeDict({{"directoryListing", Node::MakeDict({{"root", "/var/www"}})}})}, }); ASSERT_EQ(cfg["modules"]["directoryListing"]["root"].AsString(), "/var/www"); From ddeb50ac38a17d815b21ab8331cd8c7d82c72879 Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 8 Feb 2022 19:46:14 +0100 Subject: [PATCH 06/23] fix: remove debug printing in unit tests --- tests/Version.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Version.cpp b/tests/Version.cpp index 1075297..8efc752 100644 --- a/tests/Version.cpp +++ b/tests/Version.cpp @@ -43,19 +43,16 @@ TEST(Version, greater) ziapi::Version a{1, 0, 3}; ziapi::Version b{1, 0, 3}; - std::cout << (a > b) << std::endl; ASSERT_FALSE(a > b); a = ziapi::Version{1, 1, 2}; b = ziapi::Version{1, 1, 0}; - std::cout << (a > b) << std::endl; ASSERT_TRUE(a > b); a = ziapi::Version{2, 0, 2}; b = ziapi::Version{3, 1, 2}; - std::cout << (a > b) << std::endl; ASSERT_FALSE(a > b); } From 542c40f17c091cfd2228fbde4abbb7c7d73f16f6 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 21:52:02 +0100 Subject: [PATCH 07/23] feat: standards documentation --- docs/general/STANDARDS.md | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 docs/general/STANDARDS.md diff --git a/docs/general/STANDARDS.md b/docs/general/STANDARDS.md new file mode 100644 index 0000000..74661f2 --- /dev/null +++ b/docs/general/STANDARDS.md @@ -0,0 +1,55 @@ +# ZIAPI Standards + +A set of rules that define a standard implementation of a ZIAPI module to make it easier to share modules between groups. + +The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC 2119](https://datatracker.ietf.org/doc/html/rfc2119). + +## C1 - Module Configuration Documentation + +ZIAPI doesn't impose a norm for configuration files. However modules **MUST** be shipped with sufficient documentation regarding the configuration fields they expect. + +#### Example + +If the module `tls` depends on the following configuration: + +```c++ +void Init(const ziapi::config::Node &cfg) override { + auto enable_tls = cfg["modules"]["tls"]["enableTls"].AsBool(); + auto certificate_path = cfg["modules"]["tls"]["certficiatePath"].AsString(); +} +``` + +Then the `tls` module's documentation should explicitly state that the module expects: +- `modules.tls.enableTls` to be defined as a boolean +- `modules.tls.certificatePath` to be defined as a string + +## C2 - Configuration file layout + +To limit friction when sharing modules, all groups **MAY** implement the following configuration file layout where: +- The top level node is a `ziapi::config::Dict` +- It contains a `modules` key which is a `ziapi::config::Dict` too. +- Each module which requires a configuration fetches its configuration information from `modules.*` + +#### Example + +To illustrate this, let's look at a YAML configuration file example. + +Let's say we have two modules respectively named `tls` and `directoryListing`. By applying rule C2 we get the following configuration. + +```yaml +modules: + tls: + foo: bar + directoryListing: + foo: bar +``` + +Therefore it is **NOT RECOMMENDED** to depend on configuration fields outside of the `modules.` scope like so: + +```c++ +void Init(const ziapi::config::Node &cfg) override { + /// Not C2 compliant, the module depends on a node outside of "modules.". + auto enable_tls = cfg["enableTls"].AsBool(); + auto certificate_path = cfg["certficiatePath"].AsString(); +} +``` From adc4a9ad48fa36b620cea943656529d5b8114b4f Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 21:53:15 +0100 Subject: [PATCH 08/23] feat: links to new documentation files in main doc readme --- docs/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/README.md b/docs/README.md index 8117c1c..4fe5c82 100644 --- a/docs/README.md +++ b/docs/README.md @@ -11,6 +11,7 @@ General documentation pages will help you understand the general concepts of the - [Getting started](general/GETTING_STARTED.md) - [Why choose ZIAPI](general/WHY_US.md) - [Modules](general/MODULES.md) +- [ZIAPI Standards](general/STANDARDS.md) - [Contributing](general/CONTRIBUTING.md) ## Guides @@ -19,6 +20,7 @@ Guides are step by step instructions on how to do pretty much anything with the - [Install and build](guides/INSTALL_AND_BUILD.md) - [Module 101](guides/MODULES_101.md) +- [The config object](guides/CONFIG.md) - [Implementing a handler module](guides/IMPLEMENT_HANDLER.md) - [Implementing a pre-processor module](guides/IMPLEMENT_PREPROCESSOR.md) - [Implementing a post-processor module](guides/IMPLEMENT_POSTPROCESSOR.md) @@ -30,3 +32,4 @@ Module examples are code tutorials on how to build a variety of modules. - [Logger Module Tutorial](examples/LOGGER.md) - [Directory Listing Module Tutorial](examples/DIRECTORY_LISTING.md) +- [Redirection Module Tutorial](examples/REDIRECTION.md) From b653c1b314a1e5e8c91409b431a94e3a74620769 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:02:03 +0100 Subject: [PATCH 09/23] update: simplified github issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 9 +++----- .github/ISSUE_TEMPLATE/feature_request.md | 28 ++++------------------- 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 07ee370..c0d610f 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -1,14 +1,11 @@ --- name: Bug report about: Create a report to help us improve -title: '' -labels: '' -assignees: '' +title: 'My Bug Report' +labels: 'bug' --- -## [BUG] [Subject of the issue] - ### Description Describe your issue in as much detail as possible here. @@ -38,4 +35,4 @@ Please paste any logs here that demonstrate the issue, if they exist ### Proposed solution -If you have an idea of how to fix this issue, please write it down here, so we can begin discussing it \ No newline at end of file +If you have an idea of how to fix this issue, please write it down here, so we can begin discussing it diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 973c340..547390e 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,23 +1,15 @@ --- -name: Feature report +name: Feature request about: Suggest an idea for this project -title: '' -labels: '' -assignees: '' +title: 'My Feature' +labels: 'enhancement' --- -## [FEATURES] [Subject of the issue] - ### Description Describe your issue in as much detail as possible here. -### Your environment - -* OS and version -* branch that causes this issue - ### Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] @@ -26,18 +18,6 @@ A clear and concise description of what the problem is. Ex. I'm always frustrate A clear and concise description of what you want to happen. -### Expected behaviour - -Tell us what should happen - -### Actual behaviour - -Tell us what happens instead - -### Logs - -Please paste any logs here that demonstrate the issue, if they exist - ### Additional context -Add any other context or screenshots about the feature request here. \ No newline at end of file +Add any other context or screenshots about the feature request here. From 0844409eee3e570311dd36515e7a814c1a14549c Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:02:55 +0100 Subject: [PATCH 10/23] feat: missing newline in file --- .github/pull_request_template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 0b78643..bc2503a 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -21,4 +21,4 @@ Please complete this section if any breaking changes have been made, otherwise d # Additional comments -Please post additional comments in this section if you have them, otherwise delete it. \ No newline at end of file +Please post additional comments in this section if you have them, otherwise delete it. From 645e4808be4564bc8b8c57031b13754362a2daee Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:06:20 +0100 Subject: [PATCH 11/23] docs: update api version --- README.md | 4 ++-- docs/guides/INSTALL_AND_BUILD.md | 2 +- docs/guides/MODULES_101.md | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 505aa34..60819c0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![Banner](docs/assets/project_banner.png) -[![ZiAPI](https://img.shields.io/badge/ZiAPI-v2.1.0-blue.svg)](https://github.com/martin-olivier/ZiAPI/releases/tag/v2.1.0) +[![ZiAPI](https://img.shields.io/badge/ZiAPI-v3.0.0-blue.svg)](https://github.com/martin-olivier/ZiAPI/releases/tag/v3.0.0) [![CPP Version](https://img.shields.io/badge/C++-17_and_above-darkgreen.svg)](https://isocpp.org/) [![Discord](https://img.shields.io/discord/934852777136513075)](https://discord.gg/ztptguX2sE) [![workflow](https://github.com/martin-olivier/ZiAPI/actions/workflows/CI.yml/badge.svg)](https://github.com/martin-olivier/ZiAPI/actions/workflows/CI.yml) @@ -38,7 +38,7 @@ include(ExternalProject) ExternalProject_Add( ziapi GIT_REPOSITORY https://github.com/martin-olivier/ZiAPI.git - GIT_TAG v2.1.0 + GIT_TAG v3.0.0 INSTALL_COMMAND "" TEST_COMMAND "" ) diff --git a/docs/guides/INSTALL_AND_BUILD.md b/docs/guides/INSTALL_AND_BUILD.md index 8d424a4..90e8329 100644 --- a/docs/guides/INSTALL_AND_BUILD.md +++ b/docs/guides/INSTALL_AND_BUILD.md @@ -11,7 +11,7 @@ include(ExternalProject) ExternalProject_Add( ziapi GIT_REPOSITORY https://github.com/martin-olivier/ZiAPI.git - GIT_TAG v2.1.0 + GIT_TAG v3.0.0 INSTALL_COMMAND "" TEST_COMMAND "" ) diff --git a/docs/guides/MODULES_101.md b/docs/guides/MODULES_101.md index 7afecc6..25d0d79 100644 --- a/docs/guides/MODULES_101.md +++ b/docs/guides/MODULES_101.md @@ -42,7 +42,7 @@ include(ExternalProject) ExternalProject_Add( ziapi GIT_REPOSITORY https://github.com/martin-olivier/ZiAPI.git - GIT_TAG v2.1.0 + GIT_TAG v3.0.0 INSTALL_COMMAND "" TEST_COMMAND "" ) From 4cf857744a8d63385e42202af09259883bd6c0ad Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:11:56 +0100 Subject: [PATCH 12/23] update(http)!: http versions now use semantic versioning --- include/ziapi/Http.hpp | 22 ++++++++++++++++++++++ include/ziapi/Version.hpp | 12 +++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/include/ziapi/Http.hpp b/include/ziapi/Http.hpp index 6485289..19909a9 100644 --- a/include/ziapi/Http.hpp +++ b/include/ziapi/Http.hpp @@ -10,6 +10,28 @@ namespace ziapi::http { +struct Version { + Version(int major, int minor) : major(major), minor(minor) {} + + inline bool operator==(const Version &other) const noexcept { return major == other.major && minor == other.minor; } + + inline bool operator!=(const Version &other) const noexcept { return !(*this == other); } + + inline bool operator>(const Version &other) const noexcept { return (*this >= other) && (*this != other); } + + inline bool operator<(const Version &other) const noexcept { return !(*this > other) && other != *this; } + + inline bool operator>=(const Version &other) const noexcept + { + return (major == other.major ? minor == other.minor ? true : minor > other.minor : major > other.major); + } + + inline bool operator<=(const Version &other) const noexcept { return (*this < other) || (*this == other); } + + unsigned int major; + unsigned int minor; +}; + /** * Struct that represents an HTTP request message */ diff --git a/include/ziapi/Version.hpp b/include/ziapi/Version.hpp index d2ff460..9c8a4ba 100644 --- a/include/ziapi/Version.hpp +++ b/include/ziapi/Version.hpp @@ -21,15 +21,9 @@ struct Version { inline bool operator>=(const Version &other) const noexcept { - return ( - major == other.major - ? minor == other.minor - ? patch == other.patch - ? true - : patch > other.patch - : minor > other.minor - : major > other.major - ); + return (major == other.major + ? minor == other.minor ? patch == other.patch ? true : patch > other.patch : minor > other.minor + : major > other.major); } inline bool operator<=(const Version &other) const noexcept { return (*this < other) || (*this == other); } From 88d962be1a024b2eb4bff7f842200a09da9f992b Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:17:23 +0100 Subject: [PATCH 13/23] update(examples): switched to new config dict access --- docs/examples/DIRECTORY_LISTING.md | 2 +- docs/examples/REDIRECTION.md | 2 +- examples/modules/directory-listing/DirectoryListingModule.hpp | 2 +- examples/modules/redirection/Module.hpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/examples/DIRECTORY_LISTING.md b/docs/examples/DIRECTORY_LISTING.md index b5618f7..2e80fdb 100644 --- a/docs/examples/DIRECTORY_LISTING.md +++ b/docs/examples/DIRECTORY_LISTING.md @@ -60,7 +60,7 @@ Well, we can add the path to this directory as a variable of our config file and void Init(const ziapi::config::Node &cfg) override { /// In our config, we can specify which folder our module serves. - root_ = cfg.AsDict()["modules"]->AsDict()["directoryListing"]->AsDict()["root"]->AsString(); + root_ = cfg["modules"]["directoryListing"]["root"].AsString(); } ... diff --git a/docs/examples/REDIRECTION.md b/docs/examples/REDIRECTION.md index e0ec1ea..c07c08f 100644 --- a/docs/examples/REDIRECTION.md +++ b/docs/examples/REDIRECTION.md @@ -55,7 +55,7 @@ Let's load from the config the route to which we will redirect requests. We'll s void Init(const Config &cfg) { /// We'll load from the configuration where to redirect to! - redirection_route_ = cfg.AsDict()["modules"]->AsDict()["redirection"]->AsDict()["route"]->AsString(); + redirection_route_ = cfg["modules"]["redirection"]["route"]; } ... diff --git a/examples/modules/directory-listing/DirectoryListingModule.hpp b/examples/modules/directory-listing/DirectoryListingModule.hpp index a996ff1..aedf762 100644 --- a/examples/modules/directory-listing/DirectoryListingModule.hpp +++ b/examples/modules/directory-listing/DirectoryListingModule.hpp @@ -11,7 +11,7 @@ class DirectoryListingModule : public ziapi::IHandlerModule { /// In our config, we can specify which folder our module serves. /// We fetch the "modules.directoryListing.root" variable from the config /// as a string. - root_ = cfg.AsDict()["modules"]->AsDict()["directoryListing"]->AsDict()["root"]->AsString(); + root_ = cfg["modules"]["directoryListing"]["root"].AsString(); } [[nodiscard]] ziapi::Version GetVersion() const noexcept override { return {3, 0, 0}; } diff --git a/examples/modules/redirection/Module.hpp b/examples/modules/redirection/Module.hpp index 2a4ac8f..d1385f3 100644 --- a/examples/modules/redirection/Module.hpp +++ b/examples/modules/redirection/Module.hpp @@ -11,7 +11,7 @@ class RedirectionModule : public ziapi::IHandlerModule { void Init(const ziapi::config::Node &cfg) override { /// We'll load from the configuration where to redirect to! - redirection_route_ = cfg.AsDict()["modules"]->AsDict()["redirection"]->AsDict()["route"]->AsString(); + redirection_route_ = cfg["modules"]["redirection"]["route"]; } void Handle(ziapi::http::Context &ctx, const ziapi::http::Request &req, ziapi::http::Response &res) override From 61377e6a7a01f32caa00020bbcab52247fd8693b Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:22:30 +0100 Subject: [PATCH 14/23] cut(http)!: http version class in place of int --- include/ziapi/Http.hpp | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/include/ziapi/Http.hpp b/include/ziapi/Http.hpp index 19909a9..c563469 100644 --- a/include/ziapi/Http.hpp +++ b/include/ziapi/Http.hpp @@ -10,34 +10,12 @@ namespace ziapi::http { -struct Version { - Version(int major, int minor) : major(major), minor(minor) {} - - inline bool operator==(const Version &other) const noexcept { return major == other.major && minor == other.minor; } - - inline bool operator!=(const Version &other) const noexcept { return !(*this == other); } - - inline bool operator>(const Version &other) const noexcept { return (*this >= other) && (*this != other); } - - inline bool operator<(const Version &other) const noexcept { return !(*this > other) && other != *this; } - - inline bool operator>=(const Version &other) const noexcept - { - return (major == other.major ? minor == other.minor ? true : minor > other.minor : major > other.major); - } - - inline bool operator<=(const Version &other) const noexcept { return (*this < other) || (*this == other); } - - unsigned int major; - unsigned int minor; -}; - /** * Struct that represents an HTTP request message */ struct Request { - /// For possible values of version checkout ziapi::http::version. - Version version; + /// For possible values of version checkout ziapi::http::Version. + unsigned int version; /// For possible values of method checkout ziapi::http::method. std::string method; @@ -53,8 +31,8 @@ struct Request { * Struct that represents an HTTP response message */ struct Response { - /// For possible values of version checkout ziapi::http::version. - Version version; + /// For possible values of version checkout ziapi::http::Version. + unsigned int version; /// For possible values of version checkout ziapi::http::code. Code status_code; From ecc83f255339feb6ddd938d7c2b9731f7820dc09 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 22:25:45 +0100 Subject: [PATCH 15/23] update(http): revert back to version as enum --- include/ziapi/Http.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ziapi/Http.hpp b/include/ziapi/Http.hpp index c563469..ed4b7b3 100644 --- a/include/ziapi/Http.hpp +++ b/include/ziapi/Http.hpp @@ -15,7 +15,7 @@ namespace ziapi::http { */ struct Request { /// For possible values of version checkout ziapi::http::Version. - unsigned int version; + Version version; /// For possible values of method checkout ziapi::http::method. std::string method; @@ -32,7 +32,7 @@ struct Request { */ struct Response { /// For possible values of version checkout ziapi::http::Version. - unsigned int version; + Version version; /// For possible values of version checkout ziapi::http::code. Code status_code; From 02c4cb0c5699d9344b16ae2e037a3a1291fbfc65 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 23:13:50 +0100 Subject: [PATCH 16/23] docs: update ziapi standards --- docs/general/STANDARDS.md | 47 +++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/docs/general/STANDARDS.md b/docs/general/STANDARDS.md index 74661f2..67cc54f 100644 --- a/docs/general/STANDARDS.md +++ b/docs/general/STANDARDS.md @@ -4,7 +4,11 @@ A set of rules that define a standard implementation of a ZIAPI module to make i The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC 2119](https://datatracker.ietf.org/doc/html/rfc2119). -## C1 - Module Configuration Documentation +## `Z0` - Module Documentation + +Each module **MUST** be shipped with a `README.md` providing basic documentation for the module. + +## `Z1` - Module Configuration Documentation ZIAPI doesn't impose a norm for configuration files. However modules **MUST** be shipped with sufficient documentation regarding the configuration fields they expect. @@ -23,7 +27,7 @@ Then the `tls` module's documentation should explicitly state that the module ex - `modules.tls.enableTls` to be defined as a boolean - `modules.tls.certificatePath` to be defined as a string -## C2 - Configuration file layout +## `Z2` - Configuration File Layout To limit friction when sharing modules, all groups **MAY** implement the following configuration file layout where: - The top level node is a `ziapi::config::Dict` @@ -34,7 +38,7 @@ To limit friction when sharing modules, all groups **MAY** implement the followi To illustrate this, let's look at a YAML configuration file example. -Let's say we have two modules respectively named `tls` and `directoryListing`. By applying rule C2 we get the following configuration. +Let's say we have two modules respectively named `tls` and `directoryListing`. By applying rule Z2 we get the following configuration. ```yaml modules: @@ -47,9 +51,44 @@ modules: Therefore it is **NOT RECOMMENDED** to depend on configuration fields outside of the `modules.` scope like so: ```c++ -void Init(const ziapi::config::Node &cfg) override { +void MyModule::Init(const ziapi::config::Node &cfg) override { /// Not C2 compliant, the module depends on a node outside of "modules.". auto enable_tls = cfg["enableTls"].AsBool(); auto certificate_path = cfg["certficiatePath"].AsString(); } ``` + +## `Z3` - Request/Response Context Documentation + +ZIAPI doesn't impose a norm for the request/response context. However, modules **MUST** be shipped with sufficient documentation regarding the context fields they write to and read from. + +Modules **SHOULD** try to populate the context with primitive types only or basic `std` types (Like `std::string`). + +#### Example + +If the `MyPreProcessor` module depends on the following context: + +```c++ +void MyPreProcessor::PreProcess(http::Context &ctx, http::Request &req) +{ + auto client_address = std::any_cast(ctx["client.socket.address"]); + ctx["php_cgi.user.is_authenticated"] = (client_address == "127.0.0.1"); +} +``` + +Then the `MyPreProcessor` module's documentation must explicitly state that it expects: +- `ctx["client.socket.address"]` to be defined as a `std::string` + +The documentation must also state that the module plans to: +- Write a boolean value inside `ctx["user.is_authenticated"]` + +## `Z4` - Standard Request/Response Context Fields + +Each request/response is associated with a `ziapi::http::Context` to allow modules to communicate data. + +The following fields are standard fields and **SHOULD** be populated according to the following specification. + +| Key | Type | Description | +|-------------------------|-----------------|----------------------------------------------------------------------------| +| `client.socket.address` | `std::string` | The IP address of the client. May only be mutated by `INetworkModule`s | +| `client.socket.port` | `std::uint16_t` | The port of the client's socket. May only be mutated by `INetworkModule`s | From b5a8fba73cb33a185c0882c208cd692b6a789446 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 23:20:50 +0100 Subject: [PATCH 17/23] fix(http): warning due to conflicting names closes #21 --- include/ziapi/Http.hpp | 8 ++++---- include/ziapi/Version.hpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/ziapi/Http.hpp b/include/ziapi/Http.hpp index ed4b7b3..b292319 100644 --- a/include/ziapi/Http.hpp +++ b/include/ziapi/Http.hpp @@ -44,11 +44,11 @@ struct Response { std::string body; - void Bootstrap(Code status_code = Code::kOK, std::string reason = reason::kOK, Version version = Version::kV1_1) + void Bootstrap(Code status_code_ = Code::kOK, std::string reason_ = reason::kOK, Version version_ = Version::kV1_1) { - this->status_code = status_code; - this->reason = reason; - this->version = version; + status_code = status_code_; + reason = reason_; + version = version_; } }; diff --git a/include/ziapi/Version.hpp b/include/ziapi/Version.hpp index 9c8a4ba..0a2fe04 100644 --- a/include/ziapi/Version.hpp +++ b/include/ziapi/Version.hpp @@ -6,7 +6,7 @@ namespace ziapi { * Semantic versioning structure with a major and a minor version */ struct Version { - Version(int major, int minor, int patch) : major(major), minor(minor), patch(patch) {} + Version(int major_, int minor_, int patch_) : major(major_), minor(minor_), patch(patch_) {} inline bool operator==(const Version &other) const noexcept { From 833997c7234d11ba02621558d0528d604a7ee747 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 23:27:56 +0100 Subject: [PATCH 18/23] update(github): pull request template --- .github/pull_request_template.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index bc2503a..4ea8f90 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -2,22 +2,18 @@ Please provide a detailed description of what was done in this PR. -# Changes include - -- [ ] Bugfix (non-breaking change that solves an issue) -- [ ] New feature (non-breaking change that adds functionality) -- [ ] Breaking change (change that is not backwards-compatible and/or changes current functionality) - # Breaking changes Please complete this section if any breaking changes have been made, otherwise delete it. +# Additional Changes + # Checklist - [ ] I have assigned this PR to myself - [ ] I have added at least 1 reviewer - [ ] I have tested this code -- [ ] I have added sufficient documentation in the code +- [ ] I have added sufficient documentation and/or updated documentation to reflect my changes # Additional comments From 3fe42d864f8c1a6a2c7a6dc19bae9bf1e390fb73 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Tue, 8 Feb 2022 23:54:45 +0100 Subject: [PATCH 19/23] update(config)!: drop old config constructors --- docs/guides/CONFIG.md | 123 +++++++++++++++++++++------------------ include/ziapi/Config.hpp | 6 -- tests/Config.cpp | 2 +- 3 files changed, 66 insertions(+), 65 deletions(-) diff --git a/docs/guides/CONFIG.md b/docs/guides/CONFIG.md index f951f84..195bf9a 100644 --- a/docs/guides/CONFIG.md +++ b/docs/guides/CONFIG.md @@ -2,9 +2,9 @@ In this guide we will learn how to use our configuration, to setup all of your modules easily. -## `Node` class +## The `Node` object -First, let's take a look at the `Node` definition +First, let's take a look at the definition of `ziapi::config::Node`. ```cpp using NodeVariant = std::variant; @@ -17,7 +17,8 @@ The `std::variant` declines all the possible data types available in JSON, YAML, Let's look at a simple example: -Imagine you have something like +Imagine you have a configuration file of the following form: + ```json { "sources": ["/bin/ls", 10, null, 10.5], @@ -28,7 +29,9 @@ Imagine you have something like } } ``` -or + +Or its YAML equivalent + ```yaml sources: - "/bin/ls" @@ -39,7 +42,9 @@ modules: directoryListing: root: /var/www ``` -You could generate it like so + +You could represent it using a `ziapi::config::Node` like so + ```cpp using Node = ziapi::config::Node; @@ -59,7 +64,8 @@ Node obj( })} } ); -// or + +// Or using helper static methods auto obj = Node::MakeDict({ {"sources", Node::MakeArray({ "/bin/ls", 10, nullptr, 10.5 })}, @@ -71,113 +77,115 @@ auto obj = Node::MakeDict({ }) ``` -# -## Data Types -### Undefined +## Data Types -example of a json containing an `undefined` +### `Undefined` -```json +The `Undefined` data type represents an empty configuration, much like an empty file. -``` -transcription with our `Config.hpp` ```cpp ziapi::config::Node(ziapi::config::Undefined{}) ``` +### `Null` -### Null - -example of a json containing a `null` +The `Null` data type represents a boolean value. So the following JSON: ```json null ``` -transcription with our `Config.hpp` + +Would translate to + ```cpp ziapi::config::Node(nullptr) ``` -### bool +### `bool` -example of a json containing a `bool` +The `bool` data type represents a boolean value. So the following JSON: ```json true ``` -transcription with our `Config.hpp` + +Would translate to + ```cpp ziapi::config::Node(true) ``` +### `int` -### int - -example of a json containing an `int` +The `int` data type represents an integer value. So the following JSON: ```json 5 ``` -transcription with our `Config.hpp` + +Would translate to + ```cpp ziapi::config::Node(5) ``` +### `double` -### double - -example of a json containing a `double` +The `double` data type represents a floating point value. So the following JSON: ```json 4.2 ``` -transcription with our `Config.hpp` +Would translate to + ```cpp ziapi::config::Node(4.2) ``` +### `String` -### String - -example of a json containing an `String` +The `String` data type represents a string value. So the following JSON: ```json "Hello World!" ``` -transcription with our `Config.hpp` + +Would translate to + ```cpp ziapi::config::Node("Hello World!") ``` -### Array +### `Array` -example of a json containing an `Array` +The `Array` data type represents an array of values. So the following JSON: ```json ["Hello", "World", "!"] ``` -transcription with our `Config.hpp` -```cpp -using Node = ziapi::config::Node; +Would translate to -Node({ - std::make_shared("Hello"), - std::make_shared("World"), - std::make_shared("!") +```cpp +ziapi::config::Node({ + std::make_shared("Hello"), + std::make_shared("World"), + std::make_shared("!") }); +``` -// or +Which is equivalent to -Node::MakeArray({ "Hello", "World", "!" }); -```n +```c++ +ziapi::config::Node::MakeArray({ "Hello", "World", "!" }); +``` -### Dict +### `Dict` -example of a json containing a `Dict` +The `Dict` data type represents a dictionary of values. So the following JSON: ```json { @@ -188,25 +196,24 @@ example of a json containing a `Dict` } ``` -transcription with our `Config.hpp` -```cpp -using Node = ziapi::config::Node; +Would translate to -Node({ - {"age", std::make_shared(19)}, - {"first_name", std::make_shared("Charlie")}, - {"last_name", std::make_shared("Chou")}, - {"is_sexy", std::make_shared(true)} +```cpp +ziapi::config::Node({ + {"age", std::make_shared(19)}, + {"first_name", std::make_shared("Charlie")}, + {"last_name", std::make_shared("Chou")}, + {"is_sexy", std::make_shared(true)} }) +``` -// or +Which is equivalent to -Node::MakeDict({ +```c++ +ziapi::config::Node::MakeDict({ {"age", 19}, {"first_name", "Charlie"}, {"last_name", "Chou"}, {"is_sexy", true}, }) - ``` - diff --git a/include/ziapi/Config.hpp b/include/ziapi/Config.hpp index f7fcc47..b5c6f41 100644 --- a/include/ziapi/Config.hpp +++ b/include/ziapi/Config.hpp @@ -62,12 +62,6 @@ struct Node : public NodeVariant { return dict; } - /// Used to construct a Node from a Dict - Node(const std::initializer_list &values) : NodeVariant(Dict(values)){}; - - /// Used to construct a Node from an Array - Node(const std::initializer_list &values) : NodeVariant(std::vector(values)){}; - /// Used to construct a Node from a string Node(const char *str) : NodeVariant(std::string(str)){}; diff --git a/tests/Config.cpp b/tests/Config.cpp index e550f0f..71a731a 100644 --- a/tests/Config.cpp +++ b/tests/Config.cpp @@ -16,7 +16,7 @@ TEST(Config, SimpleInt) TEST(Config, SimpleString) { - Node node(String("Hello world")); + Node node("Hello world"); ASSERT_EQ(node.AsString(), "Hello world"); } From 7e184a8967766926dd7cda1d4a39cd4af7a1dbbe Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Wed, 9 Feb 2022 00:01:08 +0100 Subject: [PATCH 20/23] tests(config): add shorthand notations --- tests/Config.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Config.cpp b/tests/Config.cpp index 71a731a..c6647a4 100644 --- a/tests/Config.cpp +++ b/tests/Config.cpp @@ -39,9 +39,9 @@ TEST(Config, SimpleArray) { auto array = Node::MakeArray({10, "Hello world", 14.5f}); - ASSERT_EQ(array.AsArray()[0]->AsInt(), 10); - ASSERT_EQ(array.AsArray()[1]->AsString(), "Hello world"); - ASSERT_EQ(array.AsArray()[2]->AsDouble(), 14.5f); + ASSERT_EQ(array[(std::size_t)0].AsInt(), 10); + ASSERT_EQ(array[1].AsString(), "Hello world"); + ASSERT_EQ(array[2].AsDouble(), 14.5f); } TEST(Config, SimpleDict) @@ -49,7 +49,7 @@ TEST(Config, SimpleDict) Node modules_count(10); auto dict = Node::MakeDict({{"modules_count", modules_count}}); - ASSERT_EQ(dict.AsDict()["modules_count"]->AsInt(), 10); + ASSERT_EQ(dict["modules_count"].AsInt(), 10); } TEST(Config, NestedAccess) From a8e048081b846833846da99e5c3a11146c89c401 Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Wed, 9 Feb 2022 00:02:12 +0100 Subject: [PATCH 21/23] tests(config): use shorthand in hope to fix windows CI --- tests/Config.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Config.cpp b/tests/Config.cpp index c6647a4..8e98f1f 100644 --- a/tests/Config.cpp +++ b/tests/Config.cpp @@ -46,8 +46,7 @@ TEST(Config, SimpleArray) TEST(Config, SimpleDict) { - Node modules_count(10); - auto dict = Node::MakeDict({{"modules_count", modules_count}}); + auto dict = Node::MakeDict({{"modules_count", 10}}); ASSERT_EQ(dict["modules_count"].AsInt(), 10); } From e8fae9725ccdb61aed997759a053144b032909ff Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Wed, 9 Feb 2022 00:12:12 +0100 Subject: [PATCH 22/23] update!: network module run must be blocking --- include/ziapi/Module.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/ziapi/Module.hpp b/include/ziapi/Module.hpp index b7c955d..b2b3139 100644 --- a/include/ziapi/Module.hpp +++ b/include/ziapi/Module.hpp @@ -105,9 +105,10 @@ class INetworkModule : public IModule { public: virtual ~INetworkModule() = default; /** - * Run starts the module providing it with an output queue in which it + * Runs the module providing it with an output queue in which it * shall push incoming requests and an input queue from which it should - * receive incoming responses and send them over the network + * receive incoming responses and send them over the network. + * A call to Run must be blocking till a call to Terminate is issued. */ virtual void Run(http::IRequestOutputQueue &requests, http::IResponseInputQueue &responses) = 0; From 5c158391bc81c56d40b78fae5db7b43f46938e8d Mon Sep 17 00:00:00 2001 From: Diego ROJAS <63368264+rojasdiegopro@users.noreply.github.com> Date: Wed, 9 Feb 2022 00:16:46 +0100 Subject: [PATCH 23/23] fix(readme): extra space in header --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 5d49139..124b872 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ ![Banner](docs/assets/project_banner.png) - [![ZiAPI](https://img.shields.io/badge/ZiAPI-v3.0.0-blue.svg)](https://github.com/martin-olivier/ZiAPI/releases/tag/v3.0.0) [![CPP Version](https://img.shields.io/badge/C++-17_and_above-darkgreen.svg)](https://isocpp.org/) [![Discord](https://img.shields.io/discord/934852777136513075)](https://discord.gg/CzKv6dGXmf)