From 2f3fc70cd8e025e8398dc38de74341b3dab97635 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 29 Oct 2023 22:28:41 +0100 Subject: [PATCH 1/5] change 'Restults' to 'TestingResults' and 'Results' to 'results' --- .gitignore | 5 +++-- src/classes/command.hpp | 3 ++- src/testing/testing.hpp | 36 ++++++++++++++++++------------------ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index d04d428..546c097 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -main.cpp +test.cpp *.exe *.out -.vscode \ No newline at end of file +.vscode +makefile \ No newline at end of file diff --git a/src/classes/command.hpp b/src/classes/command.hpp index 6abae33..2b50cdb 100644 --- a/src/classes/command.hpp +++ b/src/classes/command.hpp @@ -32,7 +32,8 @@ namespace lpstd commandInfo currentCommand = {name : "", description : "", - func : nullptr}; + func : nullptr, + args : {}}; public: Command(/* args */) diff --git a/src/testing/testing.hpp b/src/testing/testing.hpp index 2f42de6..382a4ea 100644 --- a/src/testing/testing.hpp +++ b/src/testing/testing.hpp @@ -13,13 +13,13 @@ namespace lpstd namespace testing { - struct Results + struct Testingresults { int passed = 0; int failed = 0; }; - Results Results; + Testingresults results; void drawResults() { @@ -27,13 +27,13 @@ namespace lpstd needUTF8(); std::cout << std::endl; - if (Results.failed == 0) + if (results.failed == 0) { std::cout << "=== ✅ All tests passed ===" << std::endl; } else { - std::cout << "=== ❌ " << Results.failed << " tests failed ===" << std::endl; + std::cout << "=== ❌ " << results.failed << " tests failed ===" << std::endl; } } @@ -55,12 +55,12 @@ namespace lpstd if (result) { std::cout << "✅ Expected " << this->value << " to be " << expected << std::endl; - Results.passed++; + results.passed++; } else { std::cout << "❌ Expected " << expected << " but got " << this->value << std::endl; - Results.failed++; + results.failed++; } return result; } @@ -71,12 +71,12 @@ namespace lpstd if (result) { std::cout << "✅ Expected " << this->value << " to be greater than " << expected << std::endl; - Results.passed++; + results.passed++; } else { std::cout << "❌ Expected " << this->value << " to be greater than " << expected << std::endl; - Results.failed++; + results.failed++; } return result; @@ -88,12 +88,12 @@ namespace lpstd if (result) { std::cout << "✅ Expected " << this->value << " to be less than " << expected << std::endl; - Results.passed++; + results.passed++; } else { std::cout << "❌ Expected " << this->value << " to be less than " << expected << std::endl; - Results.failed++; + results.failed++; } return result; } @@ -104,12 +104,12 @@ namespace lpstd if (result) { std::cout << "✅ Expected " << this->value << " to be true" << std::endl; - Results.passed++; + results.passed++; } else { std::cout << "❌ Expected " << this->value << " to be true" << std::endl; - Results.failed++; + results.failed++; } return result; } @@ -120,12 +120,12 @@ namespace lpstd if (result) { std::cout << "✅ Expected " << this->value << " to be false" << std::endl; - Results.passed++; + results.passed++; } else { std::cout << "❌ Expected " << this->value << " to be false" << std::endl; - Results.failed++; + results.failed++; } return result; } @@ -136,12 +136,12 @@ namespace lpstd if (result) { std::cout << "✅ Expected " << this->value << " to be close to " << expected << std::endl; - Results.passed++; + results.passed++; } else { std::cout << "❌ Expected " << this->value << " to be close to " << expected << std::endl; - Results.failed++; + results.failed++; } return result; } @@ -185,11 +185,11 @@ namespace lpstd catch (std::exception &e) { std::cout << "✅ Expected exception thrown: " << e.what() << std::endl; - Results.passed++; + results.passed++; return; } std::cout << "❌ Expected exception not thrown" << std::endl; - Results.failed++; + results.failed++; } } From 3847522e8fddcd83962b35b90d383110ba7ead5c Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 29 Oct 2023 23:00:52 +0100 Subject: [PATCH 2/5] add getCommands() | add drawResults in describe function --- src/classes/command.hpp | 5 +++++ src/testing/testing.hpp | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/classes/command.hpp b/src/classes/command.hpp index 2b50cdb..9f472bc 100644 --- a/src/classes/command.hpp +++ b/src/classes/command.hpp @@ -88,6 +88,11 @@ namespace lpstd return values.size() > 0 ? values[0] : ""; } + std::list getCommands() + { + return this->commands; + } + void addCommand(std::string name, std::string description, void (*func)(void)) { commandInfo command; diff --git a/src/testing/testing.hpp b/src/testing/testing.hpp index 382a4ea..eba7207 100644 --- a/src/testing/testing.hpp +++ b/src/testing/testing.hpp @@ -167,6 +167,7 @@ namespace lpstd { std::cout << description << std::endl; testCase(); + std::cout << std::endl; } // Custom describe function to group related test cases @@ -174,6 +175,8 @@ namespace lpstd { std::cout << "Describing " << description << std::endl; testSuite(); + drawResults(); + std::cout << std::endl; } void testThrow(std::function testCase) From d24cca1bd7aea1be159eb24fdc84ff515be8d611 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Fri, 3 Nov 2023 16:15:02 +0100 Subject: [PATCH 3/5] add command aliases --- src/classes/command.hpp | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/classes/command.hpp b/src/classes/command.hpp index 9f472bc..41df298 100644 --- a/src/classes/command.hpp +++ b/src/classes/command.hpp @@ -20,6 +20,7 @@ namespace lpstd std::string name; std::string description; void (*func)(); + std::vector aliases; std::vector args; }; @@ -101,6 +102,18 @@ namespace lpstd command.func = func; this->commands.push_back(command); } + void addCommandAlias(std::string name, std::string alias) + { + for (auto &commandInfo : this->commands) + { + if (commandInfo.name == name) + { + commandInfo.aliases.push_back(alias); + return; + } + } + std::cout << "Command not found" << std::endl; + } // void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) // { @@ -121,7 +134,26 @@ namespace lpstd for (auto &commandInfo : sortedCommands) { - help += commandInfo.name + " - " + commandInfo.description + "\n"; + if (commandInfo.aliases.size() == 0) + { + help += commandInfo.name + " - " + commandInfo.description + "\n"; + continue; + } + std::string aliases = ""; + + for (auto &alias : commandInfo.aliases) + { + if (aliases != "") + { + aliases += ", " + alias; + } + else + { + aliases += alias; + } + } + + help += commandInfo.name + " - " + commandInfo.description + " [ " + aliases + " ]\n"; } return help; @@ -178,6 +210,17 @@ namespace lpstd commandInfo.func(); return; } + + for (auto &alias : commandInfo.aliases) + { + if (alias == command) + { + commandInfo.args = args; + currentCommand = commandInfo; + commandInfo.func(); + return; + } + } } std::cout << "Command not found" << std::endl; } From bcac027fb5655022a16b73c90bd6a6a7b6b3d766 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sat, 4 Nov 2023 00:22:02 +0100 Subject: [PATCH 4/5] add basic logger.hpp --- lpstd.hpp | 2 + src/logging/logger.hpp | 200 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 src/logging/logger.hpp diff --git a/lpstd.hpp b/lpstd.hpp index 4574a26..37d9e9d 100644 --- a/lpstd.hpp +++ b/lpstd.hpp @@ -7,6 +7,8 @@ #include #endif +#include "src/logging/logger.hpp" + #include "src/patterns/singleton.hpp" #include "src/testing/testing.hpp" // #include "src/faker/faker.hpp" diff --git a/src/logging/logger.hpp b/src/logging/logger.hpp new file mode 100644 index 0000000..6e2df4a --- /dev/null +++ b/src/logging/logger.hpp @@ -0,0 +1,200 @@ + +#ifndef LOGGER_HPP +#define LOGGER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include "../patterns/singleton.hpp" +#include "../exceptions/exceptions.hpp" + +namespace lpstd +{ + namespace logging + { + enum LogLevel + { + LOG_DEBUG, + LOG_INFO, + LOG_WARNING, + LOG_ERROR, + LOG_FATAL + }; + + enum Logmode + { + CONSOLE, + FILE, + BOTH + }; + + class Logger : public lpstd::Singleton + { + private: + Logmode mode = Logmode::CONSOLE; + std::map level_filename; + std::map level_file; + std::string filename = "log.txt"; + std::ofstream file; + LogLevel level = LogLevel::LOG_DEBUG; + std::string message = ""; + + std::ofstream getFile() + { + throw lpstd::exceptions::NotImplementedException(); + // return this->getFile(this->level); + } + + // std::ofstream getFile(LogLevel level) + // { + // std::string filename = ""; + + // if (this->level_filename.find(level) == this->level_filename.end()) + // { + // filename = this->level_filename[level]; + // } + // else + // { + // filename = this->filename; + // } + + // return this->level_file[level]; + // } + + void LogToLogmode(std::string message, LogLevel level) + { + switch (this->mode) + { + case Logmode::CONSOLE: + std::cout << message << std::endl; + break; + case Logmode::FILE: + this->file << message << std::endl; + break; + case Logmode::BOTH: + std::cout << message << std::endl; + this->file << message << std::endl; + break; + default: + break; + } + } + + void initFile() + { + this->file.open(this->filename, std::ios::app); + } + + public: + Logger() + { + } + + ~Logger() + { + this->file.close(); + } + + void LevelToFile(LogLevel level, std::string filename) + { + throw lpstd::exceptions::NotImplementedException(); + // this->level_filename[level] = filename; + } + + void setLogmode(Logmode mode) + { + this->mode = mode; + } + + void setFilename(std::string filename) + { + this->filename = filename; + if (this->file.is_open()) + { + this->file.close(); + } + this->initFile(); + } + + void setLevel(LogLevel level) + { + this->level = level; + } + + void log(std::string message) + { + this->log(this->level, message); + } + + void log(LogLevel level, std::string message) + { + if (file.is_open() == false) + { + this->initFile(); + } + + std::time_t t = std::time(nullptr); + std::tm tm = *std::localtime(&t); + std::ostringstream oss; + oss << std::put_time(&tm, "%d-%m-%Y %H-%M-%S"); + std::string str = oss.str(); + + message = "[" + str + "] " + message; + + switch (level) + { + case LogLevel::LOG_DEBUG: + this->message = "[DEBUG] " + message; + break; + case LogLevel::LOG_INFO: + this->message = "[INFO] " + message; + break; + case LogLevel::LOG_WARNING: + this->message = "[WARNING] " + message; + break; + case LogLevel::LOG_ERROR: + this->message = "[ERROR] " + message; + break; + case LogLevel::LOG_FATAL: + this->message = "[FATAL] " + message; + break; + default: + break; + } + + this->LogToLogmode(this->message, level); + } + + void debug(std::string message) + { + this->log(LogLevel::LOG_DEBUG, message); + } + + void info(std::string message) + { + this->log(LogLevel::LOG_INFO, message); + } + + void warning(std::string message) + { + this->log(LogLevel::LOG_WARNING, message); + } + + void error(std::string message) + { + this->log(LogLevel::LOG_ERROR, message); + } + + void fatal(std::string message) + { + this->log(LogLevel::LOG_FATAL, message); + } + }; + + } // namespace logging +} // namespace lpstd +#endif // LOGGER_HPP From 866830d61af1f9f24fcf51e262e15f8290a3b018 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Mon, 6 Nov 2023 21:01:02 +0100 Subject: [PATCH 5/5] add webserver.hpp --- lpstd.hpp | 1 + src/classes/webserver.hpp | 92 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/classes/webserver.hpp diff --git a/lpstd.hpp b/lpstd.hpp index 37d9e9d..856e5ed 100644 --- a/lpstd.hpp +++ b/lpstd.hpp @@ -14,5 +14,6 @@ // #include "src/faker/faker.hpp" #include "src/exceptions/exceptions.hpp" #include "src/classes/command.hpp" +#include "src/classes/webserver.hpp" #endif // __LPSTD_HPP__ diff --git a/src/classes/webserver.hpp b/src/classes/webserver.hpp new file mode 100644 index 0000000..8635233 --- /dev/null +++ b/src/classes/webserver.hpp @@ -0,0 +1,92 @@ +#ifndef __LPSTD_WEBSERVER_H__ +#define __LPSTD_WEBSERVER_H__ + +#include "../patterns/singleton.hpp" +#include +#include +#include + + +namespace lpstd +{ + namespace classes + { + + namespace web + { + struct route + { + std::string path; + std::string method; + void (*func)(); + }; + } + + class WebServer : public lpstd::Singleton + { + + private: + std::vector routes = {}; + + public: + WebServer(/* args */) + { + } + ~WebServer() + { + } + + void addRoute(web::route route) + { + this->routes.push_back(route); + } + + void addRoute(std::string path, std::string method, void (*func)()) + { + web::route route = {path : path, method : method, func : func}; + this->routes.push_back(route); + } + + void get(std::string path, void (*func)()) + { + this->addRoute(path, "GET", func); + } + + void post(std::string path, void (*func)()) + { + this->addRoute(path, "POST", func); + } + + void put(std::string path, void (*func)()) + { + this->addRoute(path, "PUT", func); + } + + void patch(std::string path, void (*func)()) + { + this->addRoute(path, "PATCH", func); + } + + void del(std::string path, void (*func)()) + { + this->addRoute(path, "DELETE", func); + } + + void run(int port = 3000) + { + for (auto &route : this->routes) + { + std::cout << route.path << std::endl; + } + + std::cout << "Running on port " << port << std::endl; + + std::cout << "Listening..." << std::endl; + } + }; + + } // namespace classes + +} // namespace + +#endif // __LPSTD_WEBSERVER_H__ \ No newline at end of file