Skip to content

hishamhm/restserver

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

RestServer

A simple server API for writing REST services running over WSAPI.

A plugin for launching on Xavante is provided. Currently, it is the only way to run this out-of-the box.

Installing

luarocks install restserver-xavante

Using

Check the todo-list example in the examples/ directory. It should be easy to follow!

The server is a Lua script and the example client exercising the REST API provided by the server is a shell script powered by Curl.

REST handlers

GET - No authentication

function handler(queryParams, pathParam1, pathParam2,...)
  • queryParams is an object with key-value pairs the parameters of the query or is an empty object {}
  • one or more pathParam* arguments may be present as second, third arguments etc. and represent the parameters passed in the path as {paramname: syntax}

Examples

GET /server/query?a=1&b=2
handler({a=1,b=2})

GET /server/user/{id:[0-9]+}/items/{itemid:[0-9]+}
handler({},id,itemid)

GET - Authenticated

function handler(queryParams, user, pathParam1, pathParam2,...)

If authentication is enabled, i.e. an authentication function was specified in add_resource(), the username is present as an additional parameter in the handler().

Examples

GET /server/query?a=1&b=2
handler({a=1,b=2}, usere)

GET /server/user/{id:[0-9]+}/items/{itemid:[0-9]+}
handler({},user, id,itemid)

POST - No authentication

function handler(postBody, pathParam1, pathParam2, ...)
  • postBody - the post body in application/json content type. Form posts are not supported.
  • pathParamX - zero or more path parameters follow

Note, query parameters are not supplied in this type of handler.

Examples

path: /server/people 

POST /server/people
--> {"name":"Alice"}

handler({name=Alice})

path: /server/people/{name:[a-zA-Z]+}/items

POST /server/people/Alice/items
--> {"name":"Laptop"}

handler({name=Laptop},Alice)

POST - Authenticated

function handler(postBody, username, pathParam1, pathParam2, ...)
  • postBody - the post body in application/json content type. Form posts are not supported.
  • username - authenticated username
  • pathParamX - zero or more path parameters follow

Again, query parameters are not supplied in this type of handler.

Examples

path: /server/people 

POST /server/people
--> {"name":"Alice"}
headers: Authorization: Basic Ym9iOnNlY3JldA==  - i.e. bob:secret

handler({name=Alice},"bob")

path: /server/people/{name:[a-zA-Z]+}/items

POST /server/people/Alice/items
--> {"name":"Laptop"}
Authorization: Basic Ym9iOnNlY3JldA== - i.e. bob:secret

handler({name=Laptop},Bob,Alice)

Authors and Maintainance

Hisham Muhammad - @hisham_hm - http://hisham.hm/
Currently maintained by Orestis Tsakiridis - @otsakir - https://twitter.com/otsakir

License

MIT/X11. Enjoy! :-D

About

A simple server API for writing REST services running over WSAPI

Resources

License

Stars

Watchers

Forks

Packages

No packages published