Inline some of web.h into web.c
This commit is contained in:
parent
34032cb1c6
commit
806f63e8ae
43
src/web.c
43
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 <string.h>
|
||||||
#include <microhttpd.h>
|
#include <microhttpd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -21,6 +11,18 @@ typedef enum MHD_Result HttpResult;
|
||||||
typedef int HttpResult;
|
typedef int HttpResult;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
enum RouteType {
|
||||||
|
GET,
|
||||||
|
POST,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Route {
|
||||||
|
const char* path;
|
||||||
|
Object routeAction;
|
||||||
|
struct Environment* env;
|
||||||
|
enum RouteType type;
|
||||||
|
};
|
||||||
|
|
||||||
int requestDefinition = -1;
|
int requestDefinition = -1;
|
||||||
|
|
||||||
int routeCount = 0;
|
int routeCount = 0;
|
||||||
|
@ -112,21 +114,22 @@ answer_to_connection(void* cls, struct MHD_Connection* connection,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int initialized = 0;
|
|
||||||
|
|
||||||
void initialize()
|
void initialize()
|
||||||
{
|
{
|
||||||
if (!initialized) {
|
printf("Initializing...\n");
|
||||||
Object o = parseEval("(struct Request (queryParams))", global());
|
Object o = parseEval("(struct Request (queryParams))", global());
|
||||||
cleanObject(&o);
|
cleanObject(&o);
|
||||||
requestDefinition = getStructIndex("Request");
|
requestDefinition = getStructIndex("Request");
|
||||||
}
|
|
||||||
initialized = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int start(int port)
|
int start(int port)
|
||||||
{
|
{
|
||||||
initialize();
|
static int initialized = 0;
|
||||||
|
if (!initialized) {
|
||||||
|
initialize();
|
||||||
|
initialized = 1;
|
||||||
|
}
|
||||||
|
|
||||||
struct MHD_Daemon* daemon = MHD_start_daemon(
|
struct MHD_Daemon* daemon = MHD_start_daemon(
|
||||||
MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
|
MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
|
||||||
port, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_END);
|
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];
|
Object portObject = params[0];
|
||||||
|
|
||||||
int port = 8888;
|
int port = 8888;
|
||||||
if (portObject.type == TYPE_NUMBER) {
|
if (length && portObject.type == TYPE_NUMBER) {
|
||||||
port = portObject.number;
|
port = portObject.number;
|
||||||
}
|
}
|
||||||
return numberObject(start(port));
|
return numberObject(start(port));
|
||||||
|
|
40
src/web.h
40
src/web.h
|
@ -1,38 +1,22 @@
|
||||||
#include "pebblisp.h"
|
#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,
|
fn(startServer,
|
||||||
"(serve) => 0\n"
|
"(serve) => 0\n"
|
||||||
"Starts a simple web server with routes that have been added with (get) and (post).\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"
|
||||||
" A simple way would be to use (inp) immediately after the (serve) call."
|
"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,
|
fn(addGetRoute,
|
||||||
"Note: Implementation bugs currently prevent using an inline lambda.\n"
|
"Note: Implementation bugs currently prevent using an inline lambda.\n"
|
||||||
" (def homepage (fn () (\"Hello, world!\")));(get \"/\" homepage)\n"
|
" (def homepage (fn () (\"Hello, world!\")));(get \"/\" homepage)\n"
|
||||||
" (def queryPage (fn (req) (req's queryParams)));(get \"/x\" queryPage)\n"
|
" (def queryPage (fn (req) (req's queryParams)));(get \"/x\" queryPage)\n"
|
||||||
" (serve)\n"
|
" (serve)\n"
|
||||||
);
|
);
|
||||||
|
|
||||||
fn(addPostRoute,
|
fn(addPostRoute,
|
||||||
"Note: Implementation bugs currently prevent using an inline lambda.\n"
|
"Note: Implementation bugs currently prevent using an inline lambda.\n"
|
||||||
" (def homepage (fn () (\"Hello, world!\")));(post \"/\" homepage)\n"
|
" (def homepage (fn () (\"Hello, world!\")));(post \"/\" homepage)\n"
|
||||||
" (serve)\n"
|
" (serve)\n"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue