Learn you some InterMine Web Services for good.
##Quickstart
Install Docker and test all is well:
$ docker run ubuntu /bin/echo hello world
Create an intermine
image based on a Dockerfile
:
$ docker build -t intermine server/
Install service dependencies:
$ npm install
Start the service:
$ sudo PORT=5000 node index.js
##Commands
###Update image
While your container is still running, get its id and save it as an image:
$ docker commit <id> intermine
$ docker images
Now we have an image saved under intermine
. You can now exit from the container.
You can also edit the Dockerfile
in server/
and rerun the abovementioned command to update the image.
###Languages/environments
The following command in server/run.sh
is being run for each new job:
$ echo "$1" | sudo docker run -i intermine /bin/bash -c "$2"
The first argument is a script source, the second is one command out of server/config.coffee
e.g.:
$ cat > script.js; node script.js
###Production
$ npm install pm2 -g
$ PORT=80 pm2 start index.js
##API
You can post some code to get a job id back:
#!/usr/bin/env coffee
restify = require 'restify'
client = restify.createJsonClient
'url': 'http://0.0.0.0:5000'
client.post '/api/v1/jobs.json',
'lang': 'javascript'
'src': 'console.log(3*6)'
, (err, req, res, body) ->
throw err if err
###Design
- Use plural nouns, not verbs.
- Provide a
message
when handling errors. - Only 3 levels of depth when showing associations, e.g.:
/owners/123/dogs
. - All responses contain a
responseCode
. - Be able to override status codes and always return
200
in the header but not in the body; use?suppress_response_codes=true
. - Codes:
- 200 OK
- 201 Created: when submitting new items
- 400 Bad Request: it is the client's fault, like params not provided
- 401 Not Authorized: you can access this page, but your token is no good
- 404 Not Found
- 500 Internal Server Error: client is fine, we are at fault
- Do versioning, but do not change versions often.
- Never respond with a JSON Array, always an Object.
- Allow people to specify format in a suffix
.json
, but default to JSON. - Specify which fields to return using query param
?fields=name,color
. - When returning a listing of many things, return only a partial and assume that
?limit=10&offset=0
. Metadata related to total count, limit & offset should be returned undermetadata
key in response. - Search is done by appending
?q=wulfie
to the resource or/search?q=wulfie
for a global one. - When returning timestamps use
toJSON()
so we can nicely debug in the browser. - Key names in responses (JSON) need to follow the same pattern as those in URLs so
created_at
. - When creating objects, it should return them.
- When returning an object or an array, return it under
data
key. - Support trailing slashes in urls.