-
Notifications
You must be signed in to change notification settings - Fork 40
/
new_abstract_path.pl
50 lines (40 loc) · 1.81 KB
/
new_abstract_path.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
%
% Add another abstract base location like root
% this one is files, and we map it to /f,
% so /f/limburger can be described as files(limburger)
:- multifile http:location/3.
:- dynamic http:location/3.
http:location(files, '/f', []).
:- http_handler(files(bunny), say_bunny , []).
% Declare a handler, binding an HTTP path to a predicate.
% The notation root(hello_world) uses an alias-mechanism similar to
% absolute_file_name/3 and allows for moving parts of the server locations
% easily. See http_absolute_location/3.
:- http_handler(root(.), say_hi, []).
% And, just for clarity, define a second handler
% this one can by reached at http://127.0.0.1:8000/taco
:- http_handler(root(taco), say_taco, []).
% The predicate server(?Port) starts the server. It simply creates a
% number of Prolog threads and then returns to the toplevel, so you can
% (re-)load code, debug, etc.
server(Port) :-
http_server(http_dispatch, [port(Port)]).
/* The implementation of /hello_world. The single argument provides the request
details, which we ignore for now. Our task is to write a CGI-Document:
a number of name: value -pair lines, followed by two newlines, followed
by the document content, The only obligatory header line is the
Content-type: <mime-type> header.
Printing can be done using any Prolog printing predicate, but the
format-family is the most useful. See format/2. */
say_hi(_Request) :-
format('Content-type: text/plain~n~n'),
format('Hello World!~n').
say_taco(_Request) :-
format('Content-type: text/plain~n~n'),
format('Tacos are yummie!~n').
say_bunny(_Request) :-
format('Content-type: text/plain~n~n'),
format('bunnies are cute!~n').