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?

  1.  
  2.  
  3.  
  4. esp_err_t get_handler(httpd_req_t *req) {
  5.  
  6.     extern const char index_html_start[] asm("_binary_index_html_start");
  7.     extern const char index_html_end[] asm("_binary_index_html_end");
  8.  
  9.     httpd_resp_send(req, index_html_start ,(index_html_end-index_html_start));
  10.  
  11. return ESP_OK;
  12. }
  13.  
  14.  
  15.  
  16.  
  17. static const httpd_uri_t hello = {
  18.         .uri       = "/",
  19.         .method    = HTTP_GET,
  20.         .handler   = get_handler,
  21.         /* Let's pass response string in user
  22.          * context to demonstrate it's usage */
  23.         .user_ctx  = "Hello World!"
  24.  
  25. };
  26.  
  27. static httpd_handle_t start_webserver(void) {
  28.  
  29.  
  30.     httpd_config_t webconfig = HTTPD_DEFAULT_CONFIG();
  31.     ESP_LOGI(TAG, "Starting server on port: '%d'", webconfig.server_port);
  32.     if (httpd_start(&web_server, &webconfig) == ESP_OK) {
  33.         ESP_LOGI(TAGHTTP, "Registering URI handlers");
  34.         httpd_register_uri_handler(web_server, &hello);
  35.  
  36.     }
  37.  
  38.  
  39.     return web_server;
  40.  
  41.  
  42. }
  43.