Skip to content

Commit 112b97c

Browse files
authored
Optional std function middleware
1 parent 5057638 commit 112b97c

File tree

6 files changed

+151
-78
lines changed

6 files changed

+151
-78
lines changed
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
compile:
2+
platforms:
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#if defined(ESP8266)
2+
#include <ESP8266WiFi.h>
3+
#else
4+
#include <WiFi.h>
5+
#endif
6+
7+
// Manually add "#define STD_FUNCTION_MIDDLEWARE" to the top of the aWOT.h file
8+
#include <aWOT.h>
9+
10+
#define WIFI_SSID ""
11+
#define WIFI_PASSWORD ""
12+
13+
WiFiServer server(80);
14+
Application app;
15+
16+
class Counter {
17+
public:
18+
Counter(int initialCount) {
19+
_count = initialCount;
20+
}
21+
22+
void handleRequest(Request &req, Response &res) {
23+
_count++;
24+
res.print("I have been called ");
25+
res.print(_count);
26+
res.print(" times!");
27+
}
28+
29+
private:
30+
int _count = 0;
31+
};
32+
33+
Counter counter(666);
34+
35+
void callback(Request &req, Response &res) {
36+
res.print("Hello World!");
37+
}
38+
39+
void setup() {
40+
Serial.begin(115200);
41+
42+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
43+
while (WiFi.status() != WL_CONNECTED) {
44+
delay(500);
45+
Serial.print(".");
46+
}
47+
Serial.println(WiFi.localIP());
48+
49+
50+
app.get("/", std::bind(&Counter::handleRequest, &counter, std::placeholders::_1, std::placeholders::_2));
51+
52+
// Do not add '&' in front of the parameter when STD_FUNCTION_MIDDLEWARE has been defined.
53+
app.get("/callback", callback);
54+
server.begin();
55+
}
56+
57+
void loop() {
58+
WiFiClient client = server.available();
59+
60+
if (client.connected()) {
61+
app.process(&client);
62+
client.stop();
63+
}
64+
}

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aWOT",
3-
"version": "3.4.0",
3+
"version": "3.5.0",
44
"keywords": "wifi, ethernet, http, web, server, rest",
55
"description": "aWOT is a web server library compatible with multiple different board architectures and networking solutions.",
66
"license": "MIT",

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=aWOT
2-
version=3.4.0
2+
version=3.5.0
33
author=Lasse Lukkari <lasse.lukkari@gmail.com>
44
maintainer=Lasse Lukkari <lasse.lukkari@gmail.com>
55
sentence=Arduino web server library.

src/aWOT.cpp

+35-36
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ Response::Response(Client* client, uint8_t * writeBuffer, int writeBufferLength)
3333
m_sendingStatus(false),
3434
m_sendingHeaders(false),
3535
m_headersCount(0),
36-
m_mime(NULL),
3736
m_bytesSent(0),
3837
m_ended(false),
3938
m_buffer(writeBuffer),
@@ -1283,67 +1282,67 @@ Router::~Router() {
12831282
m_head = NULL;
12841283
}
12851284

