Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "simple_web"

Title:Microframework for building websites
Rating:Not rated. Create the first rating!
Latest version:0.2.1
SHA1 sum:1d374cfcb17ce8a90fbc283a422fcc19b7c9ea77
Author:Paul Brown <paul@paulbrownmagic.com>
Home page:https://gitlab.com/PaulBrownMagic/simple_web
Download URL:https://gitlab.com/PaulBrownMagic/simple_web/*
Requires:simple_template

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.1.051090a8b31a72ffe346985f16dabeceaa4c3bb8d1https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.0/simple_web-0.1.0.zip
0.1.33d68a216d3deb27d52f992beb853e242942dd6b73https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.3/simple_web-0.1.3.zip
0.1.4feeaa4d0cb1dd85f25756151580e9c9d5f82cffd2https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.1.4/simple_web-0.1.4.zip
0.2.006cda0edbe2a58acc947a30dfa152138034720b72https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.2.0/simple_web-0.2.0.zip
0.2.11d374cfcb17ce8a90fbc283a422fcc19b7c9ea772https://gitlab.com/PaulBrownMagic/simple_web/-/archive/0.2.1/simple_web-0.2.1.zip

Simple-Web Framework

Easy, simple websites with Prolog.

Simply declare your routes as predicates and run the server. Simple-Web aims to help make web development simpler and easier. It does this by being opinionated. The scope of SWI-Prolog's capabilities are narrowed to a core subset, it requires a wise directory structure for static files, and it expects that you'll use simple_template for clever work with HTML.

Setup

Simple-Web depends on simple-templates, this can be installed via

?- pack_install(simple_template).

Example Basic Use

Create a directory with app.pl

:- use_module(sw/simple_web).

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

sw:route('/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, options{cache:true}).

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

sw:route('/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 }}
  </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.

Predicates

  • run/0: run the webserver on a random, free, high numbered port
  • run/1: run the server with options as per http_server/3
  • route/2: Declare a route: sw:route(Path, Request) where Request is a list of attr(val)
  • route/3: Declare a route: sw:route(Path, method(M), Request)
  • reply_html/1: Reply with a string assumed to be valid html
  • reply_template/2: Use simple_template to reply: reply_template(Template, DataDict)
  • read_json_dict/2 &/3: Read request JSON into dict. See http_read_json_dict.
  • reply_json_dict/1 & 2: Convert dict to JSON and reply. See read_json_dict
  • reply_file/3: Reply with a file. See http_reply_file/3
  • location/2 &/3: Define a new URL location, as per http:location(api, "/api", []), allows you to declare routes with sw:route(api(all_data), method(get), _Request), where api(all_data) is equivalent to `/api/all_data`. See http:location/3
  • reply_404/1 &/2: Return a 404, eg: reply_404(Request). See http_404/2
  • reply_redirect/3: Redirect to another page, eg: reply_redirect(How, Where, Request). How is one of moved, moved_temporary or see_other. Where can be an atom ('/some/page'), or an aliased path (root(some/page)). See http_redirect/3

Config Options

You can optionally create a config.pl file in the root directory of your application to change the default values:

For simple_web:

  • config(debug, true). Turn on http/http_error. Default false
  • config(static_dir, "/static"). Name of the directory in the web application root directory to serve static files from. Default "/static"
  • config(templates_dir, "/templates"). Name of the directory in the root directory in which templates are stored. *Default "/templates"*

    For simple_template, these are set to the same default's as simple_template and are documented by simple_template:

  • config(st_encoding, utf8).
  • config(st_extension, html).
  • config(st_cache, false).
  • config(st_strip, false).
  • config(st_frontend, simple).
  • config(st_undefined, error).

Contents of pack "simple_web"

Pack contains 9 files holding a total of 17.2K bytes.