aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/httpd.c
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2018-05-13 02:39:28 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2018-05-13 19:38:32 +0200
commitf62faeb3ff69db2b1d27d775a7c30dbe4a78917c (patch)
tree217a48ef4e069369526f4438989849a0f17c3b2e /src/blogc-make/httpd.c
parent3be45bbbdc3f231926a30b2585a69b2e82918460 (diff)
downloadblogc-f62faeb3ff69db2b1d27d775a7c30dbe4a78917c.tar.gz
blogc-f62faeb3ff69db2b1d27d775a7c30dbe4a78917c.tar.bz2
blogc-f62faeb3ff69db2b1d27d775a7c30dbe4a78917c.zip
make: added 'watch' rule. improved 'runserver' rule.
Diffstat (limited to 'src/blogc-make/httpd.c')
-rw-r--r--src/blogc-make/httpd.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/blogc-make/httpd.c b/src/blogc-make/httpd.c
new file mode 100644
index 0000000..7d67b57
--- /dev/null
+++ b/src/blogc-make/httpd.c
@@ -0,0 +1,81 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include "../common/utils.h"
+#include "ctx.h"
+#include "exec.h"
+#include "reloader.h"
+#include "httpd.h"
+
+// we are not going to unit-test these functions, then printing errors
+// directly is not a big issue
+
+
+typedef struct {
+ bm_ctx_t *ctx;
+ bc_trie_t *args;
+} bm_httpd_t;
+
+
+static void*
+httpd_thread(void *arg)
+{
+ bm_httpd_t *httpd = arg;
+
+ int rv = bm_exec_blogc_runserver(httpd->ctx, bc_trie_lookup(httpd->args, "host"),
+ bc_trie_lookup(httpd->args, "port"), bc_trie_lookup(httpd->args, "threads"));
+
+ free(httpd);
+
+ // stop the reloader
+ bm_reloader_stop(rv);
+
+ return NULL;
+}
+
+
+int
+bm_httpd_run(bm_ctx_t **ctx, bm_rule_exec_func_t rule_exec, bc_slist_t *outputs,
+ bc_trie_t *args)
+{
+ int err;
+
+ pthread_attr_t attr;
+ if (0 != (err = pthread_attr_init(&attr))) {
+ fprintf(stderr, "blogc-make: error: failed to initialize httpd "
+ "thread attributes: %s\n", strerror(err));
+ return 3;
+ }
+
+ // we run the thread detached, because we don't want to wait it to join
+ // before exiting. the OS can clean it properly
+ if (0 != (err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
+ fprintf(stderr, "blogc-make: error: failed to mark httpd thread as "
+ "detached: %s\n", strerror(err));
+ return 3;
+ }
+
+ bm_httpd_t *rv = bc_malloc(sizeof(bm_httpd_t));
+ rv->ctx = *ctx;
+ rv->args = args;
+
+ pthread_t thread;
+ if (0 != (err = pthread_create(&thread, &attr, httpd_thread, rv))) {
+ fprintf(stderr, "blogc-make: error: failed to create httpd "
+ "thread: %s\n", strerror(err));
+ free(rv);
+ return 3;
+ }
+
+ // run the reloader
+ return bm_reloader_run(ctx, rule_exec, outputs, args);
+}