1286-
void Router::del(const char *path, Middleware *middleware) {
1285+
void Router::del(const char *path, MIDDLEWARE_PARAM middleware) {
12871286
m_addMiddleware(Request::DELETE, path, middleware);
12881287
}
12891288

1290-
void Router::del(Middleware *middleware) {
1289+
void Router::del(MIDDLEWARE_PARAM middleware) {
12911290
del(NULL, middleware);
12921291
}
12931292

1294-
void Router::get(const char *path, Middleware *middleware) {
1293+
void Router::get(const char *path, MIDDLEWARE_PARAM middleware) {
12951294
m_addMiddleware(Request::GET, path, middleware);
12961295
}
12971296

1298-
void Router::get(Middleware *middleware) {
1297+
void Router::get(MIDDLEWARE_PARAM middleware) {
12991298
get(NULL, middleware);
13001299
}
13011300

1302-
void Router::head(const char *path, Middleware *middleware) {
1301+
void Router::head(const char *path, MIDDLEWARE_PARAM middleware) {
13031302
m_addMiddleware(Request::HEAD, path, middleware);
13041303
}
13051304

1306-
void Router::head(Middleware *middleware) {
1305+
void Router::head(MIDDLEWARE_PARAM middleware) {
13071306
head(NULL, middleware);
13081307
}
13091308

1310-
void Router::options(const char *path, Middleware *middleware) {
1309+
void Router::options(const char *path, MIDDLEWARE_PARAM middleware) {
13111310
m_addMiddleware(Request::OPTIONS, path, middleware);
13121311
}
13131312

1314-
void Router::options(Middleware *middleware) {
1313+
void Router::options(MIDDLEWARE_PARAM middleware) {
13151314
options(NULL, middleware);
13161315
}
13171316

1318-
void Router::post(const char *path, Middleware *middleware) {
1317+
void Router::post(const char *path, MIDDLEWARE_PARAM middleware) {
13191318
m_addMiddleware(Request::POST, path, middleware);
13201319
}
13211320

1322-
void Router::post(Middleware *middleware) {
1321+
void Router::post(MIDDLEWARE_PARAM middleware) {
13231322
post(NULL, middleware);
13241323
}
13251324

1326-
void Router::put(const char *path, Middleware *middleware) {
1325+
void Router::put(const char *path, MIDDLEWARE_PARAM middleware) {
13271326
m_addMiddleware(Request::PUT, path, middleware);
13281327
}
13291328

1330-
void Router::put(Middleware *middleware) {
1329+
void Router::put(MIDDLEWARE_PARAM middleware) {
13311330
put(NULL, middleware);
13321331
}
13331332

1334-
void Router::patch(const char *path, Middleware *middleware) {
1333+
void Router::patch(const char *path, MIDDLEWARE_PARAM middleware) {
13351334
m_addMiddleware(Request::PATCH, path, middleware);
13361335
}
13371336

1338-
void Router::patch(Middleware *middleware) {
1337+
void Router::patch(MIDDLEWARE_PARAM middleware) {
13391338
patch(NULL, middleware);
13401339
}
13411340

1342-
void Router::use(const char *path, Middleware *middleware) {
1341+
void Router::use(const char *path, MIDDLEWARE_PARAM middleware) {
13431342
m_addMiddleware(Request::ALL, path, middleware);
13441343
}
13451344

1346-
void Router::use(Middleware *middleware) {
1345+
void Router::use(MIDDLEWARE_PARAM middleware) {
13471346
use(NULL, middleware);
13481347
}
13491348

@@ -1361,7 +1360,7 @@ void Router::use(Router *router) {
13611360
}
13621361

13631362
void Router::m_addMiddleware(Request::MethodType type, const char *path,
1364-
Middleware *middleware) {
1363+
MIDDLEWARE_PARAM middleware) {
13651364
MiddlewareNode *tail = new MiddlewareNode();
13661365
tail->path = path;
13671366
tail->middleware = middleware;
@@ -1507,67 +1506,67 @@ Application::~Application() {
15071506
m_headerTail = NULL;
15081507
}
15091508

1510-
void Application::del(const char *path, Router::Middleware *middleware) {
1509+
void Application::del(const char *path, Router::MIDDLEWARE_PARAM middleware) {
15111510
m_defaultRouter.m_addMiddleware(Request::DELETE, path, middleware);
15121511
}
15131512

1514-
void Application::del(Router::Middleware *middleware) {
1513+
void Application::del(Router::MIDDLEWARE_PARAM middleware) {
15151514
del(NULL, middleware);
15161515
}
15171516

1518-
void Application::finally(Router::Middleware *final) {
1517+
void Application::finally(Router::MIDDLEWARE_PARAM final) {
15191518
m_final = final;
15201519
}
15211520

1522-
void Application::get(const char *path, Router::Middleware *middleware) {
1521+
void Application::get(const char *path, Router::MIDDLEWARE_PARAM middleware) {
15231522
m_defaultRouter.m_addMiddleware(Request::GET, path, middleware);
15241523
}
15251524

1526-
void Application::get(Router::Middleware *middleware) {
1525+
void Application::get(Router::MIDDLEWARE_PARAM middleware) {
15271526
get(NULL, middleware);
15281527
}
15291528

1530-
void Application::head(const char *path, Router::Middleware *middleware) {
1529+
void Application::head(const char *path, Router::MIDDLEWARE_PARAM middleware) {
15311530
m_defaultRouter.m_addMiddleware(Request::HEAD, path, middleware);
15321531
}
15331532

1534-
void Application::head(Router::Middleware *middleware) {
1533+
void Application::head(Router::MIDDLEWARE_PARAM middleware) {
15351534
head(NULL, middleware);
15361535
}
15371536

1538-
void Application::notFound(Router::Middleware *notFound) {
1537+
void Application::notFound(Router::MIDDLEWARE_PARAM notFound) {
15391538
m_notFound = notFound;
15401539
}
15411540

1542-
void Application::options(const char *path, Router::Middleware *middleware) {
1541+
void Application::options(const char *path, Router::MIDDLEWARE_PARAM middleware) {
15431542
m_defaultRouter.m_addMiddleware(Request::OPTIONS, path, middleware);
15441543
}
15451544

1546-
void Application::options(Router::Middleware *middleware) {
1545+
void Application::options(Router::MIDDLEWARE_PARAM middleware) {
15471546
options(NULL, middleware);
15481547
}
15491548

1550-
void Application::patch(const char *path, Router::Middleware *middleware) {
1549+
void Application::patch(const char *path, Router::MIDDLEWARE_PARAM middleware) {
15511550
m_defaultRouter.m_addMiddleware(Request::PATCH, path, middleware);
15521551
}
15531552

1554-
void Application::patch(Router::Middleware *middleware) {
1553+
void Application::patch(Router::MIDDLEWARE_PARAM middleware) {
15551554
patch(NULL, middleware);
15561555
}
15571556

1558-
void Application::post(const char *path, Router::Middleware *middleware) {
1557+
void Application::post(const char *path, Router::MIDDLEWARE_PARAM middleware) {
15591558
m_defaultRouter.m_addMiddleware(Request::POST, path, middleware);
15601559
}
15611560

1562-
void Application::post(Router::Middleware *middleware) {
1561+
void Application::post(Router::MIDDLEWARE_PARAM middleware) {
15631562
post(NULL, middleware);
15641563
}
15651564

1566-
void Application::put(const char *path, Router::Middleware *middleware) {
1565+
void Application::put(const char *path, Router::MIDDLEWARE_PARAM middleware) {
15671566
m_defaultRouter.m_addMiddleware(Request::PUT, path, middleware);
15681567
}
15691568

1570-
void Application::put(Router::Middleware *middleware) {
1569+
void Application::put(Router::MIDDLEWARE_PARAM middleware) {
15711570
put(NULL, middleware);
15721571
}
15731572

@@ -1641,11 +1640,11 @@ void Application::process(Stream *stream, char *urlBuffer, int urlBufferLength,
16411640
process(&client, urlBuffer, urlBufferLength, writeBuffer, writeBufferLength, context);
16421641
}
16431642

1644-
void Application::use(const char *path, Router::Middleware *middleware) {
1643+
void Application::use(const char *path, Router::MIDDLEWARE_PARAM middleware) {
16451644
m_defaultRouter.m_addMiddleware(Request::ALL, path, middleware);
16461645
}
16471646

1648-
void Application::use(Router::Middleware *middleware) {
1647+
void Application::use(Router::MIDDLEWARE_PARAM middleware) {
16491648
use(NULL, middleware);
16501649
}
16511650

0 commit comments

Comments
 (0)