Inline some of web.h into web.c
This commit is contained in:
parent
34032cb1c6
commit
806f63e8ae
35
src/web.c
35
src/web.c
|
@ -1,13 +1,3 @@
|
|||
/* Feel free to use this example code in any way
|
||||
you see fit (Public Domain) */
|
||||
|
||||
#include <sys/types.h>
|
||||
//#ifndef _WIN32
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
//#else
|
||||
//#include <winsock2.h>
|
||||
//#endif
|
||||
#include <string.h>
|
||||
#include <microhttpd.h>
|
||||
#include <stdio.h>
|
||||
|
@ -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) {
|
||||
printf("Initializing...\n");
|
||||
Object o = parseEval("(struct Request (queryParams))", global());
|
||||
cleanObject(&o);
|
||||
requestDefinition = getStructIndex("Request");
|
||||
}
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
int start(int port)
|
||||
{
|
||||
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));
|
||||
|
|
20
src/web.h
20
src/web.h
|
@ -1,25 +1,10 @@
|
|||
#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"
|
||||
"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."
|
||||
);
|
||||
|
||||
|
@ -35,4 +20,3 @@ fn(addPostRoute,
|
|||
" (def homepage (fn () (\"Hello, world!\")));(post \"/\" homepage)\n"
|
||||
" (serve)\n"
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue