about summary refs log tree commit diff
path: root/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'server.c')
-rw-r--r--server.c60
1 files changed, 24 insertions, 36 deletions
diff --git a/server.c b/server.c
index 6ecd921..fad9a90 100644
--- a/server.c
+++ b/server.c
@@ -26,13 +26,6 @@
 sqlite3 *db;
 struct Statements stmt;
 
-static void prepare(sqlite3_stmt **stmt, const char *query) {
-	int error = sqlite3_prepare_v3(
-		db, query, -1, SQLITE_PREPARE_PERSISTENT, stmt, NULL
-	);
-	if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), query);
-}
-
 const char *Pages[PagesLen] = {
 #define X(page, path) [page] = path,
 	ENUM_PAGES
@@ -45,44 +38,44 @@ const struct kvalid Keys[KeysLen] = {
 #undef X
 };
 
-bool pagePublic;
-int pageLimit = 50;
-int pageRecent = 500;
-
 static const char CSS[] = {
 #include "default.css.h"
 };
 static const char *Cache = "public, max-age=86400, immutable";
 
-static enum kcgi_err request(struct kreq *req) {
+static enum kcgi_err stylesheet(struct kreq *req) {
+	if (req->mime != KMIME_TEXT_CSS) return httpFail(req, KHTTP_404);
+	return httpHead(req, KHTTP_200, KMIME_TEXT_CSS)
+		|| khttp_head(req, kresps[KRESP_CACHE_CONTROL], "%s", Cache)
+		|| khttp_body(req)
+		|| khttp_write(req, CSS, sizeof(CSS));
+}
+
+static enum kcgi_err dispatch(struct kreq *req) {
 	if (req->method != KMETHOD_HEAD && req->method != KMETHOD_GET) {
 		return httpFail(req, KHTTP_405);
 	}
 	switch (req->page) {
-		case Networks: return pageNetworks(req);
-		case Contexts: return pageContexts(req);
-		case Events:   return pageEvents(req);
-		case Search:   return pageSearch(req);
-		case Stylesheet: {
-			if (req->mime != KMIME_TEXT_CSS) return httpFail(req, KHTTP_404);
-			return httpHead(req, KHTTP_200, KMIME_TEXT_CSS)
-				|| khttp_head(req, kresps[KRESP_CACHE_CONTROL], "%s", Cache)
-				|| khttp_body(req)
-				|| khttp_write(req, CSS, sizeof(CSS));
-		}
-		default: return httpFail(req, KHTTP_404);
+		case Networks:   return networksPage(req);
+		case Contexts:   return contextsPage(req);
+		case Events:     return eventsPage(req);
+		case Search:     return searchPage(req);
+		case Stylesheet: return stylesheet(req);
+		default:         return httpFail(req, KHTTP_404);
 	}
 }
 
 int main(int argc, char *argv[]) {
 	bool test = false;
 
-	for (int opt; 0 < (opt = getopt(argc, argv, "cl:pr:s:"));) {
+	for (int opt; 0 < (opt = getopt(argc, argv, "cg:l:o:pr:s:"));) {
 		switch (opt) {
+			break; case 'g': eventsGap = strtol(optarg, NULL, 10);
+			break; case 'o': eventsOverlap = strtol(optarg, NULL, 10);
 			break; case 'c': test = true;
-			break; case 'l': pageLimit = strtol(optarg, NULL, 10);
-			break; case 'p': pagePublic = true;
-			break; case 'r': pageRecent = strtol(optarg, NULL, 10);
+			break; case 'l': eventsLimit = strtol(optarg, NULL, 10);
+			break; case 'p': contextsPublic = true;
+			break; case 'r': contextsRecent = strtol(optarg, NULL, 10);
 			break; case 's': htmlStylesheet = optarg;
 			break; default:  return EX_USAGE;
 		}
@@ -108,12 +101,7 @@ int main(int argc, char *argv[]) {
 	}
 	sqlite3_finalize(check);
 
-	prepare(&stmt.networks, NetworksQuery);
-	prepare(&stmt.contexts, ContextsQuery);
-	prepare(&stmt.eventsAfter, EventsAfterQuery);
-	prepare(&stmt.eventsBefore, EventsBeforeQuery);
-	prepare(&stmt.search, SearchQuery);
-	
+	dbPrepareAll();
 	if (test) return EX_OK;
 	
 	if (khttp_fcgi_test()) {
@@ -127,7 +115,7 @@ int main(int argc, char *argv[]) {
 			KCGI_OK == (error = khttp_fcgi_parse(fcgi, &req));
 			khttp_free(&req)
 		) {
-			error = request(&req);
+			error = dispatch(&req);
 			if (error && error != KCGI_HUP) break;
 		}
 		if (error != KCGI_EXIT) {
@@ -140,7 +128,7 @@ int main(int argc, char *argv[]) {
 			&req, Keys, KeysLen, Pages, PagesLen, Networks
 		);
 		if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error));
-		error = request(&req);
+		error = dispatch(&req);
 		if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error));
 		khttp_free(&req);
 	}