-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
125 lines (109 loc) · 3.57 KB
/
main.cpp
File metadata and controls
125 lines (109 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fpalumbo <fpalumbo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 12:48:45 by jfazi #+# #+# */
/* Updated: 2024/09/02 19:05:11 by fpalumbo ### ########.fr */
/* */
/* ************************************************************************** */
#include "webserv.h"
#include "Server.hpp"
#include "Parsconfig.hpp"
Server *global_server = NULL;
std::vector<std::string> parsconfigs;
bool hasConfExtension(const std::string &filename)
{
size_t pos = filename.rfind(".");
if (pos != std::string::npos)
{
std::string extension = filename.substr(pos);
if (extension == ".conf")
return true;
}
return false;
}
void signalHandler(int signum)
{
std::cout << std::endl << GREEN << "Interrupt signal (" << signum << ") received." << RESET << std::endl;
if (global_server != NULL)
{
global_server->CloseSockets();
global_server->FreeAllConfig();
parsconfigs.clear();
delete global_server;
global_server = NULL;
}
std::cout << GREEN << "Server deleted and resources freed." << RESET << std::endl;
exit(signum);
}
std::vector<std::string> cutParsconfig(std::string file_name)
{
std::string str_file;
std::ifstream file(file_name.c_str());
std::string line;
std::vector<std::string> serv;
if (!file.is_open())
throw(Parsconfig::Badfile());
while (getline(file, line))
{
str_file += line;
if (!file.eof())
str_file += '\n';
if (!line.compare("};"))
{
serv.push_back(str_file);
str_file.clear();
}
}
file.close();
return (serv);
}
int main(int argc, char **argv)
{
signal(SIGINT, signalHandler);
try
{
if (argc <= 2)
{
if (argc == 1)
parsconfigs = cutParsconfig("config/default.conf");
else
{
if (!hasConfExtension(argv[1]))
{
std::cout << RED << ".conf expected" << RESET << std::endl;
return 1;
}
parsconfigs = cutParsconfig(argv[1]);
}
if (!parsconfigs.empty())
{
global_server = new Server();
for (size_t i = 0; i < parsconfigs.size(); i++)
{
global_server->SetParsconfig(new Parsconfig(parsconfigs[i]));
}
global_server->Init();
global_server->CloseSockets();
delete global_server;
global_server = NULL;
}
else
{
std::cout << RED << "No valid server configurations found." << RESET << std::endl;
}
}
else
{
std::cout << RED << "Usage : ./webserv serv.conf" << RESET << std::endl;
}
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
return 0;
}