Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·79 lines (69 loc) · 1.95 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·79 lines (69 loc) · 1.95 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
#include <stdio.h>
#include <iostream>
#include <string>
#include <functional>
#include <memory>
#include <climits>
#include "eventloop.h"
#include "address.h"
#include "httpserver.h"
#include "httprequest.h"
#include "httpresponse.h"
#include "httpresponsefile.h"
#include "logging.h"
#include "asynclogging.h"
using namespace tiny_muduo;
using tiny_muduo::Method;
using tiny_muduo::HttpStatusCode;
std::unique_ptr<AsyncLogging> asynclog;
extern void SetOutputFunc(Logger::OutputFunc);
extern void SetFlushFunc(Logger::FlushFunc);
void AsyncOutputFunc(const char* data, int len) {
asynclog->Append(data, len);
}
void AsyncFlushFunc() {
asynclog->Flush();
}
void HttpResponseCallback(const HttpRequest& request, HttpResponse& response) {
if (request.method() != kGet) {
response.SetStatusCode(k400BadRequest);
response.SetStatusMessage("Bad Request");
response.SetCloseConnection(true);
return;
}
{
const string& path = request.path();
if (path == "/") {
response.SetStatusCode(k200OK);
response.SetBodyType("text/html");
response.SetBody(love6_website);
} else if (path == "/hello") {
response.SetStatusCode(k200OK);
response.SetBodyType("text/html");
response.SetBody("Hello, world!\n");
} else if (path == "/favicon.ico" || path == "/favicon") {
response.SetStatusCode(k200OK);
response.SetBodyType("image/png");
response.SetBody(string(favicon, sizeof(favicon)));
} else {
response.SetStatusCode(k404NotFound);
response.SetStatusMessage("Not Found");
response.SetCloseConnection(true);
return;
}
}
}
int main(int argc, char* argv[]) {
if (argc <= 1) {
printf("Usage: %s port\n", argv[0]);
return 0;
}
EventLoop loop;
Address listen_address(argv[1]);
HttpServer server(&loop, listen_address);
server.SetThreadNums(4);
server.SetHttpResponseCallback(HttpResponseCallback);
server.Start();
loop.Loop();
return 0;
}