Skip to content

Commit

Permalink
feat: splited configuration and server class, now working with c11
Browse files Browse the repository at this point in the history
  • Loading branch information
0bvim committed Aug 30, 2024
1 parent 66413f7 commit d1c734a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 70 deletions.
74 changes: 74 additions & 0 deletions src/Configuration/Configuration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "Configuration.hpp"
#include <fstream>
#include <iostream>
#include <sstream>

Configuration::Configuration() : host("127.0.0.1")
{
// Default values (host can be modified)
}

bool Configuration::loadFromFile(const std::string &filename)
{
std::ifstream file(filename);
if (!file.is_open())
{
std::cerr << "Could not open configuration file: " << filename << std::endl;
return false;
}

std::string line;
while (std::getline(file, line))
{
parseLine(line);
}

file.close();
return true;
}

void Configuration::parseLine(const std::string &line)
{
std::istringstream iss(line);
std::string key, value;

if (!(iss >> key >> value))
{
return; // Skip empty or malformed lines
}

if (key == "port")
{
ports.push_back(std::stoi(value));
}
else if (key == "host")
{
host = value;
}
else if (key == "route")
{
std::string path, file;
iss >> path >> file;
addRoute(path, file);
}
}

void Configuration::addRoute(const std::string &path, const std::string &file)
{
routes[path] = file;
}

std::vector<int> Configuration::getPorts() const
{
return ports;
}

std::string Configuration::getHost() const
{
return host;
}

std::map<std::string, std::string> Configuration::getRoutes() const
{
return routes;
}
2 changes: 2 additions & 0 deletions src/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <unistd.h>
#include <sys/epoll.h>
#include <sstream>
#include <fstream>
#include "Server.hpp"
#include "../Configuration/Configuration.hpp"

Server::Server(const Configuration &config) : events(10)
{
Expand Down
71 changes: 1 addition & 70 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,78 +14,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#include "Server/Server.hpp"
#include "Configuration/Configuration.hpp"


Configuration::Configuration() : host("127.0.0.1")
{
// Default values (host can be modified)
}

bool Configuration::loadFromFile(const std::string &filename)
{
std::ifstream file(filename);
if (!file.is_open())
{
std::cerr << "Could not open configuration file: " << filename << std::endl;
return false;
}

std::string line;
while (std::getline(file, line))
{
parseLine(line);
}

file.close();
return true;
}

void Configuration::parseLine(const std::string &line)
{
std::istringstream iss(line);
std::string key, value;

if (!(iss >> key >> value))
{
return; // Skip empty or malformed lines
}

if (key == "port")
{
ports.push_back(std::stoi(value));
}
else if (key == "host")
{
host = value;
}
else if (key == "route")
{
std::string path, file;
iss >> path >> file;
addRoute(path, file);
}
}

void Configuration::addRoute(const std::string &path, const std::string &file)
{
routes[path] = file;
}

std::vector<int> Configuration::getPorts() const
{
return ports;
}

std::string Configuration::getHost() const
{
return host;
}

std::map<std::string, std::string> Configuration::getRoutes() const
{
return routes;
}


// Main function
int main()
Expand Down

0 comments on commit d1c734a

Please sign in to comment.