Skip to content

Commit

Permalink
Merge pull request #80 from coding-cpp/fix/79
Browse files Browse the repository at this point in the history
use smart pointers for middlewares
  • Loading branch information
Jadit19 authored Sep 5, 2024
2 parents ee8af2e + 7cab547 commit 3370cac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
24 changes: 14 additions & 10 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// Personally, I don't encourange using namespaces, but, I left it here just so
// that the code could be more readable ¯\_(ツ)_/¯
using namespace std;
using namespace expresso::core;
using namespace expresso::enums;
using namespace expresso::middleware;
Expand Down Expand Up @@ -66,18 +67,20 @@ int main(int argc, char **argv) {
Server app = Server();

// CORS middleware
Cors cors;
cors.allowOrigin("*");
cors.allowCredentials(true);
app.use(&cors);
unique_ptr<expresso::middleware::Cors> cors = make_unique<Cors>();
cors->allowOrigin("*");
cors->allowCredentials(true);
app.use(move(cors));

// Cookie Parser
CookieParser cookieParser;
app.use(&cookieParser);
unique_ptr<expresso::middleware::CookieParser> cookieParser =
make_unique<CookieParser>();
app.use(move(cookieParser));

// Static serve middleware
StaticServe staticServe("../assets");
app.use(&staticServe);
unique_ptr<expresso::middleware::StaticServe> staticServe =
make_unique<StaticServe>("../assets");
app.use(move(staticServe));

// Route handling like normal
app.get("/health", [](Request &req, Response &res) {
Expand All @@ -90,9 +93,10 @@ int main(int argc, char **argv) {
app.use("/about", &router);

// Listing directories
StaticServe pictureServe("../assets/github", true);
unique_ptr<expresso::middleware::StaticServe> pictureServe =
make_unique<StaticServe>("../assets/github", true);
Router pictureRouter;
pictureRouter.use(&pictureServe);
pictureRouter.use(move(pictureServe));
app.use("/pictures", &pictureRouter);

// Sending multiple files as single zip
Expand Down
4 changes: 2 additions & 2 deletions include/expresso/core/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Router {
Router *paramRouter;
std::string paramRouterParam;

std::vector<expresso::middleware::Middleware *> middlewares;
std::vector<std::unique_ptr<expresso::middleware::Middleware>> middlewares;

bool handleMiddlewares(Request &request, Response &response);

Expand All @@ -44,7 +44,7 @@ class Router {
void (*handler)(Request &request, Response &response));

void use(std::string path, Router *router);
void use(expresso::middleware::Middleware *middleware);
void use(std::unique_ptr<expresso::middleware::Middleware> middleware);

void handleRequest(Request &request, Response &response);
};
Expand Down
8 changes: 5 additions & 3 deletions src/core/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ void expresso::core::Router::use(std::string path, Router *router) {
return;
}

void expresso::core::Router::use(expresso::middleware::Middleware *middleware) {
this->middlewares.push_back(middleware);
void expresso::core::Router::use(
std::unique_ptr<expresso::middleware::Middleware> middleware) {
this->middlewares.push_back(std::move(middleware));
return;
}

Expand Down Expand Up @@ -202,7 +203,8 @@ void expresso::core::Router::handleRequest(Request &request,

bool expresso::core::Router::handleMiddlewares(Request &request,
Response &response) {
for (expresso::middleware::Middleware *middleware : this->middlewares) {
for (const std::unique_ptr<expresso::middleware::Middleware> &middleware :
this->middlewares) {
if (!middleware->use(request, response)) {
response.end();
return false;
Expand Down

0 comments on commit 3370cac

Please sign in to comment.