Simple Prolog server with JavaScript client

Nice. :slightly_smiling_face:

I like the use of fetch and addressing CORS.

Thanks for finding all of the debug/1 options, e.g. debug(cookie).

I was planning on trying a callback to the SWI-Prolog server for use with Cytoscape.js so will definitely be looking at your code with a fine tooth comb. Will give feedback as I find it.


The first bit of feedback is the choice of license.

I find that most JavaScript examples have a MIT license (ref) and the SWI-Prolog uses Simplified BSD license (ref)


While the GitHub page notes how to use it with SWI-Prolog on Ubuntu it also works on Windows 10.

Details

From DOS prompt

Microsoft Windows [Version 10.0.19041.450]
(c) 2020 Microsoft Corporation. All rights reserved.

C:\Users\Groot>cd "C:/Users/Groot/Documents/swipl-server-js-client"

C:\Users\Groot\Documents\swipl-server-js-client>swipl simple_server.pl --port=9999 --staticdir=static
% static dir: static
% Started server at http://localhost:9999/
% Starting REPL ...
?-
% [Thread httpd@9999_5] [1] get / ...
% [Thread httpd@9999_5] [1] 302 moved_temporary(/static/simple_client.html); 394 bytes
% [Thread httpd@9999_1] [2] get /static/simple_client.html ...
% [Thread httpd@9999_1] [2] 200 file(text/html,c:/users/groot/documents/swipl-server-js-client/static/simple_client.html); 2,084 bytes
% [Thread httpd@9999_2] [3] get /static/simple_client.css ...
% [Thread httpd@9999_2] [3] 200 file(text/css,c:/users/groot/documents/swipl-server-js-client/static/simple_client.css); 40 bytes
% [Thread httpd@9999_2] [4] get /static/simple_client.js ...
% [Thread httpd@9999_2] [4] 200 file(text/javascript,c:/users/groot/documents/swipl-server-js-client/static/simple_client.js); 4,161 bytes
% [Thread httpd@9999_2] [5] get /static/favicon.ico ...
% [Thread httpd@9999_2] [5] 200 file(image/x-ico,c:/users/groot/documents/swipl-server-js-client/static/favicon.ico); 32,038 bytes
% [Thread httpd@9999_5] [6] post /json ...
% [Thread httpd@9999_5] [6] 200 OK (0.016 seconds; 20,168 bytes)
% [Thread httpd@9999_4] [7] post /json ...
% [Thread httpd@9999_4] [7] 200 OK (0.000 seconds; 1,811 bytes)
% [Thread httpd@9999_1] [8] post /json ...
% [Thread httpd@9999_1] [8] 200 OK (0.000 seconds; 20,168 bytes)

Using Microsoft Edge


For those looking to start the code from the Prolog top level, so they can use make/0 and such, here is a modified predicate

Details
simple_server_main(Opts0) :-
    dict_create(Opts, opts, Opts0),
    assert_server_locations(Opts),
    http_server([port(Opts.port), workers(5)]),
    debug(log, 'Starting REPL ...', []),
    prolog.  % REPL - terminated with exit.

Don’t forget to export the predicate:

:- module(simple_server,[
        simple_server_main/0,
        simple_server_main/1,
        simple_server_main2/0
    ]).

Example run:

Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.7)

?- working_directory(_,'C:/Users/Groot/Documents/swipl-server-js-client').
true.

?- [simple_server].
true.

?- simple_server_main([port(9999),staticdir(static)]).
% static dir: static
% Started server at http://localhost:9999/
% Starting REPL ...
% Break level 1
[1]  ?- 
% [Thread httpd@9999_3] [1] get /static/simple_client.html ...
% [Thread httpd@9999_3] [1] 304 not_modified; 0 bytes
% [Thread httpd@9999_3] [2] get /static/favicon.ico ...
% [Thread httpd@9999_3] [2] 200 file(image/x-ico,c:/users/groot/documents//swipl-server-js-client/static/favicon.ico); 32,038 bytes
% [Thread httpd@9999_4] [3] post /json ...
% [Thread httpd@9999_4] [3] 200 OK (0.000 seconds; 141 bytes)