-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
69 lines (59 loc) · 2.25 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <csignal>
#include <vector>
#include "src/api/testing.cpp"
#include "src/server/server_lib.cpp"
#include <chrono>
sockaddr_in ServerAddr = serverlib::createServerAddress(8080, "87.106.130.229");
Server App(ServerAddr);
void handle_signal(int signal)
{
std::cout << "Handling signal!!" << std::endl;
App.Stop();
}
int main()
{
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
auto t1 = high_resolution_clock::now();
signal(SIGABRT, handle_signal);
serverlib::Static s("/", "./public");
App.Get("/", [](serverlib::Request& Req, serverlib::Response& Res){
std::cout << "app.get('/'')" << std::endl;
Res.SetStatus(serverlib::HTTP_OK);
Res.SetContentType(serverlib::TEXT_HTML);
Res.SetBody(file_parser::FileParser::GetHTMLPage("./public/index.html"));
Res.Send();
});
App.Post("/post", [](serverlib::Request& Req, serverlib::Response& Res){
Res.SetStatus(serverlib::HTTP_OK);
Res.SetContentType(serverlib::TEXT_JSON);
Res.SetBody("{'response': 'Succes'}");
Res.Send();
});
App.Get("/testing", [](serverlib::Request& Req, serverlib::Response& Res) {
Res.SetStatus(Enums::HTTP_OK);
Res.SetContentType(Enums::TEXT_HTML);
Res.SetBody("<h1>testing path lol lmao pls work</h1>");
Res.Send();
});
serverlib::Router static_router = serverlib::Serve_Static(s);
std::cout << "static router memaddr: " << &static_router << std::endl;
App.Use("/", static_router);
serverlib::Router TestRouter = TestingApi::GetRouter();
App.Use("/testing", TestRouter);
auto t2 = high_resolution_clock::now();
/* Getting number of milliseconds as an integer. */
auto ms_int = duration_cast<milliseconds>(t2 - t1);
/* Getting number of milliseconds as a double. */
duration<double, std::milli> ms_double = t2 - t1;
std::cout << "Server intitialisation took: " << ms_int.count() << "ms" << std::endl;
std::cout << "Server intitialisation took: " << ms_double.count() << "ms" << std::endl;
App.Start();
}