[tracker/wip/carlosg/http-endpoint: 8/12] tracker: Add --http-port option to "tracker3 endpoint" command




commit 1be13357619612865adada27ac848906ed186e3b
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Dec 12 16:47:53 2020 +0100

    tracker: Add --http-port option to "tracker3 endpoint" command
    
    Let "tracker3 endpoint" expose HTTP endpoints as well as D-Bus ones.

 src/tracker/tracker-endpoint.c | 67 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)
---
diff --git a/src/tracker/tracker-endpoint.c b/src/tracker/tracker-endpoint.c
index b58609719..33d096171 100644
--- a/src/tracker/tracker-endpoint.c
+++ b/src/tracker/tracker-endpoint.c
@@ -42,6 +42,7 @@ static gboolean session_bus = FALSE;
 static gboolean system_bus = FALSE;
 static gboolean name_owned = FALSE;
 static gboolean list = FALSE;
+static gint http_port = -1;
 
 static GOptionEntry entries[] = {
        { "database", 'd', 0, G_OPTION_ARG_FILENAME, &database_path,
@@ -60,6 +61,10 @@ static GOptionEntry entries[] = {
          N_("Specify a path to an ontology to be used in this endpoint"),
          N_("DIR")
        },
+       { "http-port", 0, 0, G_OPTION_ARG_INT, &http_port,
+         N_("HTTP port"),
+         NULL
+       },
        { "session", 0, 0, G_OPTION_ARG_NONE, &session_bus,
          N_("Use session bus"),
          NULL
@@ -100,6 +105,12 @@ sanity_check (void)
                return FALSE;
        }
 
+       if (http_port > 0 && dbus_service) {
+               /* TRANSLATORS: those are commandline arguments */
+               g_printerr ("%s\n", _("--http-port cannot be used with --dbus-service"));
+               return FALSE;
+       }
+
        return TRUE;
 }
 
@@ -129,6 +140,53 @@ name_lost_cb (GDBusConnection *connection,
        g_main_loop_quit (user_data);
 }
 
+static gboolean
+run_http_endpoint (TrackerSparqlConnection  *connection,
+                   GError                  **error)
+{
+       TrackerEndpoint *endpoint = NULL;
+       g_autoptr(GMainLoop) main_loop = NULL;
+       GError *inner_error = NULL;
+       GInetAddress *loopback;
+       gchar *loopback_str, *address;
+
+       loopback = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
+       loopback_str = g_inet_address_to_string (loopback);
+       address = g_strdup_printf ("http://%s:%d/sparql/";,
+                                  loopback_str,
+                                  http_port);
+
+       g_print (_("Creating HTTP endpoint at %s…"), address);
+       g_print ("\n");
+       g_free (address);
+       g_free (loopback_str);
+       g_object_unref (loopback);
+
+       endpoint = TRACKER_ENDPOINT (tracker_endpoint_http_new (connection,
+                                                               http_port,
+                                                               NULL, NULL, &inner_error));
+
+       if (inner_error) {
+               g_propagate_error (error, inner_error);
+               return FALSE;
+       }
+
+       main_loop = g_main_loop_new (NULL, FALSE);
+
+       g_print ("%s\n", _("Listening to SPARQL commands. Press Ctrl-C to stop."));
+
+       g_unix_signal_add (SIGINT, sigterm_cb, main_loop);
+       g_unix_signal_add (SIGTERM, sigterm_cb, main_loop);
+
+       g_main_loop_run (main_loop);
+
+       /* Carriage return, so we paper over the ^C */
+       g_print ("\r%s\n", _("Closing connection…"));
+       g_clear_object (&endpoint);
+
+       return TRUE;
+}
+
 static gboolean
 run_endpoint (TrackerSparqlConnection  *connection,
               GError                  **error)
@@ -345,7 +403,14 @@ tracker_endpoint (int argc, const char **argv)
                return EXIT_FAILURE;
        }
 
-       if (dbus_service) {
+       if (http_port > 0) {
+               run_http_endpoint (connection, &error);
+
+               if (error) {
+                       g_printerr ("%s\n", error->message);
+                       g_error_free (error);
+               }
+       } else if (dbus_service) {
                run_endpoint (connection, &error);
 
                if (error) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]