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 @@ -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

5
backend/config.mk

@ -18,9 +18,14 @@ CC=cc @@ -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

6
backend/src/main.c

@ -29,11 +29,9 @@ static int socketId; @@ -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;
}

2
backend/src/query.c

@ -64,7 +64,7 @@ void reset_params(struct param *params) @@ -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++;
}
}

2
backend/src/query.h

@ -40,7 +40,7 @@ void read_params(char *query, struct param *params); @@ -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 */

5
backend/src/routes.c

@ -22,20 +22,19 @@ @@ -22,20 +22,19 @@
#include <string.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];
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;
}

6
backend/src/routes.h

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

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

@ -17,43 +17,31 @@ @@ -17,43 +17,31 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "template.h"
#include "utils.h"
#include <stddef.h>
#include <inja/inja.hpp>
#include <nlohmann/json.hpp>
#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;
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;

4
backend/src/template.h

@ -26,10 +26,10 @@ @@ -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 */

21
src/pages/webapps/scrollbar.scm

@ -30,6 +30,13 @@ @@ -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
@ -91,16 +98,10 @@ @@ -91,16 +98,10 @@
(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")))
,(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: ")

Loading…
Cancel
Save