-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
48 lines (39 loc) · 1.49 KB
/
main.cc
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
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <boost/asio.hpp>
#include "server.h"
#include "server_config_parser.h"
#include "request_handler.h"
using boost::asio::ip::tcp;
// Entry point of the program
int main(int argc, char* argv[]) {
try {
// Make sure a config file is passed to the program
if (argc != 2) {
std::cerr << "Usage: webserver <config_file>" << std::endl;
return 1;
}
// Parse the config file into statements and tokens
NginxServerConfigParser server_parser(argv[1]);
// Get the server settings from the config file (i.e. port)
ServerSettings server_settings;
int port = server_parser.ParseServerSettings(server_settings);
if (port == -1) {
std::cerr << "Missing port <number> in config file" << std::endl;
return 1;
}
// Parse the request handlers from the config file
std::map<std::string, std::unique_ptr<RequestHandler> > handlers;
std::map<std::string, std::string> handler_info;
server_parser.ParseRequestHandlers(handlers, handler_info);
printf("%lu handlers parsed\n", (unsigned long int)(handlers.size()));
// Start the server
printf("Running server on port %d...\n\n", port);
Server::Run(port, std::move(handlers), std::move(handler_info));
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}