From 8dff506caf2aae5323260691a0e43c89accc8f64 Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 10 Apr 2025 14:19:30 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20Added=20help=20msg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 8 ++++++ exemples/default.toml | 9 ++++++ includes/help.hpp | 16 +++++++++++ includes/log.hpp | 10 +++---- includes/webserv.hpp | 2 +- src/help.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 exemples/default.toml create mode 100644 includes/help.hpp create mode 100644 src/help.cpp diff --git a/Makefile b/Makefile index 4aef912..e2cedc7 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,14 @@ ifeq ($(VERBOSE),true) FLAGS += -D VERBOSE endif +ifeq ($(TTY),true) + FLAGS += -D TTY +endif + +ifeq ($(PKGS),true) + FLAGS += -D PKGS +endif + all: $(NAME) $(NAME): $(OBJS) diff --git a/exemples/default.toml b/exemples/default.toml new file mode 100644 index 0000000..531a1af --- /dev/null +++ b/exemples/default.toml @@ -0,0 +1,9 @@ +[server] +host = "localhost" +port = 8080 + +[server.location./] +methods = { "GET" } +root = "/var/www/html" +dirlist = true +client_max_body_size = "10M" diff --git a/includes/help.hpp b/includes/help.hpp new file mode 100644 index 0000000..442cfc1 --- /dev/null +++ b/includes/help.hpp @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* help.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/10 13:43:54 by adjoly #+# #+# */ +/* Updated: 2025/04/10 13:58:52 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#define SAMPLE_CONF_PATH "./sample.conf" +#define WEBSRV_VERSION "v0.1" diff --git a/includes/log.hpp b/includes/log.hpp index df5e305..787eca1 100644 --- a/includes/log.hpp +++ b/includes/log.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */ -/* Updated: 2025/03/25 17:50:45 by adjoly ### ########.fr */ +/* Updated: 2025/04/10 13:56:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,10 +29,7 @@ class Logger { _ttyOnly = false; } if (!_file.is_open() && !_ttyOnly) { - throw std::runtime_error( - "could not open fileeee"); // TODO change that shit but i dont - // know what to put other than a - // htrow + warn("could not open logfile, going tty only"); } } @@ -97,7 +94,8 @@ class Logger { const std::string &what, const std::string &msg) { std::stringstream os; -#ifdef tty +#ifdef TTY + (void)emoji; if (what.empty()) os << type << ":" << msg; else diff --git a/includes/webserv.hpp b/includes/webserv.hpp index f638297..a519920 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/10 13:08:36 by adjoly #+# #+# */ +/* Updated: 2025/04/10 14:19:20 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "log.hpp" +#include +#include +#include +#include +#include + +void _printHelp(void) { + std::cout << "-------------- WEBSERV --------------" << std::endl; + std::cout << "- --help : Print this message" << std::endl; + std::cout << "- --generate : Generate a sample config" << std::endl; + std::cout << "- --version : Gives you the version of the server" + << std::endl; + std::cout << "-------------------------------------" << std::endl; +} + +void _generateConf(void) { + webserv::Logger _log(""); + if (access("./sample.conf", F_OK) == 0) { + _log.warn(std::string(SAMPLE_CONF_PATH) + " already exist, aborting"); + } else { + _log.info("generating config into ./sample.conf..."); + std::ofstream file("./sample.conf"); + if (file.is_open()) { + file << "[server]\nhost = \"localhost\"\nport = " + "8080\n\n[server.location./]\nmethods = { \"GET\" }\nroot " + "= \"/var/www/html\"\ndirlist = true\nclient_max_body_size " + "= \"10M\""; + file.close(); + _log.info("config file successfully generated"); + } else { + _log.warn("could not generate, sample config file"); + } + } +} + +void _printVersion(void) { + std::cout << "You are running : Webserv " << WEBSRV_VERSION << std::endl; +} + +void help(int ac, char **av) { + if (ac < 2) { + _printHelp(); + return; + } + std::string option = av[1]; + if (option == "--help") + _printHelp(); + else if (option == "--generate" || option == "-g") + _generateConf(); + else if (option == "--version" || option == "-v") + _printVersion(); +}