Start work on adding queryParams, etc.

This commit is contained in:
Sage Vaillancourt 2022-03-17 16:59:08 -04:00 committed by Sage Vaillancourt
parent 13a7ad97b5
commit b4584fa377
4 changed files with 46 additions and 18 deletions

View File

@ -37,4 +37,6 @@ void deleteEnv(struct Environment* e);
struct Environment defaultEnv();
struct StructDef getStructDef(struct Environment* env, const char* name);
#endif

View File

@ -25,9 +25,9 @@
(def rel (attribute "rel"))
(def href (attribute "href"))
(def htmlize (fn (po) (cat
(h2 po's title)
(p po's body))))
(def htmlize (fn (post) (cat
(h2 post's title)
(p post's body))))
(def p1 (Post "Hey" "This is a post"))
(def p2 (Post "This"
@ -46,7 +46,10 @@ stuff, when you think about it."
(htmlize p2)))
))))
(def revealer (fn (x) (cat "x: " x)))
(get "/" homepage)
(get "/x" revealer)
(get "/styles.css" (fn () (
"html {

View File

@ -76,12 +76,14 @@ Object evalStructArgs(const Object* symbol, const Object* fields, struct Environ
def.fieldCount = listLength(fields);
def.names = malloc(sizeof(char*) * def.fieldCount);
{
int i = 0;
FOR_POINTER_IN_LIST(fields) {
def.names[i] = malloc(sizeof(char) * (strlen(POINTER->string) + 1));
strcpy(def.names[i], POINTER->string);
i++;
}
}
while (env->outer) {
env = env->outer;
@ -318,10 +320,7 @@ Object evalList(const Object* obj, struct Environment* env)
int def = -1;
if (first_form->type == TYPE_SYMBOL) {
struct Environment* outerEnv = env;
while (outerEnv->outer) {
outerEnv = outerEnv->outer;
}
struct Environment* outerEnv = global();
for (int i = 0; i < outerEnv->structCount; i++) {
if (strcmp(first_form->string, outerEnv->structDefs[i].name) == 0) {
def = i;

View File

@ -15,6 +15,11 @@
#include "web.h"
#include "pebblisp.h"
struct StructDef query = {
.fieldCount = 1,
.name = "",
.names = {""}
};
int routeCount = 0;
struct Route routes[10];
@ -36,7 +41,22 @@ int methodMatches(const char* method, struct Route* route)
}
}
static enum MHD_Result
static int
print_out_key (void *queryParamsV, enum MHD_ValueKind kind, const char *key,
const char *value)
{
(void) kind; /* Unused. Silent compiler warning. */
Object* queryParams = queryParamsV;
Object pair = startList(stringFromSlice(key, strlen(key)));
nf_addToList(&pair, parseEval(value, NULL));
nf_addToList(queryParams, pair);
return MHD_YES;
}
//static enum MHD_Result
static int
answer_to_connection(void *cls, struct MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
@ -44,14 +64,17 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
{
const char *page = NULL;
printf("%s URL: '%s' :: ", method, url);
printf("version: %s\n", version);
printf("upload_data: %s\n", upload_data);
for (int i = 0; i < routeCount; i++) {
printf("route[%d]\n", i);
if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) {
Object queryParams = listObject();
MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, print_out_key, &queryParams);
//page = routes[i].routeAction(url);
Object emptyList = listObject();
nf_addToList(&emptyList, numberObject(0));
Object lambdaList = listObject();
nf_addToList(&lambdaList, queryParams);
Object route = cloneObject(routes[i].routeAction);
Object result = listEvalLambda(&route, &emptyList, routes[i].env);
Object result = listEvalLambda(&route, &lambdaList, routes[i].env);
page = result.string;
break;
}
@ -63,7 +86,8 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
}
struct MHD_Response *response;
enum MHD_Result ret;
//enum MHD_Result ret;
int ret;
(void) cls; /* Unused. Silent compiler warning. */
(void) url; /* Unused. Silent compiler warning. */
(void) method; /* Unused. Silent compiler warning. */