Browse Source

template rendering with inja

pull/2/head
Ivan Polyakov 2 years ago
parent
commit
67077ddf92
  1. 7
      backend/Makefile
  2. 5
      backend/config.mk
  3. 6
      backend/src/main.c
  4. 2
      backend/src/query.c
  5. 2
      backend/src/query.h
  6. 5
      backend/src/routes.c
  7. 6
      backend/src/routes.h
  8. 48
      backend/src/template.cxx
  9. 4
      backend/src/template.h
  10. 21
      src/pages/webapps/scrollbar.scm

7
backend/Makefile

@ -18,12 +18,15 @@ include config.mk
all: wwwvilorcgi all: wwwvilorcgi
wwwvilorcgi: query.o template.o routes.o utils.o main.o wwwvilorcgi: query.o routes.o utils.o template.o main.o
$(CC) $^ -o $@ $(LDFLAGS) $(CXX) $^ -o $@ $(LDFLAGS)
%.o: src/%.c %.o: src/%.c
$(CC) $(CFLAGS) $^ -c -fPIC $(CC) $(CFLAGS) $^ -c -fPIC
%.o: src/%.cxx
$(CXX) $(CXXFLAGS) $^ -c -fPIC
clean: clean:
rm -f *.o wwwvilorcgi rm -f *.o wwwvilorcgi

5
backend/config.mk

@ -18,9 +18,14 @@ CC=cc
CFLAGS=-ansi -pedantic CFLAGS=-ansi -pedantic
LDFLAGS=-lfcgi LDFLAGS=-lfcgi
CXX=c++
CXXFLAGS=-std=c++17 -pedantic
DEBUG= DEBUG=
ifdef DEBUG ifdef DEBUG
CFLAGS+=-Wall -g -DDEBUG_MODE CFLAGS+=-Wall -g -DDEBUG_MODE
CXXFLAGS+=-Wall -g -DDEBUG_MODE
else else
CFLAGS+=-O3 CFLAGS+=-O3
CXXFLAGS+=-O3
endif endif

6
backend/src/main.c

@ -29,11 +29,9 @@ static int socketId;
static int send_response(FCGX_Request *req, struct route *route) 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); fputs("Can't create response\n", stderr);
return 2; return 2;
} }

2
backend/src/query.c

@ -64,7 +64,7 @@ void reset_params(struct param *params)
{ {
int i = 0; int i = 0;
while (params[i].key) { while (params[i].key) {
strcpy(params[i].val, params[i].defaultval); sprintf(params[i].val, "%s", params[i].defaultval);
i++; i++;
} }
} }

2
backend/src/query.h

@ -40,7 +40,7 @@ void read_params(char *query, struct param *params);
void reset_params(struct param *params); void reset_params(struct param *params);
#ifdef __cplusplus #ifdef __cplusplus
}; }
#endif #endif
#endif /* QUERY_H_ENTRY */ #endif /* QUERY_H_ENTRY */

5
backend/src/routes.c

@ -22,20 +22,19 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
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]; char fpath[ROUTESZ];
sprintf(fpath, "%s%s", STATIC_PATH, route->uri); sprintf(fpath, "%s%s", STATIC_PATH, route->uri);
reset_params(route->params);
read_params(FCGX_GetParam("QUERY_STRING", req->envp), route->params); read_params(FCGX_GetParam("QUERY_STRING", req->envp), route->params);
if (rendertpl(fpath, buff, route->params)) { if (rendertpl(fpath, buff, route->params)) {
return 1; return 1;
} }
reset_params(route->params);
return 0; return 0;
} }

6
backend/src/routes.h

@ -32,12 +32,12 @@ extern "C" {
struct route { struct route {
const char *uri; 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]; struct param params[15];
}; };
/* responses */ /* 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] = { static struct route routes[2] = {
{ {
@ -68,7 +68,7 @@ static struct route routes[2] = {
struct route *find_route(FCGX_Request *req); struct route *find_route(FCGX_Request *req);
#ifdef __cplusplus #ifdef __cplusplus
}; }
#endif #endif
#endif /* ROUTES_H_ENTRY */ #endif /* ROUTES_H_ENTRY */

48
backend/src/template.c → backend/src/template.cxx

@ -17,43 +17,31 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "template.h" #include "template.h"
#include "utils.h" #include <inja/inja.hpp>
#include <stddef.h> #include <nlohmann/json.hpp>
#include <stdio.h> #include <stdio.h>
#include <string.h>
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; nlohmann::json data;
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);
for (int i = 0; params[i].key; i++) {
#ifdef DEBUG_MODE #ifdef DEBUG_MODE
puts("Interpolation"); printf("Key: %s; Val: %s;\n", params[i].key, params[i].val);
#endif #endif
while (params[i].key) { data[params[i].key] = params[i].val;
char *val = strlen(params[i].val) }
? params[i].val
: params[i].defaultval;
sprintf(paramtpl, "{{ %s }}", params[i].key); inja::Environment env;
#ifdef DEBUG_MODE std::string result;
printf("Key: %s; Val: %s; Real Val: %s\n", paramtpl, params[i].val, val);
#endif try {
while(strreplace(dest, paramtpl, val)) {} result = env.render_file(path, data);
i++; *dest = result.c_str();
} catch (const std::exception &e) {
*dest = 0;
fprintf(stderr, "%s\n", e.what());
return 1;
} }
return 0; return 0;

4
backend/src/template.h

@ -26,10 +26,10 @@
extern "C" { extern "C" {
#endif #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 #ifdef __cplusplus
}; }
#endif #endif
#endif /* TEMPLATE_H_ENTRY */ #endif /* TEMPLATE_H_ENTRY */

21
src/pages/webapps/scrollbar.scm

@ -30,6 +30,13 @@
(define page-styles '("scrollbar.css")) (define page-styles '("scrollbar.css"))
(define page-scripts '("scrollbar.js")) (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 (define page-embedded-style
'(css+ '(css+
(.scrollbar-app__demo (.scrollbar-app__demo
@ -91,16 +98,10 @@
(div (@ (class "scrollbar-app__input")) (div (@ (class "scrollbar-app__input"))
(label (@ (for "thumbbstl")) "Style: ") (label (@ (for "thumbbstl")) "Style: ")
(select (@ (id "thumbbstl") (name "thumbbstl")) (select (@ (id "thumbbstl") (name "thumbbstl"))
(option (@ (value "none")) "none") ,(map
(option (@ (value "hidden")) "hidden") (lambda (l) (backend-selectable-option l l))
(option (@ (value "solid") (selected "selected")) "solid") '("none" "hidden" "solid" "dotted" "dashed"
(option (@ (value "dotted")) "dotted") "double" "groove" "ridge" "inset" "outset"))))
(option (@ (value "dashed")) "dashed")
(option (@ (value "double")) "double")
(option (@ (value "groove")) "groove")
(option (@ (value "ridge")) "ridge")
(option (@ (value "inset")) "inset")
(option (@ (value "outset")) "outset")))
(div (@ (class "scrollbar-app__input")) (div (@ (class "scrollbar-app__input"))
(label (@ (for "thumbbrad")) "Radius: ") (label (@ (for "thumbbrad")) "Radius: ")

Loading…
Cancel
Save