11Simple Test
2- -------------------
2+ -----------
3+
4+ This is the minimal example of using the library.
5+ This example is serving a simple static text message.
6+
7+ It also manually connects to the WiFi network.
8+
9+ .. literalinclude :: ../examples/httpserver_simpletest_manual.py
10+ :caption: examples/httpserver_simpletest_manual.py
11+ :linenos:
12+
13+ Although there is nothing wrong with this approach, from the version 8.0.0 of CircuitPython,
14+ `it is possible to use the environment variables <https://docs.circuitpython.org/en/latest/docs/environment.html#circuitpython-behavior >`_
15+ defined in ``settings.toml `` file to store secrets and configure the WiFi network.
16+
17+ This is the same example as above, but it uses the ``settings.toml `` file to configure the WiFi network.
18+
19+ **From now on, all the examples will use the ** ``settings.toml `` **file to configure the WiFi network. **
20+
21+ .. literalinclude :: ../examples/settings.toml
22+ :caption: settings.toml
23+ :linenos:
324
4- Serving a simple static text message .
25+ Note that we still need to import `` socketpool `` and `` wifi `` modules .
526
6- .. literalinclude :: ../examples/httpserver_simpletest .py
7- :caption: examples/httpserver_simpletest .py
27+ .. literalinclude :: ../examples/httpserver_simpletest_auto .py
28+ :caption: examples/httpserver_simpletest_auto .py
829 :linenos:
930
31+ Serving static files
32+ --------------------
33+
34+ It is possible to serve static files from the filesystem.
35+ In this example we are serving files from the ``/static `` directory.
36+
37+ In order to save memory, we are unregistering unused MIME types and registering additional ones.
38+ `More about MIME types. <https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types >`_
39+
40+ .. literalinclude :: ../examples/httpserver_static_files_serving.py
41+ :caption: examples/httpserver_static_files_serving.py
42+ :linenos:
43+
44+ You can also serve a specific file from the handler.
45+ By default ``Response.send_file() `` looks for the file in the server's ``root_path `` directory, but you can change it.
46+
47+ .. literalinclude :: ../examples/httpserver_handler_serves_file.py
48+ :caption: examples/httpserver_handler_serves_file.py
49+ :linenos:
50+
51+ Tasks in the background
52+ -----------------------
53+
1054If you want your code to do more than just serve web pages,
1155use the ``.start() ``/``.poll() `` methods as shown in this example.
1256
1357Between calling ``.poll() `` you can do something useful,
1458for example read a sensor and capture an average or
1559a running total of the last 10 samples.
1660
17- .. literalinclude :: ../examples/httpserver_simple_poll .py
18- :caption: examples/httpserver_simple_poll .py
61+ .. literalinclude :: ../examples/httpserver_start_and_poll .py
62+ :caption: examples/httpserver_start_and_poll .py
1963 :linenos:
2064
2165Server with MDNS
@@ -30,6 +74,22 @@ In this example, the server is accessible via ``http://custom-mdns-hostname/`` a
3074 :caption: examples/httpserver_mdns.py
3175 :linenos:
3276
77+ Handling different methods
78+ ---------------------------------------
79+
80+ On every ``server.route() `` call you can specify which HTTP methods are allowed.
81+ By default, only ``GET `` method is allowed.
82+
83+ You can pass a list of methods or a single method as a string.
84+
85+ It is recommended to use the the values in ``adafruit_httpserver.methods `` module to avoid typos and for future proofness.
86+
87+ In example below, handler for ``/api `` route will be called when any of ``GET ``, ``POST ``, ``PUT ``, ``DELETE `` methods is used.
88+
89+ .. literalinclude :: ../examples/httpserver_methods.py
90+ :caption: examples/httpserver_methods.py
91+ :linenos:
92+
3393Change NeoPixel color
3494---------------------
3595
@@ -45,7 +105,7 @@ Tested on ESP32-S2 Feather.
45105 :linenos:
46106
47107Get CPU information
48- ---------------------
108+ -------------------
49109
50110You can return data from sensors or any computed value as JSON.
51111That makes it easy to use the data in other applications.
@@ -55,23 +115,23 @@ That makes it easy to use the data in other applications.
55115 :linenos:
56116
57117Chunked response
58- ---------------------
118+ ----------------
59119
60120Library supports chunked responses. This is useful for streaming data.
61- To use it, you need to set the ``chunked=True `` when creating a ``HTTPResponse `` object.
121+ To use it, you need to set the ``chunked=True `` when creating a ``Response `` object.
62122
63123.. literalinclude :: ../examples/httpserver_chunked.py
64124 :caption: examples/httpserver_chunked.py
65125 :linenos:
66126
67127URL parameters
68- ---------------------
128+ --------------
69129
70130Alternatively to using query parameters, you can use URL parameters.
71131
72- In order to use URL parameters, you need to wrap them inside ``<> `` in ``HTTPServer .route ``, e.g. ``<my_parameter> ``.
132+ In order to use URL parameters, you need to wrap them inside ``<> `` in ``Server .route ``, e.g. ``<my_parameter> ``.
73133
74- All URL parameters are **passed as positional (not keyword) arguments ** to the handler function, in order they are specified in `` HTTPServer.route `` .
134+ All URL parameters values are **passed as keyword arguments ** to the handler function.
75135
76136Notice how the handler function in example below accepts two additional arguments : ``device_id `` and ``action ``.
77137
@@ -80,11 +140,26 @@ make sure to add default values for all the ones that might not be passed.
80140In the example below the second route has only one URL parameter, so the ``action `` parameter has a default value.
81141
82142Keep in mind that URL parameters are always passed as strings, so you need to convert them to the desired type.
83- Also note that the names of the function parameters **do not have to match ** with the ones used in route, but they **must ** be in the same order.
84- Look at the example below to see how the ``route_param_1 `` and ``route_param_1 `` are named differently in the handler function.
85-
86- Although it is possible, it makes more sense be consistent with the names of the parameters in the route and in the handler function.
143+ Also note that the names of the function parameters **have to match ** with the ones used in route, but they **do not have to ** be in the same order.
87144
88145.. literalinclude :: ../examples/httpserver_url_parameters.py
89146 :caption: examples/httpserver_url_parameters.py
90147 :linenos:
148+
149+ Authentication
150+ --------------
151+
152+ In order to increase security of your server, you can use ``Basic `` and ``Bearer `` authentication.
153+
154+ If you want to apply authentication to the whole server, you need to call ``.require_authentication `` on ``Server `` instance.
155+
156+ .. literalinclude :: ../examples/httpserver_authentication_server.py
157+ :caption: examples/httpserver_authentication_server.py
158+ :linenos:
159+
160+ On the other hand, if you want to apply authentication to a set of routes, you need to call ``require_authentication `` function.
161+ In both cases you can check if ``request `` is authenticated by calling ``check_authentication `` on it.
162+
163+ .. literalinclude :: ../examples/httpserver_authentication_handlers.py
164+ :caption: examples/httpserver_authentication_handlers.py
165+ :linenos:
0 commit comments