From a34ed6f653aef57db212697a6cb2ceff823e0861 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Wed, 19 Mar 2025 10:38:07 +0100 Subject: [PATCH] product editor send files --- backend/Makefile | 2 + backend/compile_flags.txt | 1 + backend/public/product_editor.html | 10 +++++ backend/public/product_editor.js | 3 ++ backend/src/controllers/controllers.h | 11 ++++++ backend/src/controllers/products.c | 57 +++++++++++++++++++++++++++ backend/src/main.c | 5 +++ 7 files changed, 89 insertions(+) create mode 100644 backend/public/product_editor.html create mode 100644 backend/public/product_editor.js diff --git a/backend/Makefile b/backend/Makefile index c329b87..65f328b 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -40,6 +40,8 @@ ifeq ($(RUN_TESTS),1) C_FLAGS += -DRUN_TESTS endif +PUBLIC_DIR_PATH=$(shell readlink -f ./public) +C_FLAGS += -DPUBLIC_DIR_PATH='"$(PUBLIC_DIR_PATH)"' HEADERS = $(shell find src/ -name *.h) C_FILES = $(shell find src/ -name *.c) diff --git a/backend/compile_flags.txt b/backend/compile_flags.txt index f79c88b..365d678 100644 --- a/backend/compile_flags.txt +++ b/backend/compile_flags.txt @@ -8,4 +8,5 @@ -pedantic-errors -Wno-unused-parameter -Wno-unused-function +-DPUBLIC_DIR_PATH="./public" diff --git a/backend/public/product_editor.html b/backend/public/product_editor.html new file mode 100644 index 0000000..8c847e0 --- /dev/null +++ b/backend/public/product_editor.html @@ -0,0 +1,10 @@ + + + + + + + +

Product editor

+ + diff --git a/backend/public/product_editor.js b/backend/public/product_editor.js new file mode 100644 index 0000000..d5c6ee4 --- /dev/null +++ b/backend/public/product_editor.js @@ -0,0 +1,3 @@ + +console.log("hello world") + diff --git a/backend/src/controllers/controllers.h b/backend/src/controllers/controllers.h index b0ea755..dc1b025 100644 --- a/backend/src/controllers/controllers.h +++ b/backend/src/controllers/controllers.h @@ -36,6 +36,8 @@ void route_post_set_number(HttpCtx* ctx); void route_get_not_found(HttpCtx* ctx); void route_get_products_all(HttpCtx* ctx); +void route_get_product_editor_html(HttpCtx* ctx); +void route_get_product_editor_js(HttpCtx* ctx); void route_post_carts_purchase(HttpCtx* ctx); @@ -79,6 +81,15 @@ const Session* middleware_session(HttpCtx* ctx); #define RESPOND_SERVER_ERROR(HTTP_CTX) \ RESPOND_JSON(HTTP_CTX, 500, "{\"ok\":false,\"msg\":\"server error\"}") +#define RESPOND_HTML_SERVER_ERROR(CTX) \ + RESPOND_HTML(CTX, \ + 500, \ + "

500 Server " \ + "Error

GET " \ + "%s
", \ + http_ctx_req_path(CTX)); + __attribute__((unused)) static inline void ___controllers_include_user(void) { RESPOND((HttpCtx*)0, 200, "text/html", "") diff --git a/backend/src/controllers/products.c b/backend/src/controllers/products.c index b3493a2..7a0e548 100644 --- a/backend/src/controllers/products.c +++ b/backend/src/controllers/products.c @@ -35,3 +35,60 @@ void route_get_products_all(HttpCtx* ctx) RESPOND_JSON(ctx, 200, "%s", res.data); string_destroy(&res); } + +static inline int read_and_send_file(HttpCtx* ctx, + const char* filepath, + size_t max_file_size, + const char* mime_type) +{ + int res; + + FILE* fp = fopen(filepath, "r"); + if (!fp) { + RESPOND_HTML_SERVER_ERROR(ctx); + return -1; + } + + char* buf = calloc(max_file_size + 1, sizeof(char)); + size_t bytes_read = fread(buf, sizeof(char), max_file_size, fp); + if (bytes_read == 0) { + RESPOND_HTML_SERVER_ERROR(ctx); + res = -1; + goto l0_return; + } else if (bytes_read >= max_file_size) { + fprintf(stderr, + "error: file too large '%s' >= %ld\n", + filepath, + max_file_size); + RESPOND_HTML_SERVER_ERROR(ctx); + res = -1; + goto l0_return; + } + + char content_length[24] = { 0 }; + snprintf(content_length, 24 - 1, "%ld", bytes_read); + + http_ctx_res_headers_set(ctx, "Content-Type", mime_type); + http_ctx_res_headers_set(ctx, "Content-Length", content_length); + + http_ctx_respond(ctx, 200, buf); + + res = 0; +l0_return: + fclose(fp); + return res; +} + +void route_get_product_editor_html(HttpCtx* ctx) +{ + read_and_send_file( + ctx, PUBLIC_DIR_PATH "/product_editor.html", 16384 - 1, "text/html"); +} + +void route_get_product_editor_js(HttpCtx* ctx) +{ + read_and_send_file(ctx, + PUBLIC_DIR_PATH "/product_editor.js", + 16384 - 1, + "application/javascript"); +} diff --git a/backend/src/main.c b/backend/src/main.c index dbb147f..abef007 100644 --- a/backend/src/main.c +++ b/backend/src/main.c @@ -38,6 +38,11 @@ int main(void) http_server_set_user_ctx(server, &cx); http_server_get(server, "/api/products/all", route_get_products_all); + http_server_get( + server, "/product_editor/index.html", route_get_product_editor_html); + http_server_get(server, + "/product_editor/product_editor.js", + route_get_product_editor_js); http_server_post(server, "/api/carts/purchase", route_post_carts_purchase);