Creating a RESTful web service with KRL
Phil Windley’s example of creating a blog using KRL got me thinking about what other (somewhat unconventional) things could be done with KRL. The first that came to my mind is building a RESTful web service.
The advantages of setting up a web service this way include zero configuration (just write a ruleset and deploy it), awesome integration with data sources all over the web, an extensible rule-based language, and drop-dead simple persistent data storage.
I put together a simple example. It has four methods:
- echo Simply echoes back the “body” parameter
- sum Takes two parameters (“first” and “second”) and returns their sum
- counter Maintains a global (application-wide) counter, incrementing it each time and returning the value
- ent_counter Same as the normal counter, except it is maintained on a entity basis (requires cookies, so it only really works in a web browser)
I’m using webhooks to implement this. The URLs aren’t super pretty, and they probably wouldn’t be desirable as endpoints for a production system. But it’s easy enough to build a proxy in front of them.
You can try out this web service with the following URLS:
- http://webhooks.kynetxapps.net/h/a163x85/echo?body=hi%20there
- http://webhooks.kynetxapps.net/h/a163x85/sum?first=1&second=2
- http://webhooks.kynetxapps.net/h/a163x85/counter
- http://webhooks.kynetxapps.net/h/a163x85/ent_counter
Here’s the source code:
Many thanks to Sam for his answer yesterday on StackOverflow.