Running multiple HTTP Servers in SWI-prolog

You guys are lucky that I am extremly patient, immune to trolling attempts,
and goal oriented abbreviated as GO, not to be confused with OO.
Actually with enough coffee I am GO++.

A SWI-Prolog solution is rather straight forward a file demo8.p:

:- use_module(library(http/http_server)).

handler(_) :-
   write('content-type: text/plain;charset=utf-8'), nl,
   nl,
   write('Hello World!').

handler2(_) :-
   write('content-type: text/plain;charset=utf-8'), nl,
   nl,
   write('Hello World 2!').

:- http_server(handler, [port(8080)]).
:- http_server(handler2, [port(8082)]).

In terms of ADT was using http_server/2 instead of http_server/1 the
unary form is the same as http_server/2 with http_dispatch as first argument.
In the above I use two custom closures different from http_dispatch. In

terms of ADT the first initialization parameter is later part of the state of
the launched server object. Now run swipl and consult the file:

?- ['demo8.p'].
% Started server at http://localhost:8080/
% Started server at http://localhost:8082/
true.

Works fine so far:

image
image

1 Like