diff --git a/README.md b/README.md index f4dddda..48b53fe 100644 --- a/README.md +++ b/README.md @@ -869,11 +869,10 @@ Note, that Jane's user/device tuple should also be returned in order to display ### Browser API keys -In order to use the Recorder's maps and views, you have to obtain a [Google API "Browser key"](https://developers.google.com/maps/documentation/javascript/get-api-key). You then add this key to your docroot directory so that the Recorder can find it. (You should already have a file called `apikey.js.sample` in that directory; copy or rename the file to `apikey.js` and ensure its content is valid JavaScript: +In order to use the Recorder's maps and views, you have to obtain a [Google API "Browser key"](https://developers.google.com/maps/documentation/javascript/get-api-key). You then pass this key to your Recorder invocation. ```bash -$ cat .../static/apikey.js -var apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; +$ ot-recorder --browser-apikey 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ... ``` diff --git a/docroot/static/.gitignore b/docroot/static/.gitignore index 1ba7f96..e69de29 100644 --- a/docroot/static/.gitignore +++ b/docroot/static/.gitignore @@ -1 +0,0 @@ -apikey.js diff --git a/docroot/static/apikey.js.sample b/docroot/static/apikey.js.sample deleted file mode 100644 index 45dfcef..0000000 --- a/docroot/static/apikey.js.sample +++ /dev/null @@ -1 +0,0 @@ -var apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; diff --git a/http.c b/http.c index b5f8ca9..35a0249 100644 --- a/http.c +++ b/http.c @@ -826,6 +826,23 @@ static JsonNode *viewdata(struct mg_connection *conn, JsonNode *view, int limit) return (locs); } +/* + * Build and return JavaScript file containing an API key variable in it. + */ + +static int apikey(struct mg_connection *conn) +{ + struct udata *ud = (struct udata *)conn->server_param; + static UT_string *sbuf = NULL; + + utstring_renew(sbuf); + utstring_printf(sbuf, "var apiKey = '%s';", + ud->browser_apikey ? ud->browser_apikey : ""); + + mg_printf_data(conn, "%s", UB(sbuf)); + return (MG_TRUE); +} + /* * We're being asked for a view. `viewname' contains the ID for this view. * A view is a JSON file in docroot. The JSON describes which file @@ -1393,6 +1410,9 @@ int ev_handler(struct mg_connection *conn, enum mg_event ev) return view(conn, conn->uri + strlen("/view/")); } + if (strstr(conn->uri, "/apikey.js") != NULL) { + return apikey(conn); + } if (!strcmp(conn->request_method, "POST")) { diff --git a/recorder.c b/recorder.c index 07ae03e..bdfa7e4 100644 --- a/recorder.c +++ b/recorder.c @@ -1144,6 +1144,7 @@ void usage(char *prog) printf(" --http-port -A HTTP port (8083); 0 to disable HTTP\n"); 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"); #endif #ifdef WITH_LUA printf(" --lua-script path to Lua script. If unset, no Lua hooks\n"); @@ -1212,6 +1213,7 @@ int main(int argc, char **argv) udata.http_host = strdup("localhost"); udata.http_port = 8083; udata.http_logdir = NULL; + udata.browser_apikey = NULL; #endif #ifdef WITH_LUA udata.luascript = NULL; @@ -1302,6 +1304,7 @@ int main(int argc, char **argv) { "http-port", required_argument, 0, 'A'}, { "doc-root", required_argument, 0, 2}, { "http-logdir", required_argument, 0, 14}, + { "browser-apikey", required_argument, 0, 15}, #endif {0, 0, 0, 0} }; @@ -1386,6 +1389,11 @@ int main(int argc, char **argv) case 14: if (ud->http_logdir) free(ud->http_logdir); ud->http_logdir = strdup(optarg); + break; + case 15: + if (ud->browser_apikey) free(ud->browser_apikey); + ud->browser_apikey = strdup(optarg); + break; #endif case 'D': ud->skipdemo = FALSE; @@ -1705,6 +1713,7 @@ int main(int argc, char **argv) #ifdef WITH_HTTP mg_destroy_server(&udata.mgserver); free(ud->http_host); + free(ud->browser_apikey); if (ud->http_logdir) free(ud->http_logdir); #endif diff --git a/udata.h b/udata.h index de3cd60..268bb98 100644 --- a/udata.h +++ b/udata.h @@ -35,6 +35,7 @@ struct udata { char *http_host; /* address of http bind */ int http_port; /* port number for above */ char *http_logdir; /* full path to http access log */ + char *browser_apikey; /* Google maps browser API key */ #endif #ifdef WITH_LUA char *luascript; /* Path to Lua script */