aboutsummaryrefslogtreecommitdiff
path: root/main/http.cpp
blob: 82ed4ec17f1834a69da9d3f1de500b65a945c35f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <esp_http_server.h>
#include "esp_log.h"

#include "http.h"
#include "utils.h"
#include "routes/routes.h"

static const char* TAG = "http";

httpd_handle_t start_webserver(void)
{
    httpd_handle_t server = NULL;
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
#if CONFIG_IDF_TARGET_LINUX
    // Setting port as 8001 when building for Linux. Port 80 can be used only by a priviliged user in linux.
    // So when a unpriviliged user tries to run the application, it throws bind error and the server is not started.
    // Port 8001 can be used by an unpriviliged user as well. So the application will not throw bind error and the
    // server will be started.
    config.server_port = 8001;
#endif // !CONFIG_IDF_TARGET_LINUX
    config.lru_purge_enable = true;
    config.max_uri_handlers = ARRLEN(handlers);

    // Start the httpd server
    ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
    if (httpd_start(&server, &config) == ESP_OK) {
        // Set URI handlers
        const httpd_uri_t* handler = handlers;

        while (handler->uri)
        {
            ESP_LOGD(TAG, "Registering URI handler %i %s", handler->method, handler->uri);
            httpd_register_uri_handler(server, handler);
            ++handler;
        }

        return server;
    }

    ESP_LOGI(TAG, "Error starting server!");
    return NULL;
}