Enhance code style and organization: update .clang-format for include sorting, enable parallel compilation in CMake, and clean up includes across multiple files for improved readability and consistency.

This commit is contained in:
whaffman 2025-09-20 23:21:23 +02:00
parent 94cd396ac1
commit df97596321
23 changed files with 122 additions and 89 deletions

View File

@ -1 +1,38 @@
BasedOnStyle: Microsoft
BasedOnStyle: Microsoft
# Include ordering
SortIncludes: CaseSensitive
IncludeBlocks: Regroup
IncludeCategories:
# Main header (current file's header)
- Regex: '^".*\.hpp"$'
Priority: 1
SortPriority: 1
CaseSensitive: true
# Project headers
- Regex: '^<webserv/.*\.hpp>$'
Priority: 2
SortPriority: 2
CaseSensitive: true
# System C++ headers
- Regex: '^<[a-z_]+>$'
Priority: 3
SortPriority: 3
CaseSensitive: true
# System C headers
- Regex: '^<.*\.h>$'
Priority: 4
SortPriority: 4
CaseSensitive: true
# Third-party libraries
- Regex: '^<.*>$'
Priority: 5
SortPriority: 5
CaseSensitive: true
# Allow simple functions and if statements on one line
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true

3
.gitignore vendored
View File

@ -2,7 +2,4 @@
*.a
build
.cache
webserv.log

View File

