Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
93 lines (75 loc) · 2.78 KB
/
Copy pathmain.cpp
File metadata and controls
93 lines (75 loc) · 2.78 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Include files
#include <expresso/core/server.h>
#include <expresso/middleware/cookie_parser.h>
#include <expresso/middleware/cors.h>
#include <expresso/middleware/static_serve.h>
#include <expresso/utils/process.h>
// Personally, I don't encourange using namespaces, but, I left it here just so
// that the code could be more readable ¯\_(ツ)_/¯
using namespace expresso::core;
using namespace expresso::middleware;
using namespace expresso::utils;
// Global variable, just for fun :)
int port;
// Handler to handle the `GET` request on `/hello/world`
void helloWorldHandler(Request &req, Response &res) {
json::object resp;
resp["framework"] = "Expresso";
resp["repository"] = "https://github.com/coding-cpp/expresso";
resp["language"] = "C++";
resp["creator"] = "Adit Jain";
resp["submodules"].resize(3);
resp["submodules"][0]["repository"] = "https://github.com/coding-cpp/logger";
resp["submodules"][0]["work"] = "Logging library for C++";
resp["submodules"][0]["location"] = "lib/logger";
resp["submodules"][0]["name"] = "Logger";
resp["submodules"][1]["repository"] = "https://github.com/coding-cpp/nexus";
resp["submodules"][1]["work"] = "Thread pool library for C++";
resp["submodules"][1]["location"] = "lib/nexus";
resp["submodules"][1]["name"] = "Nexus";
resp["submodules"][2]["repository"] = "https://github.com/coding-cpp/json";
resp["submodules"][2]["work"] = "JSON library for C++";
resp["submodules"][2]["location"] = "lib/json";
resp["submodules"][2]["name"] = "JSON";
res.json(resp).status(200);
return;
}
int main(int argc, char **argv) {
// `Process` class to load in the environment variables
Process process("../.env");
port = std::stoi(process.getEnv("PORT"));
// Running a server with max 10 connections and 4 threads
Server app(10000, 100);
// Router, for routing requests starting with /hello
Router world;
// Cors middleware
Cors cors;
cors.allowOrigin("*.app.localhost");
cors.allowCredentials(true);
// app.use(&cors);
// CookieParser middleware
CookieParser cookieParser;
app.use(&cookieParser);
// StaticServe middleware
StaticServe staticServe("../assets");
app.use(&staticServe);
// Routing demo
world.get("/world", helloWorldHandler);
app.use("/hello", &world);
// Direct usage demo
// response to GET request on /hello
app.get("/hello", [](Request &req, Response &res) {
res.status(StatusCode::OK).send("Hello!");
return;
});
// Example sending file in response to GET request on /expresso
app.get("/expresso", [](Request &req, Response &res) {
res.sendFile("../assets/coffee.png");
return;
});
// Listening on port with optional callback function
app.listen(port, []() {
logger::success("Listening on port " + std::to_string(port));
});
return EXIT_SUCCESS;
}