A new version of
SHP.plt is now available via planet. This is a major rewrite of SHP and provides two main upgrades:
- a "web API" interface - your web script can now be exposed as an "API" (think XMLRPC/JSON), and it automatically works with either XMLRPC or JSON (details below)
- general performance enhancement - the scripts are now compiled and cached to reduce disk IO. If the scripts are updated then they are automatically recompiled
As usual, the code is released under LGPL.
Installation
(require (planet bzlib/shp:1:3))
SHP requires some newer dependencies (
bzlib/base:1:6,
bzlib/date:1:3,
bzlib/parseq:1:3,
bzlib/xml:1:3,
bzlib/mime:1:0), and the current versions of PLT Scheme and Racket have issues with version dependencies (
the link: module mismatch bug), so you might have to clear out the planet cache and recompile them again.
As usual, SHP comes with a small example site that you can play with under the example sub directory - cd to the example directory and run
(require "web.ss")
will start the example site. The example site is still just trivial code right now - it will eventually be enhanced and separated into its own package.
Cached Compiled Script
All of the scripts are now compiled and cached. This has some potential performance benefit, since we will only access the file content when the file timestamp changes (meaning the file has been touched and/or modified). As this is a non-visible feature, we won't spend much time discussing it, except to note that the change is not just done for performance reasons - it is also done to enable and simplify the design of web api, which is discussed below.
Web API
Under the example site you can find the script
shp/api/add2
, which contains the following:
;; -*- scheme -*- -p
(:api-args (a number?) (b number?))
(+ a b)
This is the new *web api* - it takes in 2 numbers,
a
and
b
, and return the added result. To write an api script, you must use the
:api-args
expression, and then supply the arguments inside. The arguments can be specified in the following forms:
(:api-args a b) ;; both a & b are non-validating and you get what's passed in
(:api-args (a number?) (b string?)) ;; a expects a number, and b expects a string
(:api-args (a number? 3) (b number? 5)) ;; a & b both expect numbers, and both have default values if they are not passed in (a defaults to 3, and b defaults to 5).
When you run the example site you can access the api via the following http call:
GET /api/add2 HTTP/1.0
When running the above in browser you should get back an XMLRPC response:
Content-Type: text/xml; charset=utf-8
<methodResponse>
<fault>
<value>
<string>required: a</string>
</value>
</fault>
</methodResponse>
XMLRPC is the default response mode for web APIs. What it returns by default as shown above is an error message, because neither
a
or
b
is passed in.
To pass in the values - you just need to specify them in the query string as following: