-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
56 lines (48 loc) · 1.16 KB
/
server.cpp
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
#include <unistd.h>
#include "server.h"
int main(int argc, char *argv[])
{
// set signal handler
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
int option_daemon = 0;
if (argc == 2 && strcmp(argv[1], "-d") == 0)
{
option_daemon = 1;
}
if (option_daemon)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("fork failed");
exit(EXIT_FAILURE);
}
else if (pid > 0)
{
exit(EXIT_SUCCESS);
}
else
{
daemon(1, 0);
}
}
redis_server.reset(new RedisServer(REDIS_ADDR, REDIS_PORT));
event_init();
struct evhttp *server(evhttp_start(SERVER_ADDR, SERVER_PORT));
if (nullptr == server)
{
LOG(LOG_TYPE::LOG_ERROR, "create evhttp failed");
return -1;
}
evhttp_set_timeout(server, 5);
evhttp_set_cb(server, "/visit_info", visit_info_cb, nullptr);
evhttp_set_gencb(server, get_cb, nullptr);
// start event loop
event_dispatch();
evhttp_free(server);
return 0;
}