How to import modules from prolog #113
Replies: 1 comment
-
For the general answer, you can refer to the following Handbook section: https://logtalk.org/manuals/userman/predicates.html#calling-prolog-predicates For the particular case you asked, the first think to notice is that, inside an object (or category, https://logtalk.org/manuals/refman/directives/use_module_1.html The advised solution is to load any required Prolog modules from you application % HTTP and JSON libraries
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
% lambda library to simplify working with end-points
% (the http_handler/1 goal second argument is a closure that takes a request as argument)
:- use_module(library(yall)).
:- use_module(library(apply_macros)).
% load Logtalk resources
:- initialization((
set_logtalk_flag(optimize, on),
...,
logtalk_load(my_http_handler),
...
)).
% define end-points
:- initialization((
% root end-point
http_handler(/, [Request]>>(my_http_handler::request(root, Request)), [spawn([])]),
% other end-points
http_handler('/foo', [Request]>>(my_http_handler::request(foo, Request)), [spawn([])]),
http_handler('/bar', [Request]>>(my_http_handler::request(bar, Request)), [spawn([])]),
...
)).
% start the server
:- initialization(http_server(http_dispatch, [port(8888)])). Then perhaps in a :- object(my_http_handler).
:- public(request/2).
:- mode(request(+atom, ++nonvar), one).
:- info(request/2, [
comment is 'Handle endpoint requests.',
argnames is ['Endpoint', 'Request']
]).
:- use_module(http_json, [
http_read_json_dict/2, reply_json_dict/1, reply_json_dict/2
]).
request(EndPoint, Request) :-
http_read_json_dict(Request, Dict),
...
...
:- end_object. This is, of course, one of several possible scenarios. But hopefully will help you get started. |
Beta Was this translation helpful? Give feedback.
-
Hi I am wondering how I can use module http/thread_httpd from Prolog?
use_module(library(http/thread_httpd) is not working. Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions