You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[lib/framework/](lib/framework) | C++ back end for the ESP8266 device
47
48
48
49
### Building the firmware
49
50
@@ -247,13 +248,92 @@ There is also a manifest file which contains the app name to use when adding the
247
248
}
248
249
```
249
250
250
-
## Back End Overview
251
+
## Back end overview
251
252
252
-
The back end is a set of REST endpoints hosted by a [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) instance. The source is split up by feature, for example [WiFiScanner.h](src/WiFiScanner.h) implements the end points for scanning for available networks.
253
+
The back end is a set of REST endpoints hosted by a [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) instance. The ['lib/framework'](lib/framework) directory contains the majority of the back end code. The framework contains of a number of useful utility classes which you can use when extending it. The project also comes with a demo project to give you some help getting started.
253
254
254
-
There is an abstract class [SettingsService.h](src/SettingsService.h)that provides an easy means of adding configurable services/features to the device. It takes care of writing the settings as JSON to SPIFFS. All you need to do is extend the class with your required configuration and implement the functions which serialize the settings to/from JSON. JSON serialization utilizes the excellent [ArduinoJson](https://github.com/bblanchon/ArduinoJson) library.
255
+
The framework's source is split up by feature, for example [WiFiScanner.h](lib/framework/WiFiScanner.h)implements the end points for scanning for available networks where as [WiFiSettingsService.h](lib/framework/WiFiSettingsService.h) handles configuring the WiFi settings and managing the WiFi connection.
255
256
256
-
Here is a example of a service with username and password settings:
257
+
### Initializing the framework
258
+
259
+
The ['src/main.cpp'](src/main.cpp) file constructs the webserver and initializes the framework. You can add endpoints to the server here to support your IoT project. The main loop is also accessable so you can run your own code easily.
260
+
261
+
The following code creates the web server, esp8266React framework and the demo project instance:
Now in the `setup()` function the initialization is performed:
270
+
271
+
```cpp
272
+
void setup() {
273
+
// start serial and filesystem
274
+
Serial.begin(SERIAL_BAUD_RATE);
275
+
276
+
// start the file system (must be done before starting the framework)
277
+
SPIFFS.begin();
278
+
279
+
// start the framework and demo project
280
+
esp8266React.begin();
281
+
282
+
// start the demo project
283
+
demoProject.begin();
284
+
285
+
// start the server
286
+
server.begin();
287
+
}
288
+
```
289
+
290
+
Finally the loop calls the framework's loop function to service the frameworks features. You can add your own code in here, as shown with the demo project:
291
+
292
+
```cpp
293
+
voidloop() {
294
+
// run the framework's loop function
295
+
esp8266React.loop();
296
+
297
+
// run the demo project's loop function
298
+
demoProject.loop();
299
+
}
300
+
```
301
+
302
+
### Adding endpoints
303
+
304
+
There are some simple classes that support adding configurable services/features to the device:
305
+
306
+
Class | Description
307
+
----- | -----------
308
+
[SimpleService.h](lib/framework/SimpleService.h) | Exposes an endpoint to read and write settings as JSON. Extend this class and implement the functions which serialize the settings to/from JSON.
309
+
[SettingsService.h](lib/framework/SettingsService.h) | As above, however this class also handles persisting the settings as JSON to the file system.
310
+
[AdminSettingsService.h](lib/framework/AdminSettingsService.h) | Extends SettingsService to secure the endpoint to administrators only, the authentication predicate can be overridden if required.
311
+
312
+
The demo project shows how these can be used, explore the framework classes for more examples.
313
+
314
+
### Security features
315
+
316
+
The framework has security features to prevent unauthorized use of the device. This is driven by [SecurityManager.h](lib/framework/SecurityManager.h).
317
+
318
+
On successful authentication, the /rest/signIn endpoint issues a JWT which is then sent using Bearer Authentication. The framework come with built in predicates for verifying a users access level. The built in AuthenticationPredicates can be found in [SecurityManager.h](lib/framework/SecurityManager.h):
319
+
320
+
Predicate | Description
321
+
-------------------- | -----------
322
+
NONE_REQUIRED | No authentication is required.
323
+
IS_AUTHENTICATED | Any authenticated principal is permitted.
324
+
IS_ADMIN | The authenticated principal must be an admin.
325
+
326
+
You can use the security manager to wrap any web handler with an authentication predicate:
Alternatively you can extend [AdminSettingsService.h](lib/framework/AdminSettingsService.h) and optionally override `getAuthenticationPredicate()` to secure an endpoint.
0 commit comments