Skip to content

Dev #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
main.cpp
test.cpp
*.exe
*.out
.vscode
.vscode
makefile
3 changes: 3 additions & 0 deletions lpstd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
#include <windows.h>
#endif

#include "src/logging/logger.hpp"

#include "src/patterns/singleton.hpp"
#include "src/testing/testing.hpp"
// #include "src/faker/faker.hpp"
#include "src/exceptions/exceptions.hpp"
#include "src/classes/command.hpp"
#include "src/classes/webserver.hpp"

#endif // __LPSTD_HPP__
53 changes: 51 additions & 2 deletions src/classes/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace lpstd
std::string name;
std::string description;
void (*func)();
std::vector<std::string> aliases;
std::vector<std::string> args;
};

Expand All @@ -32,7 +33,8 @@ namespace lpstd

commandInfo currentCommand = {name : "",
description : "",
func : nullptr};
func : nullptr,
args : {}};

public:
Command(/* args */)
Expand Down Expand Up @@ -87,6 +89,11 @@ namespace lpstd
return values.size() > 0 ? values[0] : "";
}

std::list<commandInfo> getCommands()
{
return this->commands;
}

void addCommand(std::string name, std::string description, void (*func)(void))
{
commandInfo command;
Expand All @@ -95,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[]))
// {
Expand All @@ -115,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;
Expand Down Expand Up @@ -172,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;
}
Expand Down
92 changes: 92 additions & 0 deletions src/classes/webserver.hpp
Original file line number Diff line number Diff line change
@@ -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__
Loading