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 Environment defaultEnv();
struct StructDef getStructDef(struct Environment* env, const char* name);
#endif #endif

View File

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

View File

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

View File

@ -15,6 +15,11 @@
#include "web.h" #include "web.h"
#include "pebblisp.h" #include "pebblisp.h"
struct StructDef query = {
.fieldCount = 1,
.name = "",
.names = {""}
};
int routeCount = 0; int routeCount = 0;
struct Route routes[10]; 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, answer_to_connection(void *cls, struct MHD_Connection *connection,
const char *url, const char *method, const char *url, const char *method,
const char *version, const char *upload_data, const char *version, const char *upload_data,
@ -44,14 +64,17 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
{ {
const char *page = NULL; const char *page = NULL;
printf("%s URL: '%s' :: ", method, url); 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++) { for (int i = 0; i < routeCount; i++) {
printf("route[%d]\n", i);
if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) { 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); //page = routes[i].routeAction(url);
Object emptyList = listObject(); Object lambdaList = listObject();
nf_addToList(&emptyList, numberObject(0)); nf_addToList(&lambdaList, queryParams);
Object route = cloneObject(routes[i].routeAction); 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; page = result.string;
break; break;
} }
@ -63,7 +86,8 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
} }
struct MHD_Response *response; struct MHD_Response *response;
enum MHD_Result ret; //enum MHD_Result ret;
int ret;
(void) cls; /* Unused. Silent compiler warning. */ (void) cls; /* Unused. Silent compiler warning. */
(void) url; /* Unused. Silent compiler warning. */ (void) url; /* Unused. Silent compiler warning. */
(void) method; /* Unused. Silent compiler warning. */ (void) method; /* Unused. Silent compiler warning. */