From 806f63e8aefb12cf837584cb86163f80a65d16af Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sun, 27 Mar 2022 19:36:45 -0400 Subject: [PATCH] Inline some of web.h into web.c --- src/web.c | 43 +++++++++++++++++++++++-------------------- src/web.h | 40 ++++++++++++---------------------------- 2 files changed, 35 insertions(+), 48 deletions(-) diff --git a/src/web.c b/src/web.c index 1be2033..f7d8523 100644 --- a/src/web.c +++ b/src/web.c @@ -1,13 +1,3 @@ -/* Feel free to use this example code in any way - you see fit (Public Domain) */ - -#include -//#ifndef _WIN32 -#include -#include -//#else -//#include -//#endif #include #include #include @@ -21,6 +11,18 @@ typedef enum MHD_Result HttpResult; typedef int HttpResult; #endif +enum RouteType { + GET, + POST, +}; + +struct Route { + const char* path; + Object routeAction; + struct Environment* env; + enum RouteType type; +}; + int requestDefinition = -1; int routeCount = 0; @@ -112,21 +114,22 @@ answer_to_connection(void* cls, struct MHD_Connection* connection, return ret; } -int initialized = 0; - void initialize() { - if (!initialized) { - Object o = parseEval("(struct Request (queryParams))", global()); - cleanObject(&o); - requestDefinition = getStructIndex("Request"); - } - initialized = 1; + printf("Initializing...\n"); + Object o = parseEval("(struct Request (queryParams))", global()); + cleanObject(&o); + requestDefinition = getStructIndex("Request"); } int start(int port) { - initialize(); + static int initialized = 0; + if (!initialized) { + initialize(); + initialized = 1; + } + struct MHD_Daemon* daemon = MHD_start_daemon( MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, port, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_END); @@ -173,7 +176,7 @@ Object startServer(Object* params, int length, struct Environment* env) Object portObject = params[0]; int port = 8888; - if (portObject.type == TYPE_NUMBER) { + if (length && portObject.type == TYPE_NUMBER) { port = portObject.number; } return numberObject(start(port)); diff --git a/src/web.h b/src/web.h index 0c76288..6e81858 100644 --- a/src/web.h +++ b/src/web.h @@ -1,38 +1,22 @@ #include "pebblisp.h" -enum RouteType { - GET, - POST, -}; - -struct Route { - const char* path; - Object routeAction; - struct Environment* env; - enum RouteType type; -}; - -int addRoute(struct Route route); - -int start(int port); - fn(startServer, - "(serve) => 0\n" - "Starts a simple web server with routes that have been added with (get) and (post).\n" - "Note: This is a non-blocking call. It is recommended to wait for user input before exiting.\n" - " A simple way would be to use (inp) immediately after the (serve) call." + "(serve) => 0\n" + "Starts a simple web server with routes that have been added with (get) and (post).\n" + "Returns 0 if the server was successfully started, otherwise 1.\n" + "Note: This is a non-blocking call. It is recommended to wait for some input before exiting.\n" + " A simple way would be to use (inp) immediately after the (serve) call." ); fn(addGetRoute, - "Note: Implementation bugs currently prevent using an inline lambda.\n" - " (def homepage (fn () (\"Hello, world!\")));(get \"/\" homepage)\n" - " (def queryPage (fn (req) (req's queryParams)));(get \"/x\" queryPage)\n" - " (serve)\n" + "Note: Implementation bugs currently prevent using an inline lambda.\n" + " (def homepage (fn () (\"Hello, world!\")));(get \"/\" homepage)\n" + " (def queryPage (fn (req) (req's queryParams)));(get \"/x\" queryPage)\n" + " (serve)\n" ); fn(addPostRoute, - "Note: Implementation bugs currently prevent using an inline lambda.\n" - " (def homepage (fn () (\"Hello, world!\")));(post \"/\" homepage)\n" - " (serve)\n" + "Note: Implementation bugs currently prevent using an inline lambda.\n" + " (def homepage (fn () (\"Hello, world!\")));(post \"/\" homepage)\n" + " (serve)\n" ); -