Compare commits

...

7 Commits

6 changed files with 370 additions and 22 deletions

5
.gitignore vendored
View File

@ -1,4 +1,5 @@
main.cpp test.cpp
*.exe *.exe
*.out *.out
.vscode .vscode
makefile

View File

@ -7,10 +7,13 @@
#include <windows.h> #include <windows.h>
#endif #endif
#include "src/logging/logger.hpp"
#include "src/patterns/singleton.hpp" #include "src/patterns/singleton.hpp"
#include "src/testing/testing.hpp" #include "src/testing/testing.hpp"
// #include "src/faker/faker.hpp" // #include "src/faker/faker.hpp"
#include "src/exceptions/exceptions.hpp" #include "src/exceptions/exceptions.hpp"
#include "src/classes/command.hpp" #include "src/classes/command.hpp"
#include "src/classes/webserver.hpp"
#endif // __LPSTD_HPP__ #endif // __LPSTD_HPP__

View File

@ -20,6 +20,7 @@ namespace lpstd
std::string name; std::string name;
std::string description; std::string description;
void (*func)(); void (*func)();
std::vector<std::string> aliases;
std::vector<std::string> args; std::vector<std::string> args;
}; };
@ -32,7 +33,8 @@ namespace lpstd
commandInfo currentCommand = {name : "", commandInfo currentCommand = {name : "",
description : "", description : "",
func : nullptr}; func : nullptr,
args : {}};
public: public:
Command(/* args */) Command(/* args */)
@ -87,6 +89,11 @@ namespace lpstd
return values.size() > 0 ? values[0] : ""; return values.size() > 0 ? values[0] : "";
} }
std::list<commandInfo> getCommands()
{
return this->commands;
}
void addCommand(std::string name, std::string description, void (*func)(void)) void addCommand(std::string name, std::string description, void (*func)(void))
{ {
commandInfo command; commandInfo command;
@ -95,6 +102,18 @@ namespace lpstd
command.func = func; command.func = func;
this->commands.push_back(command); 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[])) // void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]))
// { // {
@ -115,7 +134,26 @@ namespace lpstd
for (auto &commandInfo : sortedCommands) 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; return help;
@ -172,6 +210,17 @@ namespace lpstd
commandInfo.func(); commandInfo.func();
return; return;
} }
for (auto &alias : commandInfo.aliases)
{
if (alias == command)
{
commandInfo.args = args;
currentCommand = commandInfo;
commandInfo.func();
return;
}
}
} }
std::cout << "Command not found" << std::endl; std::cout << "Command not found" << std::endl;
} }

92
src/classes/webserver.hpp Normal file
View File

