Page 1 of 1

ESP web server startup with default uri handler

Posted: Fri Apr 26, 2019 3:20 pm
by erik2727
I would like to seek advice how to enable the esp32 webserver httpd_uri_t handler to start with default index.html file bypass the http_resp_send handler callback.

I have defined the index.html as one of the binary files and successfully started the webserver using the get handler to redirect to index.html using http_resp_send with a esp_err_t get_handler.

In short ,
/ -->get handler redirect-->index.html can this be just simplify as /index.html by passing the get handler?

Code: Untitled.c Select all





esp_err_t get_handler(httpd_req_t *req) {

extern const char index_html_start[] asm("_binary_index_html_start");
extern const char index_html_end[] asm("_binary_index_html_end");

httpd_resp_send(req, index_html_start ,(index_html_end-index_html_start));

return ESP_OK;
}




static const httpd_uri_t hello = {
.uri = "/",
.method = HTTP_GET,
.handler = get_handler,
/* Let's pass response string in user
* context to demonstrate it's usage */
.user_ctx = "Hello World!"

};

static httpd_handle_t start_webserver(void) {


httpd_config_t webconfig = HTTPD_DEFAULT_CONFIG();
ESP_LOGI(TAG, "Starting server on port: '%d'", webconfig.server_port);
if (httpd_start(&web_server, &webconfig) == ESP_OK) {
ESP_LOGI(TAGHTTP, "Registering URI handlers");
httpd_register_uri_handler(web_server, &hello);

}


return web_server;


}