feat(ui): implement responsive player browser experience
This commit is contained in:
@@ -7,22 +7,37 @@
|
||||
#define MAZE32_STATIC_CHUNK_SIZE 1024
|
||||
|
||||
static const char* TAG = "maze32_web";
|
||||
static const char* INDEX_PATH = "/littlefs/index.html.gz";
|
||||
|
||||
typedef struct {
|
||||
const char* path;
|
||||
const char* content_type;
|
||||
} maze32_static_asset_t;
|
||||
|
||||
static const maze32_static_asset_t INDEX_ASSET = {"/littlefs/index.html.gz",
|
||||
"text/html; charset=utf-8"};
|
||||
static const maze32_static_asset_t PLAYER_STYLE_ASSET = {"/littlefs/player.css.gz", "text/css"};
|
||||
static const maze32_static_asset_t PLAYER_SCRIPT_ASSET = {"/littlefs/player.js.gz",
|
||||
"text/javascript; charset=utf-8"};
|
||||
static const maze32_static_asset_t ICON_ASSET = {"/littlefs/icons.svg.gz", "image/svg+xml"};
|
||||
|
||||
static void preserve_static_context(void* context) {
|
||||
(void)context;
|
||||
}
|
||||
|
||||
static esp_err_t send_index(httpd_req_t* request) {
|
||||
FILE* file = fopen(INDEX_PATH, "rb");
|
||||
static esp_err_t send_static_asset(httpd_req_t* request) {
|
||||
const maze32_static_asset_t* asset = request->user_ctx;
|
||||
if (asset == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
FILE* file = fopen(asset->path, "rb");
|
||||
if (file == NULL) {
|
||||
ESP_LOGE(TAG, "Static asset is unavailable: %s", INDEX_PATH);
|
||||
ESP_LOGE(TAG, "Static asset is unavailable: %s", asset->path);
|
||||
httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR,
|
||||
"Приложение временно недоступно");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_resp_set_type(request, "text/html; charset=utf-8");
|
||||
httpd_resp_set_type(request, asset->content_type);
|
||||
httpd_resp_set_hdr(request, "Content-Encoding", "gzip");
|
||||
httpd_resp_set_hdr(request, "Cache-Control", "no-cache");
|
||||
|
||||
@@ -37,7 +52,7 @@ static esp_err_t send_index(httpd_req_t* request) {
|
||||
}
|
||||
|
||||
if (ferror(file) != 0) {
|
||||
ESP_LOGE(TAG, "Failed while reading static asset: %s", INDEX_PATH);
|
||||
ESP_LOGE(TAG, "Failed while reading static asset: %s", asset->path);
|
||||
result = ESP_FAIL;
|
||||
}
|
||||
fclose(file);
|
||||
@@ -74,7 +89,7 @@ esp_err_t maze32_web_start(httpd_handle_t* server, maze32_health_t* health,
|
||||
}
|
||||
|
||||
httpd_config_t configuration = HTTPD_DEFAULT_CONFIG();
|
||||
configuration.max_uri_handlers = 9;
|
||||
configuration.max_uri_handlers = 12;
|
||||
configuration.max_open_sockets = 9;
|
||||
configuration.lru_purge_enable = false;
|
||||
configuration.recv_wait_timeout = 5;
|
||||
@@ -91,14 +106,32 @@ esp_err_t maze32_web_start(httpd_handle_t* server, maze32_health_t* health,
|
||||
const httpd_uri_t root = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_index,
|
||||
.user_ctx = NULL,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&INDEX_ASSET,
|
||||
};
|
||||
const httpd_uri_t index = {
|
||||
.uri = "/index.html",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_index,
|
||||
.user_ctx = NULL,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&INDEX_ASSET,
|
||||
};
|
||||
const httpd_uri_t player_style = {
|
||||
.uri = "/player.css",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&PLAYER_STYLE_ASSET,
|
||||
};
|
||||
const httpd_uri_t player_script = {
|
||||
.uri = "/player.js",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&PLAYER_SCRIPT_ASSET,
|
||||
};
|
||||
const httpd_uri_t icons = {
|
||||
.uri = "/icons.svg",
|
||||
.method = HTTP_GET,
|
||||
.handler = send_static_asset,
|
||||
.user_ctx = (void*)&ICON_ASSET,
|
||||
};
|
||||
const httpd_uri_t health_uri = {
|
||||
.uri = "/health",
|
||||
@@ -111,6 +144,15 @@ esp_err_t maze32_web_start(httpd_handle_t* server, maze32_health_t* health,
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &index);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &player_style);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &player_script);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &icons);
|
||||
}
|
||||
if (result == ESP_OK) {
|
||||
result = httpd_register_uri_handler(*server, &health_uri);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user