Add POST method.

Rename `addroute` to `get`.
This commit is contained in:
Sage Vaillancourt 2022-03-15 23:22:28 -04:00
parent 4decbbbe5c
commit 7faf2ebbb3
6 changed files with 41 additions and 13 deletions

View File

@ -278,7 +278,8 @@ struct Environment defaultEnv()
//{"sys", &systemCall}, //{"sys", &systemCall},
{"loadfile", &loadFile}, {"loadfile", &loadFile},
{"inp", &takeInput}, {"inp", &takeInput},
{"addroute", &addRouteO}, {"get", &addGetRoute},
{"post", &addPostRoute},
{"serve", &startServer} {"serve", &startServer}
#endif #endif
}; };

View File

@ -1,6 +1,6 @@
#!/usr/bin/pl #!/usr/bin/pl
(struct post (struct Post
(title body)) (title body))
;(def element (fn (type) (fn (text) (cat "<" type ">" text "</" type ">")))) ;(def element (fn (type) (fn (text) (cat "<" type ">" text "</" type ">"))))
@ -22,8 +22,8 @@
(h2 po's title) (h2 po's title)
(p po's body)))) (p po'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"
"Is ALSO a post. And frankly, what a great post! It's so long and flowing, "Is ALSO a post. And frankly, what a great post! It's so long and flowing,
and the interpreter won't even instantly crash over it! It's truly astounding and the interpreter won't even instantly crash over it! It's truly astounding
stuff, when you think about it." stuff, when you think about it."
@ -36,9 +36,9 @@ stuff, when you think about it."
(htmlize p1) (htmlize p1)
(htmlize p2)))))) ) (htmlize p2)))))) )
(addroute "/" homepage) (get "/" homepage)
(addroute "/styles.css" (fn () ( (get "/styles.css" (fn () (
"html { "html {
background-color: #ddddff; background-color: #ddddff;
} }

View File

@ -664,21 +664,30 @@ Object print(Object p, Object ignore, struct Environment* env)
return numberObject(0); return numberObject(0);
} }
Object addRouteO(Object path, Object textFunc, struct Environment* env) void addRouteO(Object path, Object textFunc, struct Environment* env, enum RouteType type)
{ {
char* p = malloc(sizeof(char) * strlen(path.string) + 1); char* p = malloc(sizeof(char) * strlen(path.string) + 1);
strcpy(p, path.string); strcpy(p, path.string);
//const char* t = malloc(sizeof(char) * strlen(textFunc.string) + 1);
//strcpy(t, textFunc.string);
struct Route r; struct Route r;
r.path = p; r.path = p;
r.routeAction = cloneObject(textFunc); r.routeAction = cloneObject(textFunc);
r.env = env; r.env = env;
r.type = type;
env->refs += 1; env->refs += 1;
//r.text = t; //r.text = t;
addRoute(r); addRoute(r);
}
Object addGetRoute(Object path, Object textFunc, struct Environment* env)
{
addRouteO(path, textFunc, env, GET);
return numberObject(1);
}
Object addPostRoute(Object path, Object textFunc, struct Environment* env)
{
addRouteO(path, textFunc, env, POST);
return numberObject(1); return numberObject(1);
} }

View File

@ -105,7 +105,8 @@ Object systemCall(Object call, Object _, struct Environment* i3);
Object loadFile(Object filename, Object _, struct Environment* env); Object loadFile(Object filename, Object _, struct Environment* env);
Object startServer(Object path, Object textFunc, struct Environment* env); Object startServer(Object path, Object textFunc, struct Environment* env);
Object addRouteO(Object path, Object textFunc, struct Environment* env); Object addGetRoute(Object path, Object textFunc, struct Environment* env);
Object addPostRoute(Object path, Object textFunc, struct Environment* env);
#endif #endif

View File

@ -24,6 +24,18 @@ int addRoute(struct Route route) {
return 0; return 0;
} }
int methodMatches(const char* method, struct Route* route)
{
switch (route->type) {
case GET:
return method[0] == 'G' || method[0] == 'g';
case POST:
return method[0] == 'P' || method[0] == 'p';
default:
return 0;
}
}
static enum MHD_Result static enum MHD_Result
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,
@ -33,8 +45,8 @@ 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);
for (int i = 0; i < routeCount; i++) { for (int i = 0; i < routeCount; i++) {
if (strcmp(url, routes[i].path) == 0) {
printf("route[%d]\n", i); printf("route[%d]\n", i);
if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) {
//page = routes[i].routeAction(url); //page = routes[i].routeAction(url);
Object emptyList = listObject(); Object emptyList = listObject();
nf_addToList(&emptyList, numberObject(0)); nf_addToList(&emptyList, numberObject(0));

View File

@ -1,10 +1,15 @@
#include "object.h" #include "object.h"
enum RouteType {
GET,
POST,
};
struct Route { struct Route {
const char* path; const char* path;
Object routeAction; Object routeAction;
struct Environment* env; struct Environment* env;
//const char* text; enum RouteType type;
}; };
int addRoute(struct Route route); int addRoute(struct Route route);