aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..3d8f57d
--- /dev/null
+++ b/main.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <string.h>
+#include <dlfcn.h>
+
+#include "microhttpd.h"
+
+typedef char*(*endpoint)(struct MHD_Connection*);
+
+enum MHD_Result process_connection(void *dylib, struct MHD_Connection *connection, const char *url,
+ const char *method, const char *version, const char *upload_data,
+ size_t *upload_data_size, void **req_cls)
+{
+ printf("\nurl: %s\nmethod:%s\nversion:%s\nupload_data:%s\nsize:%zu\n",
+ url, method, version, upload_data, *upload_data_size);
+ /* Load function with the name of the url from shared library */
+ endpoint handler = dlsym(dylib, url);
+ struct MHD_Response *res; int status;
+ if (handler != NULL) {
+ fprintf(stderr, "Accessed at url\t%s\n", url);
+ char* response = handler(connection);
+ res = MHD_create_response_from_buffer(strlen(response), response, MHD_RESPMEM_MUST_FREE);
+ status = MHD_HTTP_OK;
+ } else {
+ fprintf(stderr, "Failed to access at url\t%s\n", url);
+ const char* error = "<html><body>Error 404: Page does not exist</body></html>";
+ res = MHD_create_response_from_buffer(strlen(error), (void*)error, MHD_RESPMEM_PERSISTENT);
+ status = MHD_HTTP_NOT_FOUND;
+ }
+ MHD_queue_response(connection, status, res);
+ MHD_destroy_response(res);
+ return MHD_YES;
+}
+
+
+int main(){
+ void* dylib = dlopen("./endpoints.so", RTLD_NOW);
+ if (!dylib) {
+ fprintf(stderr, "Could not open dynamic library: %s\n", dlerror());
+ return 1;
+ }
+ struct MHD_Daemon *daemon = MHD_start_daemon(MHD_USE_POLL | MHD_USE_INTERNAL_POLLING_THREAD, 8080, NULL, NULL, process_connection, dylib, MHD_OPTION_END);
+ if (daemon == NULL) {
+ perror("daemon");
+ fprintf(stderr, "could not initialize daemon\n");
+ return 1;
+ }
+ getchar();
+ MHD_stop_daemon(daemon);
+ return 0;
+}