From 67077ddf9229e8b861d5f9dc2df4559148004efd Mon Sep 17 00:00:00 2001 From: Ivan Polyakov Date: Sun, 26 Jun 2022 18:39:26 +0300 Subject: [PATCH] template rendering with inja --- backend/Makefile | 7 ++- backend/config.mk | 5 ++ backend/src/main.c | 6 +- backend/src/query.c | 2 +- backend/src/query.h | 2 +- backend/src/routes.c | 5 +- backend/src/routes.h | 6 +- backend/src/{template.c => template.cxx} | 48 ++++++---------- backend/src/template.h | 4 +- src/pages/webapps/scrollbar.scm | 73 ++++++++++++------------ 10 files changed, 76 insertions(+), 82 deletions(-) rename backend/src/{template.c => template.cxx} (50%) diff --git a/backend/Makefile b/backend/Makefile index 3ffd432..ade8787 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -18,12 +18,15 @@ include config.mk all: wwwvilorcgi -wwwvilorcgi: query.o template.o routes.o utils.o main.o - $(CC) $^ -o $@ $(LDFLAGS) +wwwvilorcgi: query.o routes.o utils.o template.o main.o + $(CXX) $^ -o $@ $(LDFLAGS) %.o: src/%.c $(CC) $(CFLAGS) $^ -c -fPIC +%.o: src/%.cxx + $(CXX) $(CXXFLAGS) $^ -c -fPIC + clean: rm -f *.o wwwvilorcgi diff --git a/backend/config.mk b/backend/config.mk index c42a695..a80026d 100644 --- a/backend/config.mk +++ b/backend/config.mk @@ -18,9 +18,14 @@ CC=cc CFLAGS=-ansi -pedantic LDFLAGS=-lfcgi +CXX=c++ +CXXFLAGS=-std=c++17 -pedantic + DEBUG= ifdef DEBUG CFLAGS+=-Wall -g -DDEBUG_MODE + CXXFLAGS+=-Wall -g -DDEBUG_MODE else CFLAGS+=-O3 + CXXFLAGS+=-O3 endif diff --git a/backend/src/main.c b/backend/src/main.c index eeaf557..8de5451 100644 --- a/backend/src/main.c +++ b/backend/src/main.c @@ -29,11 +29,9 @@ static int socketId; static int send_response(FCGX_Request *req, struct route *route) { - char resbuff[FBUFFSZ]; + const char *resbuff; - memset(resbuff, '\0', FBUFFSZ); - - if (route->resh(route, req, resbuff)) { + if (route->resh(route, req, &resbuff)) { fputs("Can't create response\n", stderr); return 2; } diff --git a/backend/src/query.c b/backend/src/query.c index bbe12dc..2204625 100644 --- a/backend/src/query.c +++ b/backend/src/query.c @@ -64,7 +64,7 @@ void reset_params(struct param *params) { int i = 0; while (params[i].key) { - strcpy(params[i].val, params[i].defaultval); + sprintf(params[i].val, "%s", params[i].defaultval); i++; } } diff --git a/backend/src/query.h b/backend/src/query.h index 4274d2e..7f25b12 100644 --- a/backend/src/query.h +++ b/backend/src/query.h @@ -40,7 +40,7 @@ void read_params(char *query, struct param *params); void reset_params(struct param *params); #ifdef __cplusplus -}; +} #endif #endif /* QUERY_H_ENTRY */ diff --git a/backend/src/routes.c b/backend/src/routes.c index 764b5ec..07d9f46 100644 --- a/backend/src/routes.c +++ b/backend/src/routes.c @@ -22,20 +22,19 @@ #include #include -int res_scrollbar(struct route *route, FCGX_Request *req, char buff[FBUFFSZ]) +int res_scrollbar(struct route *route, FCGX_Request *req, const char **buff) { char fpath[ROUTESZ]; sprintf(fpath, "%s%s", STATIC_PATH, route->uri); + reset_params(route->params); read_params(FCGX_GetParam("QUERY_STRING", req->envp), route->params); if (rendertpl(fpath, buff, route->params)) { return 1; } - reset_params(route->params); - return 0; } diff --git a/backend/src/routes.h b/backend/src/routes.h index c26690c..a98a6a6 100644 --- a/backend/src/routes.h +++ b/backend/src/routes.h @@ -32,12 +32,12 @@ extern "C" { struct route { const char *uri; - int (*resh)(struct route *route, FCGX_Request *req, char dest[FBUFFSZ]); + int (*resh)(struct route *route, FCGX_Request *req, const char **dest); struct param params[15]; }; /* responses */ -int res_scrollbar(struct route *route, FCGX_Request *req, char dest[FBUFFSZ]); +int res_scrollbar(struct route *route, FCGX_Request *req, const char **dest); static struct route routes[2] = { { @@ -68,7 +68,7 @@ static struct route routes[2] = { struct route *find_route(FCGX_Request *req); #ifdef __cplusplus -}; +} #endif #endif /* ROUTES_H_ENTRY */ diff --git a/backend/src/template.c b/backend/src/template.cxx similarity index 50% rename from backend/src/template.c rename to backend/src/template.cxx index 044e993..3975de8 100644 --- a/backend/src/template.c +++ b/backend/src/template.cxx @@ -17,43 +17,31 @@ along with this program. If not, see . */ #include "template.h" -#include "utils.h" -#include +#include +#include #include -#include -int rendertpl(const char *path, char dest[FBUFFSZ], struct param *params) +int rendertpl(const char *path, const char **dest, struct param params[]) { - FILE *fp = NULL; - int i = 0; - char paramtpl[100]; - - if (!(fp = fopen(path, "r"))) { - fputs("Can't open the file\n", stderr); - return 1; - } - - if (!fread(dest, sizeof(char), FBUFFSZ, fp)) { - fputs("Can't read file contents\n", stderr); - fclose(fp); - return 2; - } - fclose(fp); + nlohmann::json data; + for (int i = 0; params[i].key; i++) { #ifdef DEBUG_MODE - puts("Interpolation"); + printf("Key: %s; Val: %s;\n", params[i].key, params[i].val); #endif - while (params[i].key) { - char *val = strlen(params[i].val) - ? params[i].val - : params[i].defaultval; + data[params[i].key] = params[i].val; + } - sprintf(paramtpl, "{{ %s }}", params[i].key); -#ifdef DEBUG_MODE - printf("Key: %s; Val: %s; Real Val: %s\n", paramtpl, params[i].val, val); -#endif - while(strreplace(dest, paramtpl, val)) {} - i++; + inja::Environment env; + std::string result; + + try { + result = env.render_file(path, data); + *dest = result.c_str(); + } catch (const std::exception &e) { + *dest = 0; + fprintf(stderr, "%s\n", e.what()); + return 1; } return 0; diff --git a/backend/src/template.h b/backend/src/template.h index 61595a4..2262a85 100644 --- a/backend/src/template.h +++ b/backend/src/template.h @@ -26,10 +26,10 @@ extern "C" { #endif -int rendertpl(const char *path, char dest[FBUFFSZ], struct param *params); +int rendertpl(const char *path, const char **dest, struct param *params); #ifdef __cplusplus -}; +} #endif #endif /* TEMPLATE_H_ENTRY */ diff --git a/src/pages/webapps/scrollbar.scm b/src/pages/webapps/scrollbar.scm index caab15a..5be5b59 100644 --- a/src/pages/webapps/scrollbar.scm +++ b/src/pages/webapps/scrollbar.scm @@ -30,6 +30,13 @@ (define page-styles '("scrollbar.css")) (define page-scripts '("scrollbar.js")) +(define (backend-selectable-option value name) + `("{% if thumbbstl == \"" ,value "\" %}" + (option (@ (value ,value) (selected "selected")) ,name) + "{% else %}" + (option (@ (value ,value)) ,name) + "{% endif %}")) + (define page-embedded-style '(css+ (.scrollbar-app__demo @@ -60,14 +67,14 @@ (fieldset (@ (class "scrollbar-app__panel")) (h3 "Bar:") (div (@ (class "scrollbar-app__input")) - (label (@ (for "sbw")) "Width: ") - (input (@ - (type "text") - (id "sbw") - (name "sbw") - (value "{{ sbw }}") - (pattern "\\d+px") - (title "Size in pixels with format: \"px\""))))) + (label (@ (for "sbw")) "Width: ") + (input (@ + (type "text") + (id "sbw") + (name "sbw") + (value "{{ sbw }}") + (pattern "\\d+px") + (title "Size in pixels with format: \"px\""))))) (fieldset (@ (class "scrollbar-app__panel")) (h3 "Thumbnail:") @@ -89,38 +96,32 @@ (fieldset (h4 "Border:") (div (@ (class "scrollbar-app__input")) - (label (@ (for "thumbbstl")) "Style: ") - (select (@ (id "thumbbstl") (name "thumbbstl")) - (option (@ (value "none")) "none") - (option (@ (value "hidden")) "hidden") - (option (@ (value "solid") (selected "selected")) "solid") - (option (@ (value "dotted")) "dotted") - (option (@ (value "dashed")) "dashed") - (option (@ (value "double")) "double") - (option (@ (value "groove")) "groove") - (option (@ (value "ridge")) "ridge") - (option (@ (value "inset")) "inset") - (option (@ (value "outset")) "outset"))) + (label (@ (for "thumbbstl")) "Style: ") + (select (@ (id "thumbbstl") (name "thumbbstl")) + ,(map + (lambda (l) (backend-selectable-option l l)) + '("none" "hidden" "solid" "dotted" "dashed" + "double" "groove" "ridge" "inset" "outset")))) (div (@ (class "scrollbar-app__input")) - (label (@ (for "thumbbrad")) "Radius: ") - (input (@ - (type "text") - (id "thumbbrad") - (name "thumbbrad") - (value "{{ thumbbrad }}") - (pattern "\\d+(px|%)") - (title "Radius in pixels or percents with format: \"px\" or \"%\"")))) + (label (@ (for "thumbbrad")) "Radius: ") + (input (@ + (type "text") + (id "thumbbrad") + (name "thumbbrad") + (value "{{ thumbbrad }}") + (pattern "\\d+(px|%)") + (title "Radius in pixels or percents with format: \"px\" or \"%\"")))) (div (@ (class "scrollbar-app__input")) - (label (@ (for "thumbbw")) "Width: ") - (input (@ - (type "text") - (id "thumbbw") - (name "thumbbw") - (value "{{ thumbbw }}") - (pattern "\\d+px") - (title "Size in pixels with format: \"px\"")))) + (label (@ (for "thumbbw")) "Width: ") + (input (@ + (type "text") + (id "thumbbw") + (name "thumbbw") + (value "{{ thumbbw }}") + (pattern "\\d+px") + (title "Size in pixels with format: \"px\"")))) (div (@ (class "scrollbar-app__input")) (label (@ (for "thumbbclr")) "Color: ")