Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (57 loc) · 1.89 KB
/
Copy pathmain.cpp
File metadata and controls
71 lines (57 loc) · 1.89 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
// 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) {
res.status(StatusCode::OK).send("Hello World!");
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 10 connection request queueable
Server app(10);
// 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, []() {
print::success("Listening on port " + std::to_string(port));
});
return EXIT_SUCCESS;
}