」 feat: changed a few things to be more production ready

This commit is contained in:
2025-05-08 12:03:42 +02:00
parent 8f6875dc83
commit b418e4c25a
7 changed files with 47 additions and 25 deletions

12
flake.lock generated
View File

@ -20,11 +20,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1744232761, "lastModified": 1746461020,
"narHash": "sha256-gbl9hE39nQRpZaLjhWKmEu5ejtQsgI5TWYrIVVJn30U=", "narHash": "sha256-7+pG1I9jvxNlmln4YgnlW4o+w0TZX24k688mibiFDUE=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "f675531bc7e6657c10a18b565cfebd8aa9e24c14", "rev": "3730d8a308f94996a9ba7c7138ede69c1b9ac4ae",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -54,11 +54,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1738098586, "lastModified": 1745683407,
"narHash": "sha256-jibZsqeSh74PLIsOVdp2jIDiYxTHzlVaat0AXRcpeiU=", "narHash": "sha256-KBxdhcU39pctJx1yVSxiWVRNazzBRtwch7NYjwstf5s=",
"owner": "y-syo", "owner": "y-syo",
"repo": "pogit", "repo": "pogit",
"rev": "29a0535fea029e1c5e7762f187fc259f93927e31", "rev": "16adbe5cc1f39314761ec53d348cb86b60c901af",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -34,19 +34,20 @@
name = "webserv"; name = "webserv";
src = pkgs.fetchgit { src = pkgs.fetchgit {
url = "https://github.com/keyzox71/webserv.git"; url = "https://github.com/keyzox71/webserv.git";
rev = "8ef2803"; # Specify the revision rev = "8f6875d"; # Specify the revision
sha256 = "kjHPL1LInRv+8RE2Cyqt2R9M8qxpvkmvNjLhQLm3AWs="; # Specify the SHA-256 hash sha256 = "XEy48IhXGLHw5+DI2oaV03P7rWzH+gSbOLYrbO2YTrE="; # Specify the SHA-256 hash
fetchSubmodules = true; fetchSubmodules = true; # need it for tomlpp
}; };
buildInputs = with pkgs; [ buildInputs = with pkgs; [
clang clang
]; ];
buildPhase = '' buildPhase = ''
PKGS=true make PKGS=true make -j
''; '';
installPhase = '' installPhase = ''
mkdir -p $out/bin mkdir -p /etc/webserv
cp webserv $out/bin mkdir -p $out/bin
cp webserv $out/bin
''; '';
}; };
} }

View File

@ -6,13 +6,17 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/10 13:43:54 by adjoly #+# #+# */ /* Created: 2025/04/10 13:43:54 by adjoly #+# #+# */
/* Updated: 2025/05/06 17:10:16 by adjoly ### ########.fr */ /* Updated: 2025/05/08 11:54:17 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#ifdef PKGS
#define SAMPLE_CONF_PATH "./sample.toml" #define SAMPLE_CONF_PATH "./sample.toml"
#else
#define SAMPLE_CONF_PATH "/etc/webserv/default.conf"
#endif
#define WEBSRV_VERSION "v0.2" #define WEBSRV_VERSION "v0.2"
bool help(int, char **); bool help(int, char **);

5
sample.toml Normal file
View File

@ -0,0 +1,5 @@
[server]
host = "0.0.0.0"
port = 80
[server.location./]

View File

@ -80,7 +80,7 @@ Route::Route(toml::ANode *table)
if (val != not_nullptr) if (val != not_nullptr)
_dirlist = *static_cast<bool *>(val); _dirlist = *static_cast<bool *>(val);
else else
_dirlist = true; _dirlist = false;
/* val = accessValue("cookies", toml::BOOL, _table, _log); */ /* val = accessValue("cookies", toml::BOOL, _table, _log); */
/* if (val != not_nullptr) */ /* if (val != not_nullptr) */
/* _cookies = *static_cast<bool *>(val); */ /* _cookies = *static_cast<bool *>(val); */

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/10 13:08:36 by adjoly #+# #+# */ /* Created: 2025/04/10 13:08:36 by adjoly #+# #+# */
/* Updated: 2025/05/08 11:21:10 by adjoly ### ########.fr */ /* Updated: 2025/05/08 11:58:13 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,18 +29,18 @@ void _printHelp(void) {
std::cout << "-------------------------------------" << std::endl; std::cout << "-------------------------------------" << std::endl;
} }
void _generateConf(void) { void _generateConf(const std::string path) {
webserv::Logger _log; webserv::Logger _log;
if (access(SAMPLE_CONF_PATH, F_OK) == 0) { if (access(path.c_str(), F_OK) == 0) {
_log.warn(std::string(SAMPLE_CONF_PATH) + " already exist, aborting"); _log.warn(std::string(SAMPLE_CONF_PATH) + " already exist, aborting");
} else { } else {
std::stringstream str; std::stringstream str;
str << "generating config into " << SAMPLE_CONF_PATH << "..."; str << "generating config into " << SAMPLE_CONF_PATH << "...";
_log.info(str.str()); _log.info(str.str());
std::ofstream file(SAMPLE_CONF_PATH); std::ofstream file(path.c_str());
if (file.is_open()) { if (file.is_open()) {
file << "[server]\nhost = \"0.0.0.0\"\nport = " file << "[server]\nhost = \"0.0.0.0\"\nport = "
"80\n\n[server.location./]\ndirlist = false\n"; "80\n\n[server.location./]\n";
file.close(); file.close();
_log.info("config file successfully generated"); _log.info("config file successfully generated");
} else { } else {
@ -58,15 +58,15 @@ void _printVersion(void) {
bool help(int ac, char **av) { bool help(int ac, char **av) {
if (ac < 2) { if (ac < 2) {
_printHelp(); _generateConf(SAMPLE_CONF_PATH);
return true; return false;
} }
std::string option = av[1]; std::string option = av[1];
if (option == "--help" || option == "-v") { if (option == "--help" || option == "-v") {
_printHelp(); _printHelp();
return true; return true;
} else if (option == "--generate" || option == "-g") { } else if (option == "--generate" || option == "-g") {
_generateConf(); _generateConf(SAMPLE_CONF_PATH);
return true; return true;
} else if (option == "--version" || option == "-v") { } else if (option == "--version" || option == "-v") {
_printVersion(); _printVersion();

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/05/06 19:20:55 by adjoly ### ########.fr */ /* Updated: 2025/05/08 12:00:41 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,7 +45,14 @@ int main(int ac, char **av) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
log.info("Starting server..."); log.info("Starting server...");
if (access(av[1], F_OK) < 0) { if (ac < 2) {
if (access(SAMPLE_CONF_PATH, F_OK) < 0) {
std::stringstream str;
str << "File : " << SAMPLE_CONF_PATH << " could not be opened";
log.error(str.str());
return EXIT_FAILURE;
}
} else if (access(av[1], F_OK) < 0) {
std::stringstream str; std::stringstream str;
str << "File : " << av[1] << " could not be opened"; str << "File : " << av[1] << " could not be opened";
log.error(str.str()); log.error(str.str());
@ -55,7 +62,12 @@ int main(int ac, char **av) {
_log = not_nullptr; _log = not_nullptr;
config::Config *conf; config::Config *conf;
try { try {
std::string str = av[1]; std::string str;
if (ac < 2) {
str = SAMPLE_CONF_PATH;
} else {
str = av[1];
}
conf = new config::Config(str); conf = new config::Config(str);
} catch (std::exception &) { } catch (std::exception &) {
if (_log != not_nullptr) if (_log != not_nullptr)