|
| 1 | +Server |
| 2 | +====== |
| 3 | + |
| 4 | +Server provide HTTP technologies integrations under a common interface. They |
| 5 | +inherit from `GLib.Application`_, providing an optimal integration with the |
| 6 | +host environment. |
| 7 | + |
| 8 | +The following implementations are available: |
| 9 | + |
| 10 | +- libsoup built-in HTTP server |
| 11 | +- FastCGI |
| 12 | + |
| 13 | +Basically, you have access to a `DBusConnection`_ to communicate with other |
| 14 | +process and a `GLib.MainLoop`_ to process events and asynchronous work. |
| 15 | + |
| 16 | +- an application id to identify primary instance |
| 17 | +- ``startup`` signal emmited right after the registration |
| 18 | +- ``shutdown`` signal just before the server exits |
| 19 | +- a resource base path |
| 20 | +- ability to handle CLI arguments |
| 21 | + |
| 22 | +.. _DBusConnection: http://valadoc.org/#!api=gio-2.0/GLib.DBusConnection |
| 23 | +.. _GLib.MainLoop: http://valadoc.org/#!api=glib-2.0/GLib.MainLoop |
| 24 | + |
| 25 | +DBus connection |
| 26 | +--------------- |
| 27 | + |
| 28 | +`GLib.Application`_ will automatically register to the session DBus bus, making |
| 29 | +IPC (Inter-Process Communication) an easy thing. |
| 30 | + |
| 31 | +It can be used to expose runtime information such as a database connection |
| 32 | +details or the amount of processing requests. See this `example of DBus server`_ |
| 33 | +for code examples. |
| 34 | + |
| 35 | +.. _example of DBus server: https://wiki.gnome.org/Projects/Vala/DBusServerSample |
| 36 | + |
| 37 | +To identify your workers, you can use the ``set_application_id`` function. |
| 38 | + |
| 39 | +.. code:: vala |
| 40 | +
|
| 41 | + server.set_application_id ("worker"); |
| 42 | +
|
| 43 | +This can be used to request services and communicate between your workers and |
| 44 | +interact with the runtime. |
| 45 | + |
| 46 | +.. code:: vala |
| 47 | +
|
| 48 | + var connection = server.get_dbus_connection () |
| 49 | +
|
| 50 | + connection.call () |
| 51 | +
|
| 52 | +.. _GLib.Application: http://valadoc.org/#!api=gio-2.0/GLib.Application |
| 53 | + |
| 54 | +Options |
| 55 | +------- |
| 56 | + |
| 57 | +Each server implementation can optionally take arguments that parametrize their |
| 58 | +runtime. Generally, you can set the following options: |
| 59 | + |
| 60 | +- a socket path or a TCP port |
| 61 | +- backlog |
| 62 | +- inactivity timeout |
| 63 | + |
| 64 | +If you build your application in a main block, it is not possible to obtain the |
| 65 | +CLI arguments, so you must write your code in a ``main`` function. |
| 66 | + |
| 67 | +.. code:: vala |
| 68 | +
|
| 69 | + public static int main (string[] args) { |
| 70 | + var app = new Router; |
| 71 | +
|
| 72 | + app.get ("", (req, res) => { |
| 73 | + res.write ("Hello world!".data); |
| 74 | + }); |
| 75 | +
|
| 76 | + return new Server (app).run (args); |
| 77 | + } |
| 78 | +
|
| 79 | +If you specify the ``--help`` flag, you can get more information on the |
| 80 | +available options. |
| 81 | + |
| 82 | +.. code:: bash |
| 83 | +
|
| 84 | + build/examples/fastcgi --help |
| 85 | +
|
| 86 | +.. code:: bash |
| 87 | +
|
| 88 | + Usage: |
| 89 | + fastcgi [OPTION...] |
| 90 | +
|
| 91 | + Help Options: |
| 92 | + -h, --help Show help options |
| 93 | + --help-all Show all help options |
| 94 | + --help-gapplication Show GApplication options |
| 95 | +
|
| 96 | + Application Options: |
| 97 | + -s, --socket path to the UNIX socket |
| 98 | + -p, --port TCP port on this host |
| 99 | + -b, --backlog=0 listen queue depth used in the listen() call |
| 100 | + -t, --timeout=0 inactivity timeout in ms |
| 101 | +
|
| 102 | +Socket |
| 103 | +~~~~~~ |
| 104 | + |
| 105 | +In some context, you do not want to serve your application over a TCP socket, |
| 106 | +but just a local socket. Either this or ``--port`` can be specified, but not |
| 107 | +both. |
| 108 | + |
| 109 | +Port |
| 110 | +~~~~ |
| 111 | + |
| 112 | +This is the TCP port on which the application will be exposed on the local |
| 113 | +host. |
| 114 | + |
| 115 | +Backlog |
| 116 | +~~~~~~~ |
| 117 | + |
| 118 | +The backlog correspond to the depth on the ``listen`` call and is used if you |
| 119 | +have multiple listener on a socket. |
| 120 | + |
| 121 | +Inactivity timeout |
| 122 | +~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +An inactivity timeout can be set to exit automatically after a certain amount |
| 125 | +of milliseconds if no request is being processed. |
| 126 | + |
| 127 | +The server keeps track of the number of processing requests with ``hold`` and |
| 128 | +``release`` from `GLib.Application`_. When the amount reaches 0, the server |
| 129 | +will exit automatically after the value of the inactivity timeout. |
| 130 | + |
| 131 | +This option is enabled if the default timeout value is greater than 0. |
| 132 | + |
| 133 | +.. _GLib.Application: http://valadoc.org/#!api=gio-2.0/GLib.Application |
| 134 | + |
| 135 | + |
0 commit comments