|Did you know ...||Search Documentation:|
|http_unix_daemon.pl -- Run SWI-Prolog HTTP server as a Unix system daemon|
This module provides the logic that is needed to integrate a process into the Unix service (daemon) architecture. It deals with the following aspects, all of which may be used/ignored and configured using commandline options:
port(s)to be used by the server
The typical use scenario is to write a file that loads the following components:
In the code below,
?- [load]. loads the remainder of the webserver
code. This is often a sequence of use_module/1 directives.
:- use_module(library(http/http_unix_daemon)). :- [load].
The program entry point is http_daemon/0, declared using initialization/2. This may be overruled using a new declaration after loading this library. The new entry point will typically call http_daemon/1 to start the server in a preconfigured way.
:- use_module(library(http/http_unix_daemon)). :- initialization(run, main). run :- ... http_daemon(Options).
Now, the server may be started using the command below. See http_daemon/0 for supported options.
% [sudo] swipl mainfile.pl [option ...]
Below are some examples. Our first example is completely silent, running
on port 80 as user
% swipl mainfile.pl --user=www --pidfile=/var/run/http.pid
Our second example logs HTTP interaction with the syslog daemon for
debugging purposes. Note that the argument to
--debug= is a Prolog
term and must often be escaped to avoid misinterpretation by the Unix
shell. The debug option can be repeated to log multiple debug topics.
% swipl mainfile.pl --user=www --pidfile=/var/run/http.pid \ --debug='http(request)' --syslog=http
Broadcasting The library uses broadcast/1 to allow hooking certain events:
--https=Specis followed by arguments for that server until the next
--https=Specor the end of the options.
--https=Specappears, one HTTP server is created from the specified parameters.
--workers=10 --http --https --http=8080 --https=8443 --http=localhost:8080 --workers=1 --https=8443 --workers=25
--user=Userto open ports below 1000. The default port is 80. If
--httpsis used, the default port is 443.
--ip=localhostto restrict access to connections from localhost if the server itself is behind an (Apache) proxy server running on the same host.
--user. If omitted, the login group of the target user is used.
--fork=false, the process runs in the foreground.
true, create at the specified or default address. Else use the given port and interface. Thus,
--httpcreates a server at port 80,
--http=8080creates one at port 8080 and
--http=localhost:8080creates one at port 8080 that is only accessible from
--http, but creates an HTTPS server. Use
--cipherlistto configure SSL for this server.
--password=PWas it allows using file protection to avoid leaking the password. The file is read before the server drops privileges when started with the
--no-forkand presents the Prolog toplevel after starting the server.
kill -HUP <pid>. Default is
reload(running make/0). Alternative is
quit, stopping the server.
Other options are converted by argv_options/3 and passed to http_server/1. For example, this allows for:
http_daemon/0 is defined as below. The start code for a specific server can use this as a starting point, for example for specifying defaults or additional options. This uses guided options processing from argv_options/3 from library(main). The option definitions are available as http_opt_type/3, http_opt_help/2 and http_opt_meta/2
http_daemon :- current_prolog_flag(argv, Argv), argv_options(Argv, _RestArgv, Options), http_daemon(Options).
Error handling depends on whether or not
interactive(true) is in
effect. If so, the error is printed before entering the toplevel. In
non-interactive mode this predicate calls
http_server(Handler, Options). The default is provided by start_server/1.