Skip to content

Commit

Permalink
exposed cache functions to API
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Aug 16, 2024
1 parent b09fe59 commit edb2ce3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
4 changes: 3 additions & 1 deletion include/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ struct cache_entry_s
* The function also logs the outcome of each file deletion attempt.
*
* @note This operation does not remove cache entries from memory; it only deletes the files from the file system.
*
* @return The number of files that were successfully deleted.
*/
void cache_flush();
uint32_t cache_flush();

/**
* @brief Gathers statistics about the current cache.
Expand Down
4 changes: 4 additions & 0 deletions include/handler_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ error_t handleApiAuthRefreshToken(HttpConnection *connection, const char_t *uri,

error_t handleApiMigrateContent2Lib(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx);
error_t handleDeleteOverlay(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx);

error_t handleApiCacheFlush(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx);
error_t handleApiCacheStats(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx);

#endif
8 changes: 6 additions & 2 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
cache_entry_t cache_table = {.next = NULL, .hash = 0, .original_url = NULL, .cached_url = NULL, .file_path = NULL};
uint32_t cache_entries = 0;

void cache_flush()
uint32_t cache_flush()
{
uint32_t deleted = 0;
cache_entry_t *pos = &cache_table;

while (pos != NULL)
{
if (pos->exists)
{
// Attempt to delete the local file
if (pos->file_path && remove(pos->file_path) == 0)
if (pos->file_path && fsDeleteFile(pos->file_path) == NO_ERROR)
{
deleted++;
TRACE_INFO("Deleted cached file: %s\n", pos->file_path);
}
else
Expand All @@ -34,6 +36,8 @@ void cache_flush()

pos = pos->next;
}

return deleted;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/handler_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "fs_ext.h"
#include "cert.h"
#include "esp32.h"
#include "cache.h"

error_t parsePostData(HttpConnection *connection, char_t *post_data, size_t buffer_size)
{
Expand Down Expand Up @@ -2667,4 +2668,40 @@ error_t handleDeleteOverlay(HttpConnection *connection, const char_t *uri, const
TRACE_INFO("Removed overlay %s\n", overlay);

return httpOkResponse(connection);
}

error_t handleApiCacheFlush(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx)
{
/* RESTful API-based cache flush request */
uint32_t deleted = cache_flush();

char json_resp[128];
snprintf(json_resp, sizeof(json_resp), "{\"message\": \"Cache successfully flushed.\", \"deleted_files\": %u}", deleted);

httpPrepareHeader(connection, "application/json; charset=utf-8", strlen(json_resp));
return httpWriteResponseString(connection, json_resp, false);
}

error_t handleApiCacheStats(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx)
{
cache_stats_t stats;
cache_stats(&stats);

char stats_json[512];
snprintf(stats_json, sizeof(stats_json),
"{"
"\"total_entries\": %zu,"
"\"exists_entries\": %zu,"
"\"total_files\": %zu,"
"\"total_size\": %zu,"
"\"memory_used\": %zu"
"}",
stats.total_entries,
stats.exists_entries,
stats.total_files,
stats.total_size,
stats.memory_used);

httpPrepareHeader(connection, "application/json; charset=utf-8", osStrlen(stats_json));
return httpWriteResponseString(connection, stats_json, false);
}
35 changes: 22 additions & 13 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ request_type_t request_paths[] = {
{REQ_POST, "/api/settings/reset/", SERTY_WEB, &handleApiSettingsReset},
{REQ_POST, "/api/settings/removeOverlay", SERTY_WEB, &handleDeleteOverlay},
{REQ_POST, "/api/migrateContent2Lib", SERTY_WEB, &handleApiMigrateContent2Lib},
{REQ_POST, "/api/cacheFlush", SERTY_WEB, &handleApiCacheFlush},
{REQ_GET, "/api/cacheStats", SERTY_WEB, &handleApiCacheStats},
{REQ_GET, "/api/sse", SERTY_WEB, &handleApiSse},
{REQ_GET, "/robots.txt", SERTY_WEB, &handleSecMitRobotsTxt},
/* official tonies API */
Expand All @@ -160,28 +162,20 @@ request_type_t request_paths[] = {

error_t handleCacheDownload(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx)
{
if (strcmp(uri, "/cache/flush") == 0)
{
cache_flush();

char *resp = "<html><body><h1>Cache Flushed</h1><p>The cache has been successfully flushed.</p><a href=\"/cache/stats.html\">Return to Cache Stats</a></body></html>";
httpPrepareHeader(connection, "text/html; charset=utf-8", osStrlen(resp));
return httpWriteResponseString(connection, resp, false);
}

if (strcmp(uri, "/cache/stats.html") == 0)
/* guerilla-style stats page for internal tests, to be removed when web ui is finished */
if (osStrcmp(uri, "/cache/stats.html") == 0)
{
cache_stats_t stats;
cache_stats(&stats);

char stats_page[4096]; // Buffer for the HTML page
char stats_page[4096];
snprintf(stats_page, sizeof(stats_page),
"<!DOCTYPE html>"
"<html lang=\"en\">"
"<head>"
"<meta charset=\"UTF-8\">"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
"<meta http-equiv=\"refresh\" content=\"5\">"
"<meta http-equiv=\"refresh\" content=\"1\">"
"<title>Cache Statistics</title>"
"<style>"
"body { font-family: Arial, sans-serif; background-color: #f4f4f4; color: #333; }"
Expand All @@ -192,6 +186,21 @@ error_t handleCacheDownload(HttpConnection *connection, const char_t *uri, const
"th { background-color: #f2f2f2; }"
".btn { display: inline-block; padding: 10px 20px; font-size: 16px; color: #fff; background-color: #007bff; border: none; border-radius: 5px; text-decoration: none; margin-top: 20px; }"
"</style>"
"<script>"
"function flushCache() {"
" fetch('/api/cacheFlush', { method: 'POST' })"
" .then(response => response.json())"
" .then(data => {"
" alert(data.message + ' Number of files deleted: ' + data.deleted_files);"
" location.reload();"
" })"
" .catch(error => {"
" console.error('Error flushing cache:', error);"
" alert('Failed to flush cache.');"
" });"
"}"

"</script>"
"</head>"
"<body>"
"<div class=\"container\">"
Expand All @@ -203,7 +212,7 @@ error_t handleCacheDownload(HttpConnection *connection, const char_t *uri, const
"<tr><th>Total Cache Size</th><td>%zu bytes</td></tr>"
"<tr><th>Memory Used</th><td>%zu bytes</td></tr>"
"</table>"
"<a href=\"/cache/flush\" class=\"btn\">Flush Cache</a>"
"<button class=\"btn\" onclick=\"flushCache()\">Flush Cache</button>"
"</div>"
"</body>"
"</html>",
Expand Down

0 comments on commit edb2ce3

Please sign in to comment.