1#define LOG_TAG "WEBSRV"
16#include "freertos/FreeRTOS.h"
17#include "freertos/task.h"
25#define WEBSERVER_DEFAULT_PORT 80
29#define WEBSERVER_DEFAULT_MAX_HANDLERS 32
33#define WEBSERVER_DEFAULT_STACK_SIZE (8 * 1024)
37#define WEBSERVER_DEFAULT_TASK_PRIORITY 5
56 const char *dot = strrchr(filename,
'.');
58 return "application/octet-stream";
60 if (strcmp(dot,
".html") == 0)
62 if (strcmp(dot,
".css") == 0)
64 if (strcmp(dot,
".js") == 0)
65 return "application/javascript";
66 if (strcmp(dot,
".json") == 0)
67 return "application/json";
68 if (strcmp(dot,
".png") == 0)
70 if (strcmp(dot,
".jpg") == 0 || strcmp(dot,
".jpeg") == 0)
72 if (strcmp(dot,
".gif") == 0)
74 if (strcmp(dot,
".svg") == 0)
75 return "image/svg+xml";
76 if (strcmp(dot,
".ico") == 0)
77 return "image/x-icon";
78 if (strcmp(dot,
".txt") == 0)
80 if (strcmp(dot,
".xml") == 0)
81 return "application/xml";
82 if (strcmp(dot,
".wav") == 0)
84 if (strcmp(dot,
".mp3") == 0)
87 return "application/octet-stream";
100 if (strcmp(req->uri,
"/") == 0) {
101 snprintf(filepath,
sizeof(filepath),
"%s/index.html",
105 FILE *f = fopen(filepath,
"r");
107 LOGW(
"File not found: %s", filepath);
108 httpd_resp_send_404(req);
114 httpd_resp_set_type(req, mime_type);
119 while ((read_len = fread(buf, 1,
sizeof(buf), f)) > 0) {
120 if (httpd_resp_send_chunk(req, buf, read_len) != ESP_OK) {
121 LOGW(
"Failed to send data chunk for %s", filepath);
127 httpd_resp_send_chunk(req, NULL, 0);
135 httpd_resp_set_type(req,
"application/json");
136 httpd_resp_sendstr(req,
"{\"status\":\"ok\"}");
146 LOGI(
"Web server task started");
150 vTaskDelay(pdMS_TO_TICKS(10000));
153 LOGI(
"Web server task ending");
168 LOGW(
"Web server already running");
175 LOGE(
"Failed to initialize storage");
187 max_handlers = config->max_uri_handlers;
188 stack_size = config->stack_size;
189 task_priority = config->task_priority;
193 httpd_config_t http_config = HTTPD_DEFAULT_CONFIG();
194 http_config.server_port = port;
195 http_config.max_uri_handlers = max_handlers;
196 http_config.stack_size = stack_size;
197 http_config.uri_match_fn = httpd_uri_match_wildcard;
202 LOGE(
"Failed to start HTTP server: %s", esp_err_to_name(ret));
207 LOGI(
"HTTP server started on port %d", port);
211 httpd_uri_t health_uri = {
212 .uri =
"/api/health",
220 httpd_uri_t file_uri = {
231 BaseType_t task_ret = xTaskCreate(
webserver_task,
"webserver", stack_size,
235 if (task_ret != pdPASS) {
236 LOGE(
"Failed to create web server task");
242 LOGI(
"Web server initialized successfully");
254 if (server == NULL) {
263 vTaskDelay(pdMS_TO_TICKS(100));
267 LOGI(
"Web server stopped");
279 const httpd_uri_t *uri_handler) {
280 if (server == NULL || uri_handler == NULL) {
281 return ESP_ERR_INVALID_ARG;
284 esp_err_t ret = httpd_register_uri_handler(server, uri_handler);
286 LOGI(
"Registered handler: %s [%d]", uri_handler->uri, uri_handler->method);
288 LOGE(
"Failed to register handler %s: %s", uri_handler->uri,
289 esp_err_to_name(ret));
Project-wide logging macros based on ESP-IDF's logging library.
#define LOGW(...)
Log a message at Warning level.
#define LOGI(...)
Log a message at Info level.
#define LOGE(...)
Log a message at Error level.
esp_err_t storage_init(void)
Initialize and mount LittleFS filesystem.
const char * storage_get_mount_point(void)
Get the mount point for the LittleFS filesystem.
Web server configuration structure.
#define WEBSERVER_DEFAULT_STACK_SIZE
Default maximum number of URI handlers.
static esp_err_t static_file_handler(httpd_req_t *req)
HTTP handler for static files from LittleFS.
static const char * get_mime_type(const char *filename)
Get MIME type based on file extension.
esp_err_t webserver_register_handler(httpd_handle_t server, const httpd_uri_t *uri_handler)
Register a URI handler with the web server.
static void webserver_task(void *arg)
FreeRTOS task function for the HTTP server. Allows non-blocking server operation and future extensibi...
void webserver_stop(httpd_handle_t server)
Stop the web server and clean up resources.
static TaskHandle_t s_server_task_handle
Handle for the FreeRTOS web server task.
#define WEBSERVER_DEFAULT_PORT
Default port for the web server.
httpd_handle_t webserver_start(const webserver_config_t *config)
Start the web server with the given configuration.
static httpd_handle_t s_server_handle
Default task priority for the web server task.
#define WEBSERVER_DEFAULT_TASK_PRIORITY
Default stack size for the web server task.
#define WEBSERVER_DEFAULT_MAX_HANDLERS
Default port for the web server.
static esp_err_t health_check_handler(httpd_req_t *req)
HTTP handler for API health check (GET /api/health).
Simple HTTP web server component for ESP32 with async FreeRTOS support.