Simple Web

Easy, simple websites with Prolog.

Example basic use

Create a directory with app.pl

:- use_module(sw/simple_web).

sw:route(home, '/', _Request) :-
    reply_html("<h1>Hello, world!</h1>
                <img src='static/images/logo.jpg' alt='logo'/>").

sw:route(template, '/test', _Request) :-
    Data = data{ title: 'Hello'
               , items: [ item{ title: 'Item 1', content: 'Abc 1' }
                        , item{ title: 'Item 1', content: 'Abc 2' }
                        ]
               },
    reply_template(test, Data).

sw:route(termarized, root(termerized), _Request) :-
    reply_html([title('Termerized')], [h1('Termerized Example'), p('With some text')]).

sw:route(api, '/api', method(get), _Request) :-
    reply_json_dict(data{example: "Hello, world!"}).

:- run([port(5000)]).

Inside this root directory, create a folder called static to save your static files, such as your css, javascript and images. Place an image called logo.jpg into static/images/.

Also inside the root directory, create a folder called templates, inside this place test.html

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1>{{= title }}</h1>
    {{ each items, item }}
        <h2>{{= item.title }}</h2>
        <div class="content">{{- item.content }}</div>
    {{ end }}
    <ul>
        <li><a href="{{= url_for('home') }}">Home</a></li>
        <li><a href="{{= url_for('termarized') }}">Termarized Example</a></li>
        <li><a href="{{= url_for('api') }}">API Example</a></li>
    </ul>
  </body>
</html>

Finally, run app.pl and navigate to http://localhost:5000

:~$ swipl app.pl

More Examples

A repository of examples, including using static, templates and creating an API can be found in the simple_web_examples repository.

author
- Paul Brown
version
- 0.1.4 */
license
- MIT
   79:- module(simple_web,
   80    [ run/0
   81    , run/1
   82    , read_json_dict/2
   83    , read_json_dict/3
   84    , reply_json_dict/1
   85    , reply_json_dict/2
   86    , reply_404/1
   87    , reply_404/2
   88    , reply_redirect/3
   89    , url_for/2
   90    ]
   91).   92
   93:-  ( user:file_search_path(sw_app, _) ->
   94      true
   95    ;
   96      working_directory(Dir, Dir),
   97      asserta(user:file_search_path(sw_app, Dir))
   98    ).   99
  100:- use_module(sw_config_handling).  101
  102:- reexport(sw).  103:- reexport(sw_static_handling).  104:- use_module(sw_route_handling).  105
  106:- reexport(sw_template_handling).  107:- use_module(library(http/http_json),
  108    [ http_read_json_dict/2 as read_json_dict
  109    , http_read_json_dict/3 as read_json_dict
  110    , reply_json_dict/1
  111    , reply_json_dict/2
  112    ]
  113).  114:- use_module(library(http/http_dispatch),
  115    [ http_404/2 as reply_404
  116    , http_redirect/3 as reply_redirect
  117    ]
  118).
 reply_404(Options, Request) is det
Arguments:
Options- as per http_404/2. ! reply_404(Request) is det. Respond with a 404, no options, as per http_404/2
  125reply_404(Request) :-
  126    http_404([], Request).
  127
  128:- use_module(library(http/thread_httpd)).  129:- use_module(library(http/http_dispatch)).  130:- ( get_config(debug, true) ->
  131     use_module(library(http/http_error))
  132   ; true).
 run is det
run the webserver on a random free port
  136run :-
  137    sw_run([port(_)]), !.
 run(+Options) is det
run the webserver with Options
Arguments:
Options- as per http_server/3
  143run(Options) :-
  144    is_list(Options),
  145    sw_run(Options), !.
  146run(Options) :-
  147    \+ is_list(Options),
  148    sw_run([Options]), !.
 sw_run(+Options) is det
init routes and run webserver
  152sw_run(Options) :-
  153    init_routes,
  154    http_server(http_dispatch, Options), !