-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (53 loc) · 1.68 KB
/
main.cpp
File metadata and controls
64 lines (53 loc) · 1.68 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
#include "http.hpp"
#include "router.hpp"
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace http;
void hello_world(Request req, Response *res) {
res->set_payload("Hello World!");
res->set_content_type("text/plain");
}
static Router router(8181);
void quit(int s) {
router.stop();
std::cout << "Stopping Server" << std::endl;
}
int main() {
struct sigaction sig_int_handler;
sig_int_handler.sa_handler = quit;
sigemptyset(&sig_int_handler.sa_mask);
sig_int_handler.sa_flags = 0;
sigaction(SIGINT, &sig_int_handler, nullptr);
// Allow all Methods
router.handle("GET /helloWorld", hello_world);
router.handle("GET /healthz", [](Request req, Response *res) {
res->set_status_code(StatusCode::OK);
res->set_payload(std::vector<std::byte>());
res->set_content_type("text/plain");
});
// Only allow GET
router.handle("GET /echo/{name}", [](Request req, Response *res) {
std::string name = req.path.get("name").value_or("No Name given");
res->set_payload("Hello " + name);
res->set_content_type("text/plain");
});
// Only allow POST
router.handle("POST /echo/{name}", [](Request req, Response *res) {
std::string name = req.path.get("name").value_or("No Name given");
res->set_payload("Hello with Post" + name);
res->set_content_type("text/plain");
});
router.handle("GET /", [](Request req, Response *res) {
res->set_payload("Main");
res->set_content_type("text/plain");
});
std::cout << "Starting Server" << std::endl;
int err = router.start();
if (err != 0) {
std::cout << "Error starting the Server with: " << strerror(errno)
<< std::endl;
}
return err;
}