1:- module(dotcloud, [server/1, server/2]). 2:- reexport(library(http/http_dispatch)). 3 4:- use_module(library(http/thread_httpd)). 5:- use_module(library(http/http_dispatch)).
For convenience during development, if the environment variable 'PORT_$Service' is not set, listen on port 3000.
16:- meta_predicate server( , ). 17server(ServiceAtom, Goal) :- 18 % on which port should we listen? 19 upcase_atom(ServiceAtom, ServiceName), 20 format(atom(Env), 'PORT_~w', [ServiceName]), 21 getenv_default(Env, '3000', PortAtom), 22 atom_number(PortAtom, Port), 23 24 % start an HTTP server there 25 http_server(Goal, [port(Port), keep_alive_timeout(1)]), 26 format('Listening on port ~d~n', [Port]), 27 format(atom(Thread), 'http@~d', [Port]), 28 29 % wait for the server to exit 30 thread_join(Thread, _).
36server(ServiceAtom) :- 37 server(ServiceAtom, http_dispatch). 38 39 40% retrieve an environment variable with a fallback default value 41getenv_default(Name, Default, Value) :- 42 ( getenv(Name, Value0) -> 43 Value = Value0 44 ; true -> 45 debug(dotcloud, 'No ~w environment. Defaulting to ~w', [Name, Default]), 46 Value = Default 47 )