Commit caa26efc authored by Paul's avatar Paul

Update for v0.2.0

parent ec1a57c0
# Simple Web Examples
This repository contains example projects demonstrating how to use
Simple Web.
Simple Web. Requires Simple Web version 0.2.0.
1. [Hello World](examples/hello_world)
2. [Handling Static](examples/static_eg)
......
......@@ -6,7 +6,7 @@ To make creating an API easier, Simple Web has included a
## Setup Your API
First we create a base url for our API with `http:location/3`, this
First we create a base url for our API with `:- location(Name, Url)`, this
lets us define our API routes with `api/1` as shown with
`api(get_number)`.
......@@ -27,6 +27,6 @@ The POST API route in the example can be tested with curl:
## JSON and Dicts
For more information about the `http_read_json_dict/2&3` predicates and
For more information about the `read_json_dict/2&3` predicates and
`reply_json_dict/1&2` predicates, please refer to the documentation
at `[library(http/http_json)](http://www.swi-prolog.org/pldoc/man?section=jsonsupport)`.
......@@ -5,7 +5,7 @@
:- use_module(library(sw/simple_web)).
http:location(api, "/api", []).
:- location(api, "/api").
sw:route(root(.), _Request) :-
reply_html("<a href='/api/get_number'>Go To API</a>").
......@@ -15,7 +15,7 @@ sw:route(api(get_number), method(get), _Request) :-
reply_json_dict(resp{number: Number, status: success}).
sw:route(api(send_number), method(post), Request) :-
http_read_json_dict(Request, Data),
read_json_dict(Request, Data),
NumStr = Data.get(number),
number_string(Number, NumStr),
Incr is Number + 1,
......
......@@ -55,4 +55,4 @@ your static directory.
Sometimes you need more options and more flexibility. That's why
Simple Web also exposes
`[http_reply_file/3](http://www.swi-prolog.org/pldoc/doc_for?object=http_dispatch%3Ahttp_reply_file/3)` for you. In the example `main.pl` file, we're using this for the '/favicon.ico' route to demonstrate it with caching. Check the linked docs for more information on using this predicate.
`[reply_file/3](http://www.swi-prolog.org/pldoc/doc_for?object=http_dispatch%3Ahttp_reply_file/3)` for you, and provides `reply_file/2` without the options. In the example `main.pl` file, we're using this for the '/favicon.ico' route to demonstrate it with caching. Check the linked docs for more information on using this predicate.
......@@ -16,7 +16,7 @@ sw:route(root(.), _Request) :-
%
% Respond with image file from static files, cache for speed
sw:route(root('favicon.ico'), Request) :-
http_reply_file('static/images/hedgehog.jpg', [cache(true)], Request).
reply_file('static/images/hedgehog.jpg', [cache(true)], Request).
serve :-
run(port(8000)).
......@@ -19,6 +19,6 @@ config(template_dir, NewTemplateDir).
# Config Support for Simple Templates
To save you from needing to pass options to `render_template/3` all
To save you from needing to pass options to `reply_template/3` all
the time, you can configure your own default options in the
`config.pl` file. These options are detailed in the Simple Web docs.
......@@ -14,14 +14,14 @@
sw:route('/', _Request) :-
post_titles(Titles),
render_template(blog, data{titles: Titles}).
reply_template(blog, data{titles: Titles}).
sw:route(root(Name), _Request) :-
blog_post(Name, Content),
Data = data{ name: Name
, content: Content
},
render_template(post, Data).
reply_template(post, Data).
serve :-
run(port(8000)).
......@@ -5,7 +5,7 @@
:- use_module(library(sw/simple_web)).
http:location(api, "/api", []).
:- location(api, "/api").
sw:route(api(message), method(get), _Request) :-
reply_json_dict(resp{msg: "Hello world from Simple Web API!"}).
......@@ -4,6 +4,10 @@
]
).
% Set sw_app to this directory so app can be run from any directory
:- prolog_load_context(directory, Dir),
asserta(user:file_search_path(sw_app, Dir)).
:- use_module(library(sw/simple_web)).
% For browser opening
......@@ -16,14 +20,14 @@
serve :-
% port(X) where X must match that in
% vueapp/config/index.js: proxyTable url
run(port(8000)).
run(port(8000)), !.
view :-
serve,
www_open_url("http://localhost:8000").
www_open_url("http://localhost:8000"), !.
% route '/' is only used in production, this will
% return the built index.html, which itself will
% request JavaScript and CSS from static.
sw:route(root(.), Request) :-
http_reply_file('vueapp/dist/index.html', [cache(true)], Request).
reply_file('vueapp/dist/index.html', [cache(true)], Request).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment