New CouchDB API - npolar/api.npolar.no GitHub Wiki
This pages documents manual configuration, not recommended for most endpoints. See New-API for how to setup a JSON API.
As Step 1 in the Publishing guide, here's how you might create a brand new API endpoint.
We assume that the API will run on https://api.example.com, CouchDB on https://couchdb.example.com:6984
Create CouchDB database
$ curl -kn -X PUT https://couchdb.example.com:6984/gcmd_concept
{"ok":true}
/gcmd/concept
#config.ru for the API on api.example.com
map "/gcmd" do
map "/concept" do
run Npolar::Api::Core.new(Gcmd::Concept.new, :storage => Npolar::Storage::Couch.new("gcmd_concept"))
end
end
Test that you get an empty collection ([]
) with
$ curl -XGET http://api.npolar.no/gcmd/concept/.json
[]
Note that the convenient Npolar::Storage::Couch.new("gcmd_concept")
only works if you set the NPOLAR_API_COUCHDB
environmental variable, as explained in Configuration.
Hint
ssh [email protected]
... or ssh api.example.com
with you regular user and do sudo su - api
to change to the api
user
Auth
For authentication and authorization, we are going to use the Authorizer with a CouchDB user database.
Add Authorizer
use Npolar::Rack::Authorizer, { :auth => Npolar::Auth::Couch.new("api_user"), :system => "gcmd",
:except? => lambda {|request| ["GET", "HEAD"].include? request.request_method }
}
run Npolar::Api::Core.new(Gcmd::Concept.new, :storage => Npolar::Storage::Couch.new("gcmd_concept"))
Add gcmd
to your user's roles like this (excerpt):
"roles":{"gcmd":["editor"]}