-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.cpp
40 lines (34 loc) · 1.07 KB
/
Router.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
#include "Router.h"
#include "WebServer.h"
//register a new route on the router
bool Router::route(String method, String path, rest_callback_t cb) {
if(_routeIndex < ROUTER_MAX_ROUTES) {
routes[_routeIndex] = Route { method, path, cb};
_routeIndex++;
}
return false;
};
//Helper to register new GET route
bool Router::get(String path, rest_callback_t cb) {
return route("GET", path, cb);
};
//Helper to register new POST route
bool Router::post(String path, rest_callback_t cb) {
return route("POST", path, cb);
};
// Handle an incoming WebRequest by calling the correct handler for the path
void Router::handle(WebRequest& req) {
// find matching route
for(int i = 0; i < _routeIndex; i++) {
Route r = routes[i];
if(r.method == req.method && r.path == req.path) {
WebResponse res = req.getResponse();
r.callback(req, res);
return;
}
}
Serial.println("No matching routes for request");
WebResponse res = req.getResponse();
res.status = HTTP_NOT_FOUND;
res.send();
};