mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
product editor send files
This commit is contained in:
parent
206d49c069
commit
a34ed6f653
@ -40,6 +40,8 @@ ifeq ($(RUN_TESTS),1)
|
|||||||
C_FLAGS += -DRUN_TESTS
|
C_FLAGS += -DRUN_TESTS
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
PUBLIC_DIR_PATH=$(shell readlink -f ./public)
|
||||||
|
C_FLAGS += -DPUBLIC_DIR_PATH='"$(PUBLIC_DIR_PATH)"'
|
||||||
|
|
||||||
HEADERS = $(shell find src/ -name *.h)
|
HEADERS = $(shell find src/ -name *.h)
|
||||||
C_FILES = $(shell find src/ -name *.c)
|
C_FILES = $(shell find src/ -name *.c)
|
||||||
|
@ -8,4 +8,5 @@
|
|||||||
-pedantic-errors
|
-pedantic-errors
|
||||||
-Wno-unused-parameter
|
-Wno-unused-parameter
|
||||||
-Wno-unused-function
|
-Wno-unused-function
|
||||||
|
-DPUBLIC_DIR_PATH="./public"
|
||||||
|
|
||||||
|
10
backend/public/product_editor.html
Normal file
10
backend/public/product_editor.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<script src="product_editor.js" type="module" defer></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Product editor</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
3
backend/public/product_editor.js
Normal file
3
backend/public/product_editor.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
console.log("hello world")
|
||||||
|
|
@ -36,6 +36,8 @@ void route_post_set_number(HttpCtx* ctx);
|
|||||||
void route_get_not_found(HttpCtx* ctx);
|
void route_get_not_found(HttpCtx* ctx);
|
||||||
|
|
||||||
void route_get_products_all(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);
|
void route_post_carts_purchase(HttpCtx* ctx);
|
||||||
|
|
||||||
@ -79,6 +81,15 @@ const Session* middleware_session(HttpCtx* ctx);
|
|||||||
#define RESPOND_SERVER_ERROR(HTTP_CTX) \
|
#define RESPOND_SERVER_ERROR(HTTP_CTX) \
|
||||||
RESPOND_JSON(HTTP_CTX, 500, "{\"ok\":false,\"msg\":\"server error\"}")
|
RESPOND_JSON(HTTP_CTX, 500, "{\"ok\":false,\"msg\":\"server error\"}")
|
||||||
|
|
||||||
|
#define RESPOND_HTML_SERVER_ERROR(CTX) \
|
||||||
|
RESPOND_HTML(CTX, \
|
||||||
|
500, \
|
||||||
|
"<!DOCTYPE html><html><head><meta " \
|
||||||
|
"charset=\"utf-8\"></head><body><center><h1>500 Server " \
|
||||||
|
"Error</h1><code style=\"font-size: 1rem;\">GET " \
|
||||||
|
"%s</code></center></body></html>", \
|
||||||
|
http_ctx_req_path(CTX));
|
||||||
|
|
||||||
__attribute__((unused)) static inline void ___controllers_include_user(void)
|
__attribute__((unused)) static inline void ___controllers_include_user(void)
|
||||||
{
|
{
|
||||||
RESPOND((HttpCtx*)0, 200, "text/html", "")
|
RESPOND((HttpCtx*)0, 200, "text/html", "")
|
||||||
|
@ -35,3 +35,60 @@ void route_get_products_all(HttpCtx* ctx)
|
|||||||
RESPOND_JSON(ctx, 200, "%s", res.data);
|
RESPOND_JSON(ctx, 200, "%s", res.data);
|
||||||
string_destroy(&res);
|
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");
|
||||||
|
}
|
||||||
|
@ -38,6 +38,11 @@ int main(void)
|
|||||||
http_server_set_user_ctx(server, &cx);
|
http_server_set_user_ctx(server, &cx);
|
||||||
|
|
||||||
http_server_get(server, "/api/products/all", route_get_products_all);
|
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);
|
http_server_post(server, "/api/carts/purchase", route_post_carts_purchase);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user