Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
74 lines (62 loc) · 1.6 KB
/
Copy pathmain.cpp
File metadata and controls
74 lines (62 loc) · 1.6 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
#include <unistd.h>
#include <iostream>
#include <string>
#include <memory>
#include <set>
#include <functional>
#include <thread>
#include <unistd.h>
#include <signal.h>
#include <unistd.h>
#include "http_server.h"
using namespace asio::ip;
int main(int argc, char **argv)
{
address_v4 addr = make_address_v4("127.0.0.1");
unsigned short port = 1234;
std::string directory;
// parse command line options
char opt;
while((opt = getopt(argc, argv, "h:p:d:")) != -1)
{
switch(opt)
{
case 'h':
addr = make_address_v4(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 'd':
directory = optarg;
if(!directory.empty())
{
char buf[PATH_MAX];
realpath(directory.c_str(), buf);
directory = buf;
}
break;
default: break;
}
}
// daemon
signal(SIGHUP, SIG_IGN);
daemon(0, 0);
try
{
asio::io_service service;
http_server server(service, addr, port, directory);
unsigned int nThreads = std::thread::hardware_concurrency();
std::vector<std::thread> threads;
for (unsigned int i = 0; i < nThreads; ++i)
threads.emplace_back([&service](){ service.run(); });
for(auto &thread : threads)
thread.join();
}
catch(std::exception &ex)
{
std::string error = ex.what();
std::cerr << error << std::endl;
}
return 0;
}