From 60813e80d70ef4d2299f69e2b367722420520a74 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Sun, 9 Jun 2019 19:41:41 +0200 Subject: [PATCH] option --viewsdir overrides /views addresses #305 --- README.md | 4 ++++ http.c | 7 ++----- recorder.c | 13 ++++++++++++- udata.h | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e3681f8..6839d6e 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,8 @@ This section lists the most important options of the Recorder with their long na `--docroot` overrides the compile-time setting of the HTTP document root. +`--viewsdir` overrides the path to the JSON views, which defaults to `/views`. + `--lua-script` specifies the path to the Lua script. If not given, Lua support is disabled. `--precision` overrides the compiled-in default. (See "Precision" later.) @@ -831,6 +833,8 @@ All JSON elements are copied into the `lastpos` data which is returned to the ca Note how `pathname` and `port` have been copied into the object. These values can be used by the `page` served in the view. +Recall that while we typically say `htdocs/views/` this path is actually configurable with the `--viewsdir` option. + ### Authentication If `view.json` contains an element called `auth`, it is assumed to be an array of strings, each of which are a 32-character [Digest authentication](https://en.wikipedia.org/wiki/Digest_access_authentication) SHA1 strings for the realm `owntracks-recorder`, for example: diff --git a/http.c b/http.c index 446c413..35f9daf 100644 --- a/http.c +++ b/http.c @@ -48,7 +48,6 @@ #ifdef WITH_HTTP #define MAXPARTS 40 -#define VIEWSUBDIR "views" /* A transparent 40x40 PNG image with a black border */ static unsigned char border40x40png[] = { @@ -132,11 +131,10 @@ static long *field_n(struct mg_connection *conn, char *fieldname) static JsonNode *loadview(struct udata *ud, const char *viewname) { static UT_string *fpath = NULL; - const char *doc_root = mg_get_option(ud->mgserver, "document_root"); JsonNode *view; utstring_renew(fpath); - utstring_printf(fpath, "%s/%s/%s.json", doc_root, VIEWSUBDIR, viewname); + utstring_printf(fpath, "%s/%s.json", ud->viewsdir, viewname); debug(ud, "loadview fpath=%s", UB(fpath)); view = json_mkobject(); @@ -905,7 +903,6 @@ static int view(struct mg_connection *conn, const char *viewname) struct udata *ud = (struct udata *)conn->server_param; int limit; char *p, buf[BUFSIZ]; - const char *doc_root = mg_get_option(ud->mgserver, "document_root"); static UT_string *fpath = NULL, *sbuf = NULL; FILE *fp; JsonNode *view, *j, *locarray, *obj, *loc, *geoline; @@ -946,7 +943,7 @@ static int view(struct mg_connection *conn, const char *viewname) } utstring_renew(fpath); - utstring_printf(fpath, "%s/%s/%s", doc_root, VIEWSUBDIR, j->string_); + utstring_printf(fpath, "%s/%s", ud->viewsdir, j->string_); debug(ud, "page file=%s", UB(fpath)); if ((fp = fopen(UB(fpath), "r")) == NULL) { diff --git a/recorder.c b/recorder.c index 95ad576..6d07788 100644 --- a/recorder.c +++ b/recorder.c @@ -1162,6 +1162,7 @@ void usage(char *prog) printf(" --doc-root document root (%s)\n", DOCROOT); printf(" --http-logdir directory in which to store access.log\n"); printf(" --browser-apikey Google maps browser API key\n"); + printf(" --viewsdir full path to JSON views. Default: (%s/views)\n", DOCROOT); #endif #ifdef WITH_LUA printf(" --lua-script path to Lua script. If unset, no Lua hooks\n"); @@ -1190,7 +1191,7 @@ int main(int argc, char **argv) { #if WITH_MQTT struct mosquitto *mosq = NULL; - UT_string *clientid; + UT_string *clientid, *uviewsdir; int rc, i; struct utsname uts; bool do_tls = false; @@ -1238,6 +1239,10 @@ int main(int argc, char **argv) udata.http_port = 8083; udata.http_logdir = NULL; udata.browser_apikey = NULL; + udata.viewsdir = NULL; + + utstring_new(uviewsdir); + utstring_printf(uviewsdir, "%s/views", DOCROOT); #endif #ifdef WITH_LUA udata.luascript = NULL; @@ -1360,6 +1365,7 @@ int main(int argc, char **argv) { "doc-root", required_argument, 0, 2}, { "http-logdir", required_argument, 0, 14}, { "browser-apikey", required_argument, 0, 15}, + { "viewsdir", required_argument, 0, 16}, #endif {0, 0, 0, 0} }; @@ -1455,6 +1461,10 @@ int main(int argc, char **argv) if (ud->browser_apikey) free(ud->browser_apikey); ud->browser_apikey = strdup(optarg); break; + case 16: + if (ud->viewsdir) free(ud->viewsdir); + ud->viewsdir = strdup(optarg); + break; #endif case 'D': ud->skipdemo = FALSE; @@ -1804,6 +1814,7 @@ int main(int argc, char **argv) mg_destroy_server(&udata.mgserver); free(ud->http_host); free(ud->browser_apikey); + free(ud->viewsdir); if (ud->http_logdir) free(ud->http_logdir); #endif diff --git a/udata.h b/udata.h index 43dfa32..4a1bf28 100644 --- a/udata.h +++ b/udata.h @@ -41,6 +41,7 @@ struct udata { int http_port; /* port number for above */ char *http_logdir; /* full path to http access log */ char *browser_apikey; /* Google maps browser API key */ + char *viewsdir; /* path to views directory */ #endif #ifdef WITH_LUA char *luascript; /* Path to Lua script */