Skip to content

Commit

Permalink
If hostname is not explicitly specified, deduce from request
Browse files Browse the repository at this point in the history
Hopefully allows to run without special config on Heroku
  • Loading branch information
jonnor committed Jan 10, 2015
1 parent bd99cd2 commit f92446b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ PREFIX=$(shell echo `pwd`/install)
FLAGS=-Wall -Werror -std=c99 -g -DHAVE_UUID -I$(PREFIX)/include/uuid
DEBUGPROG=
PORT=3569
HOST=localhost
EXTPORT=$(PORT)
#GRAPH=graphs/checker.json
#TESTS=
#HOST=localhost

ifneq ("$(wildcard /app)","")
# Heroku build. TODO: find better way to detect
Expand All @@ -25,10 +25,13 @@ LIBS=gegl-0.3 gio-unix-2.0 json-glib-1.0 libsoup-2.4 libpng uuid
DEPS=$(shell $(PREFIX)/env.sh pkg-config $(PKGCONFIG_ARGS) --libs --cflags $(LIBS))
TRAVIS_DEPENDENCIES=$(shell echo `cat .vendor_urls | sed -e "s/heroku/travis/" | tr -d '\n'`)

RUN_ARGUMENTS:=--port $(PORT) --host $(HOST) --external-port=$(EXTPORT)
RUN_ARGUMENTS:=--port $(PORT) --external-port=$(EXTPORT)
ifdef GRAPH
RUN_ARGUMENTS+=--graph $(GRAPH)
endif
ifdef HOST
RUN_ARGUMENTS+=--host $(HOST)
endif

ifdef TESTS
TEST_ARGUMENTS=--grep $(TESTS)
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: make run-noinstall PORT=$PORT HOST=$HOSTNAME EXTPORT=80 GRAPH=graphs/checker.json
web: make run-noinstall PORT=$PORT EXTPORT=80 GRAPH=graphs/checker.json
2 changes: 1 addition & 1 deletion bin/imgflo-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ quit(int sig)

static int port = 3569;
static int extport = 3569;
static gchar *host = "localhost";
static gchar *host = "";
static gchar *defaultgraph = "";

static GOptionEntry entries[] = {
Expand Down
37 changes: 30 additions & 7 deletions lib/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ ui_net_state_changed(Network *network, gboolean running,

gchar *
ui_get_process_url(UiConnection *ui, Network *network, const gchar *node) {
gchar *url = g_strdup_printf("http://%s:%d/process?graph=%s&node=%s",
ui->hostname, ui->registry->info->port,
network->graph->id, node);
gchar *endpoint = "/process";
if (g_str_has_suffix(ui->hostname, "/")) {
endpoint = "process";
}

gchar *url = g_strdup_printf("%s%s?graph=%s&node=%s",
ui->hostname, endpoint, network->graph->id, node);
return url;
}

Expand Down Expand Up @@ -420,14 +424,32 @@ ui_connection_handle_message(UiConnection *self,
}
}

static gboolean
ensure_hostname_set(UiConnection *self, SoupURI *uri) {
if (g_strcmp0(self->hostname, "") != 0) {
// Already configured
return FALSE;
}

SoupURI *hostUri = soup_uri_copy_host(uri);
g_free(self->hostname);
self->hostname = soup_uri_to_string(hostUri, FALSE);
soup_uri_free(hostUri);
// NOTE: does not check wether existing hostname was equivalent
return TRUE;
}

static void
on_web_socket_open(SoupWebsocketConnection *ws, gpointer user_data)
{
gchar *url = soup_uri_to_string(soup_websocket_connection_get_uri (ws), FALSE);
SoupURI *uri = soup_websocket_connection_get_uri(ws);
gchar *url = soup_uri_to_string(uri, FALSE);

imgflo_message("WebSocket: client opened %s with %s\n", soup_websocket_connection_get_protocol(ws), url);

UiConnection *self = (UiConnection *)user_data;
g_assert(self);
ensure_hostname_set(self, uri);
self->connection = ws;

g_free(url);
Expand Down Expand Up @@ -617,15 +639,16 @@ server_callback (SoupServer *server, SoupMessage *msg,

SoupMessageHeadersIter iter;
const char *name, *value;
UiConnection *self = (UiConnection *)data;

imgflo_message("%s %s HTTP/1.%d\n", msg->method, path,
soup_message_get_http_version(msg));
imgflo_message("HTTP URI: %s", soup_uri_to_string(soup_message_get_uri(msg), FALSE));
ensure_hostname_set(self, soup_message_get_uri(msg));

if (g_strcmp0(path, "/process") == 0 && msg->method == SOUP_METHOD_GET) {
process_image_callback(server, msg, path, query, context, data);
process_image_callback(server, msg, path, query, context, self);
} else if (g_strcmp0(path, "/") == 0 && msg->method == SOUP_METHOD_GET) {
serve_frontpage(server, msg, path, query, context, data);
serve_frontpage(server, msg, path, query, context, self);
} else {
imgflo_warning("Unknown HTTP request: %s, %s", msg->method, path);
soup_message_headers_iter_init(&iter, msg->request_headers);
Expand Down

0 comments on commit f92446b

Please sign in to comment.