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

View File

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

View File

@ -6,13 +6,17 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
#ifdef PKGS
#define SAMPLE_CONF_PATH "./sample.toml"
#else
#define SAMPLE_CONF_PATH "/etc/webserv/default.conf"
#endif
#define WEBSRV_VERSION "v0.2"
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)
_dirlist = *static_cast<bool *>(val);
else
_dirlist = true;
_dirlist = false;
/* val = accessValue("cookies", toml::BOOL, _table, _log); */
/* if (val != not_nullptr) */
/* _cookies = *static_cast<bool *>(val); */

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
void _generateConf(void) {
void _generateConf(const std::string path) {
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");
} else {
std::stringstream str;
str << "generating config into " << SAMPLE_CONF_PATH << "...";
_log.info(str.str());
std::ofstream file(SAMPLE_CONF_PATH);
std::ofstream file(path.c_str());
if (file.is_open()) {
file << "[server]\nhost = \"0.0.0.0\"\nport = "
"80\n\n[server.location./]\ndirlist = false\n";
"80\n\n[server.location./]\n";
file.close();
_log.info("config file successfully generated");
} else {
@ -58,15 +58,15 @@ void _printVersion(void) {
bool help(int ac, char **av) {
if (ac < 2) {
_printHelp();
return true;
_generateConf(SAMPLE_CONF_PATH);
return false;
}
std::string option = av[1];
if (option == "--help" || option == "-v") {
_printHelp();
return true;
} else if (option == "--generate" || option == "-g") {
_generateConf();
_generateConf(SAMPLE_CONF_PATH);
return true;
} else if (option == "--version" || option == "-v") {
_printVersion();

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
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;
str << "File : " << av[1] << " could not be opened";
log.error(str.str());
@ -55,7 +62,12 @@ int main(int ac, char **av) {
_log = not_nullptr;
config::Config *conf;
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);
} catch (std::exception &) {
if (_log != not_nullptr)