@ -0,0 +1,92 @@
#ifndef __LPSTD_WEBSERVER_H__
#define __LPSTD_WEBSERVER_H__
#include "../patterns/singleton.hpp"
#include <iostream>
#include <string>
#include <vector>
namespace lpstd
{
namespace classes
{
namespace web
{
struct route
{
std::string path;
std::string method;
void (*func)();
};
}
class WebServer : public lpstd::Singleton<WebServer>
{
private:
std::vector<web::route> 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__

200
src/logging/logger.hpp Normal file
View File

@ -0,0 +1,200 @@
#ifndef LOGGER_HPP
#define LOGGER_HPP
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <map>
#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<Logger>
{
private:
Logmode mode = Logmode::CONSOLE;
std::map<LogLevel, std::string> level_filename;
std::map<LogLevel, std::ofstream> 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

View File

@ -13,13 +13,13 @@ namespace lpstd
namespace testing namespace testing
{ {
struct Results struct Testingresults
{ {
int passed = 0; int passed = 0;
int failed = 0; int failed = 0;
}; };
Results Results; Testingresults results;
void drawResults() void drawResults()
{ {
@ -27,13 +27,13 @@ namespace lpstd
needUTF8(); needUTF8();
std::cout << std::endl; std::cout << std::endl;
if (Results.failed == 0) if (results.failed == 0)
{ {
std::cout << "=== ✅ All tests passed ===" << std::endl; std::cout << "=== ✅ All tests passed ===" << std::endl;
} }
else 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) if (result)
{ {
std::cout << "✅ Expected " << this->value << " to be " << expected << std::endl; std::cout << "✅ Expected " << this->value << " to be " << expected << std::endl;
Results.passed++; results.passed++;
} }
else else
{ {
std::cout << "❌ Expected " << expected << " but got " << this->value << std::endl; std::cout << "❌ Expected " << expected << " but got " << this->value << std::endl;
Results.failed++; results.failed++;
} }
return result; return result;
} }
@ -71,12 +71,12 @@ namespace lpstd
if (result) if (result)
{ {
std::cout << "✅ Expected " << this->value << " to be greater than " << expected << std::endl; std::cout << "✅ Expected " << this->value << " to be greater than " << expected << std::endl;
Results.passed++; results.passed++;
} }
else else
{ {
std::cout << "❌ Expected " << this->value << " to be greater than " << expected << std::endl; std::cout << "❌ Expected " << this->value << " to be greater than " << expected << std::endl;
Results.failed++; results.failed++;
} }
return result; return result;
@ -88,12 +88,12 @@ namespace lpstd
if (result) if (result)
{ {
std::cout << "✅ Expected " << this->value << " to be less than " << expected << std::endl; std::cout << "✅ Expected " << this->value << " to be less than " << expected << std::endl;
Results.passed++; results.passed++;
} }
else else
{ {
std::cout << "❌ Expected " << this->value << " to be less than " << expected << std::endl; std::cout << "❌ Expected " << this->value << " to be less than " << expected << std::endl;
Results.failed++; results.failed++;
} }
return result; return result;
} }
@ -104,12 +104,12 @@ namespace lpstd
if (result) if (result)
{ {
std::cout << "✅ Expected " << this->value << " to be true" << std::endl; std::cout << "✅ Expected " << this->value << " to be true" << std::endl;
Results.passed++; results.passed++;
} }
else else
{ {
std::cout << "❌ Expected " << this->value << " to be true" << std::endl; std::cout << "❌ Expected " << this->value << " to be true" << std::endl;
Results.failed++; results.failed++;
} }
return result; return result;
} }
@ -120,12 +120,12 @@ namespace lpstd
if (result) if (result)
{ {
std::cout << "✅ Expected " << this->value << " to be false" << std::endl; std::cout << "✅ Expected " << this->value << " to be false" << std::endl;
Results.passed++; results.passed++;
} }
else else
{ {
std::cout << "❌ Expected " << this->value << " to be false" << std::endl; std::cout << "❌ Expected " << this->value << " to be false" << std::endl;
Results.failed++; results.failed++;
} }
return result; return result;
} }
@ -136,12 +136,12 @@ namespace lpstd
if (result) if (result)
{ {
std::cout << "✅ Expected " << this->value << " to be close to " << expected << std::endl; std::cout << "✅ Expected " << this->value << " to be close to " << expected << std::endl;
Results.passed++; results.passed++;
} }
else else
{ {
std::cout << "❌ Expected " << this->value << " to be close to " << expected << std::endl; std::cout << "❌ Expected " << this->value << " to be close to " << expected << std::endl;
Results.failed++; results.failed++;
} }
return result; return result;
} }
@ -167,6 +167,7 @@ namespace lpstd
{ {
std::cout << description << std::endl; std::cout << description << std::endl;
testCase(); testCase();
std::cout << std::endl;
} }
// Custom describe function to group related test cases // Custom describe function to group related test cases
@ -174,6 +175,8 @@ namespace lpstd
{ {
std::cout << "Describing " << description << std::endl; std::cout << "Describing " << description << std::endl;
testSuite(); testSuite();
drawResults();
std::cout << std::endl;
} }
void testThrow(std::function<void()> testCase) void testThrow(std::function<void()> testCase)
@ -185,11 +188,11 @@ namespace lpstd
catch (std::exception &e) catch (std::exception &e)
{ {
std::cout << "✅ Expected exception thrown: " << e.what() << std::endl; std::cout << "✅ Expected exception thrown: " << e.what() << std::endl;
Results.passed++; results.passed++;
return; return;
} }
std::cout << "❌ Expected exception not thrown" << std::endl; std::cout << "❌ Expected exception not thrown" << std::endl;
Results.failed++; results.failed++;
} }
} }