From 7b521cf75376ccfd38c2ff077c854b408fe616ff Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Thu, 9 Jul 2020 18:57:29 -0400 Subject: Split code and add breadcrumb nav --- server.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 server.c (limited to 'server.c') diff --git a/server.c b/server.c new file mode 100644 index 0000000..de18e7d --- /dev/null +++ b/server.c @@ -0,0 +1,123 @@ +/* Copyright (C) 2020 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include "server.h" + +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 +#undef X +}; + +const struct kvalid Keys[KeysLen] = { +#define X(key, name, valid) [key] = { valid, name }, + ENUM_KEYS +#undef X +}; + +bool pagePublic; + +static enum kcgi_err request(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); + default: return httpFail(req, KHTTP_404); + } +} + +int main(int argc, char *argv[]) { + bool fastCGI = false; + + for (int opt; 0 < (opt = getopt(argc, argv, "fps:"));) { + switch (opt) { + break; case 'f': fastCGI = true; + break; case 'p': pagePublic = true; + break; case 's': htmlStylesheet = optarg; + break; default: return EX_USAGE; + } + } + if (optind == argc) errx(EX_USAGE, "database path required"); + + int error = sqlite3_open_v2(argv[optind], &db, SQLITE_OPEN_READONLY, NULL); + if (error) errx(EX_NOINPUT, "%s: %s", argv[optind], sqlite3_errmsg(db)); + atexit(dbClose); + + sqlite3_stmt *check; + error = sqlite3_prepare_v2( + db, SQL(PRAGMA user_version;), -1, &check, NULL + ); + if (error) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + + error = sqlite3_step(check); + if (error != SQLITE_ROW) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db)); + + int version = sqlite3_column_int(check, 0); + if (version != DatabaseVersion) { + errx(EX_DATAERR, "unsupported database version %d", version); + } + sqlite3_finalize(check); + + prepare(&stmt.networks, NetworksQuery); + prepare(&stmt.contexts, ContextsQuery); + + if (fastCGI) { + struct kfcgi *fcgi; + enum kcgi_err error = khttp_fcgi_init( + &fcgi, Keys, KeysLen, Pages, PagesLen, Networks + ); + if (error) errx(EX_CONFIG, "khttp_fcgi_init: %s", kcgi_strerror(error)); + for ( + struct kreq req; + KCGI_OK == (error = khttp_fcgi_parse(fcgi, &req)); + khttp_free(&req) + ) { + error = request(&req); + if (error && error != KCGI_HUP) break; + } + errx(EX_PROTOCOL, "khttp_fcgi_parse: %s", kcgi_strerror(error)); + } else { + struct kreq req; + enum kcgi_err error = khttp_parse( + &req, Keys, KeysLen, Pages, PagesLen, Networks + ); + if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error)); + error = request(&req); + if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error)); + khttp_free(&req); + } +} -- cgit 1.4.1