@ -5,6 +5,13 @@ project(webserv)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Enable parallel compilation
include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
set(CMAKE_BUILD_PARALLEL_LEVEL ${N})
endif()
# Add source files
file(GLOB_RECURSE SOURCES
"${PROJECT_SOURCE_DIR}/webserv/*.cpp"

View File

@ -1,8 +1,10 @@
#include "webserv/socket/Socket.hpp"
#include <iostream>
#include <webserv/client/Client.hpp>
#include <webserv/log/Log.hpp>
#include <iostream>
Client::Client(std::unique_ptr<Socket> socket, Server &server, const ServerConfig &server_config)
: client_socket_(std::move(socket)), server(std::ref(server)), server_config(std::cref(server_config))
{
@ -14,7 +16,7 @@ Client::~Client()
server.removeFromEpoll(*client_socket_);
};
int Client::parseHeaderforContentLength(const std::string &request) //NOLINT
int Client::parseHeaderforContentLength(const std::string &request) // NOLINT
{
std::string header = "Content-Length: ";
size_t pos = request.find(header);

View File

@ -1,10 +1,12 @@
#pragma once
#include "webserv/socket/Socket.hpp"
#include <memory>
#include <webserv/config/ServerConfig.hpp>
#include <webserv/server/Server.hpp>
#include <memory>
class Server;
class Client

View File

@ -8,13 +8,9 @@
#include <sstream>
#include <stdexcept>
ConfigManager::ConfigManager() : _initialized(false)
{
}
ConfigManager::ConfigManager() : _initialized(false) {}
ConfigManager::~ConfigManager()
{
}
ConfigManager::~ConfigManager() {}
ConfigManager &ConfigManager::getInstance()
{

View File

@ -1,10 +1,10 @@
#pragma once
#include <webserv/config/ServerConfig.hpp>
#include <string>
#include <vector>
#include <webserv/config/ServerConfig.hpp>
class ConfigManager
{
public:

View File

@ -2,7 +2,6 @@
#include <webserv/config/utils.hpp>
#include <webserv/log/Log.hpp>
#include <iostream>
#include <sstream>
#include <string>

View File

@ -1,8 +1,7 @@
#pragma once
#include <string>
#include <map>
#include <string>
class LocationConfig
{

View File

@ -1,9 +1,9 @@
#include "ServerConfig.hpp"
#include <webserv/config/LocationConfig.hpp>
#include <webserv/config/ServerConfig.hpp>
#include <webserv/config/utils.hpp>
#include <webserv/log/Log.hpp>
#include <iostream>
#include <sstream>
#include <string>
@ -54,7 +54,7 @@ void ServerConfig::parseServerBlock(const std::string &block)
void ServerConfig::parseDirectives(const std::string &declarations)
{
LOG_INFO("Parsing server directives" );
LOG_INFO("Parsing server directives");
std::string line;
std::istringstream stream(declarations);
while (std::getline(stream, line))

View File

@ -1,13 +1,13 @@
#pragma once
#include <webserv/config/LocationConfig.hpp>
#include <map>
#include <string>
#include <vector>
#include <webserv/config/LocationConfig.hpp>
class ServerConfig
{
{
public:
ServerConfig(const std::string &serverBlock);
@ -20,6 +20,7 @@ class ServerConfig
[[nodiscard]] const std::vector<std::string> &getIndexFiles() const { return index_files; }
[[nodiscard]] const LocationConfig &getLocation(const std::string &path) const;
[[nodiscard]] std::vector<std::string> getLocationPaths() const;
void setServerFD(int fd) { server_fd = fd; }
[[nodiscard]] int getServerFD() const { return server_fd; }

View File

@ -1,10 +1,11 @@
#pragma once
#include <string>
#include <map>
#include <webserv/log/Log.hpp>
#include <webserv/log/LogLevel.hpp>
#include <map>
#include <string>
class Channel
{
public:

View File

@ -1,13 +1,11 @@
#include "webserv/log/LogLevel.hpp"
#include <webserv/log/FileChannel.hpp>
#include <chrono>
#include <iostream>
FileChannel::FileChannel(const std::string &filename)
: filename_(filename),
fileStream_(filename, std::ios::trunc)
FileChannel::FileChannel(const std::string &filename) : filename_(filename), fileStream_(filename, std::ios::trunc)
{
if (!fileStream_.is_open())
{
@ -23,8 +21,7 @@ FileChannel::~FileChannel()
}
}
void FileChannel::log(LogLevel &logLevel, const std::string &message,
const std::map<std::string, std::string> &context)
void FileChannel::log(LogLevel &logLevel, const std::string &message, const std::map<std::string, std::string> &context)
{
if (!fileStream_.is_open())
{
@ -39,7 +36,7 @@ void FileChannel::log(LogLevel &logLevel, const std::string &message,
// Format the log message
fileStream_ << "[" << std::put_time(tm, "%Y-%m-%d %H:%M:%S") << "] "
<< "[" << logLevelToString(logLevel) << "] " << message << '\n';
<< "[" << logLevelToString(logLevel) << "] " << message << '\n';
// Log the context if it exists
if (!context.empty())
@ -52,4 +49,3 @@ void FileChannel::log(LogLevel &logLevel, const std::string &message,
}
fileStream_ << std::flush;
}

View File

@ -3,9 +3,9 @@
#include <webserv/log/Channel.hpp>
#include <webserv/log/LogLevel.hpp>
#include <string>
#include <fstream>
#include <map>
#include <string>
class FileChannel : public Channel
{
@ -24,4 +24,4 @@ class FileChannel : public Channel
private:
std::string filename_;
std::ofstream fileStream_;
};
};

View File

@ -1,10 +1,11 @@
#include <chrono>
#include <filesystem>
#include <iostream>
#include <webserv/log/FileChannel.hpp>
#include <webserv/log/Log.hpp>
#include <webserv/log/StdoutChannel.hpp>
#include <chrono>
#include <filesystem>
#include <iostream>
Log::Log()
{
// get start time
@ -52,6 +53,7 @@ void Log::log(LogLevel level, const std::string &message, const std::string &fil
if (it != channels_.end())
{
std::string extendedMessage;
extendedMessage += message + " | ";
if (!file.empty())
{
extendedMessage += std::filesystem::path(file).filename().string();
@ -64,7 +66,7 @@ void Log::log(LogLevel level, const std::string &message, const std::string &fil
{
extendedMessage += " (" + function + ")";
}
extendedMessage += " | " + message;
// extendedMessage += " | " + message;
it->second->log(level, extendedMessage, context);
}
}

View File

@ -1,12 +1,13 @@
#pragma once
#include <webserv/log/Channel.hpp>
#include <webserv/log/LogLevel.hpp>
#include <chrono>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <webserv/log/Channel.hpp>
#include <webserv/log/LogLevel.hpp>
#define LOG(level, message) Log::static_log((level), (message), __FILE__, __LINE__, __FUNCTION__, "file", {})

View File

@ -29,18 +29,12 @@ inline std::string logLevelToString(LogLevel level)
{
switch (level)
{
case LogLevel::LOGLVL_TRACE:
return "TRACE";
case LogLevel::LOGLVL_DEBUG:
return "DEBUG";
case LogLevel::LOGLVL_INFO:
return "INFO";
case LogLevel::LOGLVL_WARN:
return "WARN";
case LogLevel::LOGLVL_ERROR:
return "ERROR";
case LogLevel::LOGLVL_FATAL:
return "FATAL";
case LogLevel::LOGLVL_TRACE: return "TRACE";
case LogLevel::LOGLVL_DEBUG: return "DEBUG";
case LogLevel::LOGLVL_INFO: return "INFO";
case LogLevel::LOGLVL_WARN: return "WARN";
case LogLevel::LOGLVL_ERROR: return "ERROR";
case LogLevel::LOGLVL_FATAL: return "FATAL";
}
return "UNKNOWN";
}
@ -49,18 +43,12 @@ inline const char *logLevelToColor(LogLevel level)
{
switch (level)
{
case LogLevel::LOGLVL_TRACE:
return LogColors::TRACE_COLOR;
case LogLevel::LOGLVL_DEBUG:
return LogColors::DEBUG_COLOR;
case LogLevel::LOGLVL_INFO:
return LogColors::INFO_COLOR;
case LogLevel::LOGLVL_WARN:
return LogColors::WARN_COLOR;
case LogLevel::LOGLVL_ERROR:
return LogColors::ERROR_COLOR;
case LogLevel::LOGLVL_FATAL:
return LogColors::FATAL_COLOR;
case LogLevel::LOGLVL_TRACE: return LogColors::TRACE_COLOR;
case LogLevel::LOGLVL_DEBUG: return LogColors::DEBUG_COLOR;
case LogLevel::LOGLVL_INFO: return LogColors::INFO_COLOR;
case LogLevel::LOGLVL_WARN: return LogColors::WARN_COLOR;
case LogLevel::LOGLVL_ERROR: return LogColors::ERROR_COLOR;
case LogLevel::LOGLVL_FATAL: return LogColors::FATAL_COLOR;
}
return LogColors::RESET;
}

View File

@ -1,7 +1,8 @@
#include <webserv/log/StdoutChannel.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <webserv/log/StdoutChannel.hpp>
#include <iomanip>
void StdoutChannel::log(LogLevel &logLevel, const std::string &message,
const std::map<std::string, std::string> &context)

View File

@ -1,4 +1,5 @@
#include "webserv/log/Log.hpp"
#include <webserv/config/ConfigManager.hpp>
#include <webserv/config/LocationConfig.hpp>
#include <webserv/config/ServerConfig.hpp>

View File

@ -1,17 +1,18 @@
#include "webserv/socket/Socket.hpp"
#include <arpa/inet.h> // For inet_addr
#include <cstdlib> // For exit()
#include <cstring> // For memset
#include <fcntl.h> // For fcntl()"
#include <iostream>
#include <webserv/log/Log.hpp>
#include <webserv/server/Server.hpp>
#include <cstring> // For memset
#include <memory>
#include <vector>
#include <arpa/inet.h> // For inet_addr
#include <fcntl.h> // For fcntl()
#include <netinet/in.h> // For sockaddr_in
#include <sys/epoll.h>
#include <sys/socket.h> // For socket functions
#include <unistd.h> // For close()
#include <vector>
#include <webserv/server/Server.hpp>
#include <webserv/log/Log.hpp>
Server::Server(const ConfigManager &configManager) : epoll_fd_(epoll_create1(0)), configManager_(configManager)
{
@ -207,8 +208,8 @@ void Server::eventLoop()
ssize_t bytesSent = send(event.data.fd, httpResponse, strlen(httpResponse), 0);
if (bytesSent < 0)
{
LOG_ERROR("Send failed for fd: " + std::to_string(event.data.fd) + " with error: " + std::strerror(errno));
LOG_ERROR("Send failed for fd: " + std::to_string(event.data.fd) +
" with error: " + std::strerror(errno));
}
else
{

View File

@ -1,13 +1,15 @@
#pragma once
#include "webserv/config/ServerConfig.hpp"
#include <functional>
#include <memory>
#include <unordered_map>
#include <webserv/client/Client.hpp>
#include <webserv/config/ConfigManager.hpp>
#include <webserv/socket/Socket.hpp>
#include <functional>
#include <memory>
#include <unordered_map>
class Client;
class Server
@ -28,7 +30,7 @@ class Server
void start();
void addToEpoll(const Socket &socket, uint32_t events) const;
void removeFromEpoll(const Socket &socket) const ;
void removeFromEpoll(const Socket &socket) const;
void setupServerSocket(const ServerConfig &config);
void handleConnection(struct epoll_event *event);
void handleRequest(struct epoll_event *event) const;
@ -39,7 +41,6 @@ class Server
const ServerConfig &getConfig(int fd) const;
const ServerConfig &getConfig(const Socket &socket) const;
private:
int epoll_fd_;
const ConfigManager &configManager_;

View File

@ -1,14 +1,14 @@
#include <memory>
#include <webserv/socket/Socket.hpp>
#include <webserv/log/Log.hpp>
#include <webserv/socket/Socket.hpp>
#include <memory>
#include <stdexcept>
#include <unistd.h> // For close()
#include <arpa/inet.h> // For inet_addr
#include <fcntl.h> // For fcntl()"
#include <netinet/in.h> // For sockaddr_in
#include <sys/socket.h>
#include <unistd.h> // For close()
Socket::Socket() : _fd(socket(AF_INET, SOCK_STREAM, 0))
{
@ -32,7 +32,7 @@ Socket::Socket(int fd) : _fd(fd) // NOLINT(readability-identifier-naming)
{
if (_fd == -1)
{
LOG_ERROR("Invalid file descriptor");
LOG_ERROR("Invalid file descriptor");
throw std::runtime_error("Invalid file descriptor");
}
setNonBlocking();

View File

@ -2,6 +2,7 @@
#include <memory>
#include <string>
#include <sys/types.h>
class Socket
@ -10,8 +11,8 @@ class Socket
Socket();
Socket(int fd); // NOLINT readability-identifier-naming
Socket(const Socket &other) = delete; // Disable copy constructor
Socket &operator=(const Socket &other) = delete; // Disable copy assignment
Socket(const Socket &other) = delete; // Disable copy constructor
Socket &operator=(const Socket &other) = delete; // Disable copy assignment
Socket(Socket &&other) noexcept = default; // Move constructor
Socket &operator=(Socket &&other) noexcept = default; // Move assignment