Skip to content

Commit f2862f7

Browse files
committed
Material doc site
1 parent 4294283 commit f2862f7

19 files changed

Lines changed: 2477 additions & 0 deletions

.github/workflows/docs.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Publish Docs
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- "docs/**"
8+
- "mkdocs.yml"
9+
permissions:
10+
contents: write
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-python@v5
17+
with:
18+
python-version: 3.x
19+
- run: pip install mkdocs-material
20+
- run: mkdocs gh-deploy --force

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
/.pio
44
/.vscode
55
/logs
6+
/site
7+
/venv

docs/code_of_conduct.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
We as members, contributors, and leaders pledge to make participation in our
6+
community a harassment-free experience for everyone, regardless of age, body
7+
size, visible or invisible disability, ethnicity, sex characteristics, gender
8+
identity and expression, level of experience, education, socioeconomic status,
9+
nationality, personal appearance, race, religion, or sexual identity
10+
and orientation.
11+
12+
We pledge to act and interact in ways that contribute to an open, welcoming,
13+
diverse, inclusive, and healthy community.
14+
15+
## Our Standards
16+
17+
Examples of behavior that contributes to a positive environment for our
18+
community include:
19+
20+
- Demonstrating empathy and kindness toward other people
21+
- Being respectful of differing opinions, viewpoints, and experiences
22+
- Giving and gracefully accepting constructive feedback
23+
- Accepting responsibility and apologizing to those affected by our mistakes,
24+
and learning from the experience
25+
- Focusing on what is best not just for us as individuals, but for the
26+
overall community
27+
28+
Examples of unacceptable behavior include:
29+
30+
- The use of sexualized language or imagery, and sexual attention or
31+
advances of any kind
32+
- Trolling, insulting or derogatory comments, and personal or political attacks
33+
- Public or private harassment
34+
- Publishing others' private information, such as a physical or email
35+
address, without their explicit permission
36+
- Other conduct which could reasonably be considered inappropriate in a
37+
professional setting
38+
39+
## Enforcement Responsibilities
40+
41+
Community leaders are responsible for clarifying and enforcing our standards of
42+
acceptable behavior and will take appropriate and fair corrective action in
43+
response to any behavior that they deem inappropriate, threatening, offensive,
44+
or harmful.
45+
46+
Community leaders have the right and responsibility to remove, edit, or reject
47+
comments, commits, code, wiki edits, issues, and other contributions that are
48+
not aligned to this Code of Conduct, and will communicate reasons for moderation
49+
decisions when appropriate.
50+
51+
## Scope
52+
53+
This Code of Conduct applies within all community spaces, and also applies when
54+
an individual is officially representing the community in public spaces.
55+
Examples of representing our community include using an official e-mail address,
56+
posting via an official social media account, or acting as an appointed
57+
representative at an online or offline event.
58+
59+
## Enforcement
60+
61+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
62+
reported to the community leaders responsible for enforcement at
63+
https://sidweb.nl/cms3/en/contact.
64+
All complaints will be reviewed and investigated promptly and fairly.
65+
66+
All community leaders are obligated to respect the privacy and security of the
67+
reporter of any incident.
68+
69+
## Enforcement Guidelines
70+
71+
Community leaders will follow these Community Impact Guidelines in determining
72+
the consequences for any action they deem in violation of this Code of Conduct:
73+
74+
### 1. Correction
75+
76+
**Community Impact**: Use of inappropriate language or other behavior deemed
77+
unprofessional or unwelcome in the community.
78+
79+
**Consequence**: A private, written warning from community leaders, providing
80+
clarity around the nature of the violation and an explanation of why the
81+
behavior was inappropriate. A public apology may be requested.
82+
83+
### 2. Warning
84+
85+
**Community Impact**: A violation through a single incident or series
86+
of actions.
87+
88+
**Consequence**: A warning with consequences for continued behavior. No
89+
interaction with the people involved, including unsolicited interaction with
90+
those enforcing the Code of Conduct, for a specified period of time. This
91+
includes avoiding interactions in community spaces as well as external channels
92+
like social media. Violating these terms may lead to a temporary or
93+
permanent ban.
94+
95+
### 3. Temporary Ban
96+
97+
**Community Impact**: A serious violation of community standards, including
98+
sustained inappropriate behavior.
99+
100+
**Consequence**: A temporary ban from any sort of interaction or public
101+
communication with the community for a specified period of time. No public or
102+
private interaction with the people involved, including unsolicited interaction
103+
with those enforcing the Code of Conduct, is allowed during this period.
104+
Violating these terms may lead to a permanent ban.
105+
106+
### 4. Permanent Ban
107+
108+
**Community Impact**: Demonstrating a pattern of violation of community
109+
standards, including sustained inappropriate behavior, harassment of an
110+
individual, or aggression toward or disparagement of classes of individuals.
111+
112+
**Consequence**: A permanent ban from any sort of public interaction within
113+
the community.
114+
115+
## Attribution
116+
117+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118+
version 2.0, available at
119+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120+
121+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
122+
enforcement ladder](https://github.com/mozilla/diversity).
123+
124+
[homepage]: https://www.contributor-covenant.org
125+
126+
For answers to common questions about this code of conduct, see the FAQ at
127+
https://www.contributor-covenant.org/faq. Translations are available at
128+
https://www.contributor-covenant.org/translations.

docs/concepts.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Concepts
2+
3+
## Principles of operation
4+
5+
### The Async Web server
6+
7+
- Listens for connections
8+
- Wraps the new clients into `Request`
9+
- Keeps track of clients and cleans memory
10+
- Manages `Rewrites` and apply them on the request url
11+
- Manages `Handlers` and attaches them to Requests
12+
13+
### Request Life Cycle
14+
15+
- TCP connection is received by the server
16+
- The connection is wrapped inside `Request` object
17+
- When the request head is received (type, url, get params, http version and host),
18+
the server goes through all `Rewrites` (in the order they were added) to rewrite the url and inject query parameters,
19+
next, it goes through all attached `Handlers`(in the order they were added) trying to find one
20+
that `canHandle` the given request. If none are found, the default(catch-all) handler is attached.
21+
- The rest of the request is received, calling the `handleUpload` or `handleBody` methods of the `Handler` if they are needed (POST+File/Body)
22+
- When the whole request is parsed, the result is given to the `handleRequest` method of the `Handler` and is ready to be responded to
23+
- In the `handleRequest` method, to the `Request` is attached a `Response` object (see below) that will serve the response data back to the client
24+
- When the `Response` is sent, the client is closed and freed from the memory
25+
26+
### Rewrites and how do they work
27+
28+
- The `Rewrites` are used to rewrite the request url and/or inject get parameters for a specific request url path.
29+
- All `Rewrites` are evaluated on the request in the order they have been added to the server.
30+
- The `Rewrite` will change the request url only if the request url (excluding get parameters) is fully match
31+
the rewrite url, and when the optional `Filter` callback return true.
32+
- Setting a `Filter` to the `Rewrite` enables to control when to apply the rewrite, decision can be based on
33+
request url, http version, request host/port/target host, get parameters or the request client's localIP or remoteIP.
34+
- Two filter callbacks are provided: `ON_AP_FILTER` to execute the rewrite when request is made to the AP interface,
35+
`ON_STA_FILTER` to execute the rewrite when request is made to the STA interface.
36+
- The `Rewrite` can specify a target url with optional get parameters, e.g. `/to-url?with=params`
37+
38+
### Handlers and how do they work
39+
40+
- The `Handlers` are used for executing specific actions to particular requests
41+
- One `Handler` instance can be attached to any request and lives together with the server
42+
- Setting a `Filter` to the `Handler` enables to control when to apply the handler, decision can be based on
43+
request url, http version, request host/port/target host, get parameters or the request client's localIP or remoteIP.
44+
- Two filter callbacks are provided: `ON_AP_FILTER` to execute the rewrite when request is made to the AP interface,
45+
`ON_STA_FILTER` to execute the rewrite when request is made to the STA interface.
46+
- The `canHandle` method is used for handler specific control on whether the requests can be handled
47+
and for declaring any interesting headers that the `Request` should parse. Decision can be based on request
48+
method, request url, http version, request host/port/target host and get parameters
49+
- Once a `Handler` is attached to given `Request` (`canHandle` returned true)
50+
that `Handler` takes care to receive any file/data upload and attach a `Response`
51+
once the `Request` has been fully parsed
52+
- `Handlers` are evaluated in the order they are attached to the server. The `canHandle` is called only
53+
if the `Filter` that was set to the `Handler` return true.
54+
- The first `Handler` that can handle the request is selected, not further `Filter` and `canHandle` are called.
55+
56+
### Responses and how do they work
57+
58+
- The `Response` objects are used to send the response data back to the client
59+
- The `Response` object lives with the `Request` and is freed on end or disconnect
60+
- Different techniques are used depending on the response type to send the data in packets
61+
returning back almost immediately and sending the next packet when this one is received.
62+
Any time in between is spent to run the user loop and handle other network packets
63+
- Responding asynchronously is probably the most difficult thing for most to understand
64+
- Many different options exist for the user to make responding a background task
65+
66+
### Template processing
67+
68+
- ESPAsyncWebserver contains simple template processing engine.
69+
- Template processing can be added to most response types.
70+
- Currently it supports only replacing template placeholders with actual values. No conditional processing, cycles, etc.
71+
- Placeholders are delimited with `%` symbols. Like this: `%TEMPLATE_PLACEHOLDER%`.
72+
- It works by extracting placeholder name from response text and passing it to user provided function which should return actual value to be used instead of placeholder.
73+
- Since it's user provided function, it is possible for library users to implement conditional processing and cycles themselves.
74+
- Since it's impossible to know the actual response size after template processing step in advance (and, therefore, to include it in response headers), the response becomes [chunked](#chunked-response).

docs/configuration.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Configuration
2+
3+
## Important recommendations for build options
4+
5+
Most of the crashes are caused by improper use or configuration of the AsyncTCP library used for the project.
6+
Here are some recommendations to avoid them and build-time flags you can change.
7+
8+
- `CONFIG_ASYNC_TCP_MAX_ACK_TIME`: defines a timeout for TCP connection to be considered alive when waiting for data.
9+
In some bad network conditions you might consider increasing it.
10+
11+
- `CONFIG_ASYNC_TCP_QUEUE_SIZE`: defines the length of the queue for events related to connections handling.
12+
Both the server and AsyncTCP library were optimized to control the queue automatically. Do NOT try blindly increasing the queue size, it does not help you in a way you might think it is. If you receive debug messages about queue throttling, try to optimize your server callbacks code to execute as fast as possible.
13+
14+
- `CONFIG_ASYNC_TCP_RUNNING_CORE`: CPU core thread affinity that runs the queue events handling and executes server callbacks. Default is ANY core, so it means that for dualcore SoCs both cores could handle server activities. If your server's code is too heavy and unoptimized or you see that sometimes
15+
server might affect other network activities, you might consider to bind it to the same core that runs Arduino code (1) to minimize affect on radio part. Otherwise you can leave the default to let RTOS decide where to run the thread based on priority
16+
17+
- `CONFIG_ASYNC_TCP_STACK_SIZE`: stack size for the thread that runs sever events and callbacks. Default is 16k that is a way too much waste for well-defined short async code or simple static file handling. You might want to cosider reducing it to 4-8k to same RAM usage. If you do not know what this is or not sure about your callback code demands - leave it as default, should be enough even for very hungry callbacks in most cases.
18+
19+
- `ASYNCWEBSERVER_USE_CHUNK_INFLIGHT`: inflight control for chunked responses.
20+
If you need to serve chunk requests with a really low buffer (which should be avoided), you can set `-D ASYNCWEBSERVER_USE_CHUNK_INFLIGHT=0` to disable the in-flight control.
21+
22+
> [!NOTE]
23+
> This relates to ESP32 only, ESP8266 uses different ESPAsyncTCP lib that does not has this build options
24+
25+
Recommended configurations:
26+
27+
```c++
28+
-D CONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 // (keep default)
29+
-D CONFIG_ASYNC_TCP_PRIORITY=10 // (keep default)
30+
-D CONFIG_ASYNC_TCP_QUEUE_SIZE=64 // (keep default)
31+
-D CONFIG_ASYNC_TCP_RUNNING_CORE=1 // force async_tcp task to be on same core as Arduino app (default is any core)
32+
-D CONFIG_ASYNC_TCP_STACK_SIZE=4096 // reduce the stack size (default is 16K)
33+
```
34+
35+
## Important things to remember
36+
37+
- This is fully asynchronous server and as such does not run on the loop thread.
38+
- You can not use yield or delay or any function that uses them inside the callbacks
39+
- The server is smart enough to know when to close the connection and free resources
40+
- You can not send more than one response to a single request

docs/eventsource.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# EventSource
2+
3+
## Async Event Source Plugin
4+
5+
The server includes EventSource (Server-Sent Events) plugin which can be used to send short text events to the browser.
6+
Difference between EventSource and WebSockets is that EventSource is single direction, text-only protocol.
7+
8+
See the [ServerSentEvents example here](../examples/ServerSentEvents/ServerSentEvents.ino).
9+
10+
### Setup Event Source on the server
11+
12+
```cpp
13+
AsyncWebServer server(80);
14+
AsyncEventSource events("/events");
15+
16+
void setup(){
17+
// setup ......
18+
events.onConnect([](AsyncEventSourceClient *client){
19+
if(client->lastId()){
20+
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
21+
}
22+
//send event with message "hello!", id current millis
23+
// and set reconnect delay to 1 second
24+
client->send("hello!",NULL,millis(),1000);
25+
});
26+
27+
server.addHandler(&events);
28+
// setup ......
29+
}
30+
31+
void loop(){
32+
if(eventTriggered){ // your logic here
33+
//send event "myevent"
34+
events.send("my event content","myevent",millis());
35+
}
36+
}
37+
```
38+
39+
**IMPORTANT**: Use `AsyncAuthenticationMiddleware` instead of the deprecated `setAuthentication()` method for authentication.
40+
41+
```cpp
42+
AsyncAuthenticationMiddleware authMiddleware;
43+
authMiddleware.setAuthType(AsyncAuthType::AUTH_DIGEST);
44+
authMiddleware.setRealm("My app name");
45+
authMiddleware.setUsername("admin");
46+
authMiddleware.setPassword("admin");
47+
authMiddleware.generateHash();
48+
49+
events.addMiddleware(&authMiddleware);
50+
```
51+
52+
### Setup Event Source in the browser
53+
54+
```javascript
55+
if (!!window.EventSource) {
56+
var source = new EventSource("/events");
57+
58+
source.addEventListener(
59+
"open",
60+
function (e) {
61+
console.log("Events Connected");
62+
},
63+
false,
64+
);
65+
66+
source.addEventListener(
67+
"error",
68+
function (e) {
69+
if (e.target.readyState != EventSource.OPEN) {
70+
console.log("Events Disconnected");
71+
}
72+
},
73+
false,
74+
);
75+
76+
source.addEventListener(
77+
"message",
78+
function (e) {
79+
console.log("message", e.data);
80+
},
81+
false,
82+
);
83+
84+
source.addEventListener(
85+
"myevent",
86+
function (e) {
87+
console.log("myevent", e.data);
88+
},
89+
false,
90+
);
91+
}
92+
```
93+

0 commit comments

Comments
 (0)