plugin

package
v0.15.2-0...-1d34bd1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2021 License: AGPL-3.0-only Imports: 16 Imported by: 0

README

Kopano API kvs plugin

The kvs plugin provides a key/value store system HTTP API to store data persistently.

In its current state, the kv store can only store documents with size up to 16 KiB and will not return more than 500 documents for recursive get requests. This is by design - if that does not fit your storage requirements, either reorganize the data set or kvs is not for you.

Configuration

KOPANO_KVS_DB_DRIVER is an environment variable which defines what backend database driver to use for persistent storage. Currently supported drivers are sqlite3 and mysql.

KOPANO_KVS_DB_DATASOURCE is an environment variable which describes the data source name (DNS) of the backend database. The exact value depends on the used database driver. For sqlite3 the value is the full path to a database file and for mysql it isa MYSQL DSN (See https://github.com/go-sql-driver/mysql#dsn-data-source-name) for details.

KOPANO_KVS_DB_MIGRATIONS is an environment variable pointing to the folder where to find database migrations. It must point to a folder which contains a sub folder which name matches the configured database driver.

KOPANO_KVS_ALLOW_CORS is an environment variable which if set to 1 enables CORS (Cross Origin Resource Sharing) HTTP requests and headers so that the REST endpoints provided by this plugin can be used from a browser cross origin.

KOPANO_KVS_REQUIRED_SCOPES is an environment variable which defines the required access token scopes to grant access to the API endpoints provided by this plugin. By default the scopes are kopano/kvs.

HTTP API v1

The base URL to this API is /api/kvs/v1. All example URLs are sub paths of this base URL.

Authentication

All endpoints of the kvs API require OAuth2 Bearer authentication with an access token (if not stated otherwise).

Realms

The kvs data set is grouped into realms and keys. Currently the only supported realm is user which limits data access to particular users. The key is the URL to a specific document. It can contain slashes to group individual keys loosely together. The first segment of such a key path becomes a collection and all keys with the same collection can be fetched efficiently using a recursive query.

Create or Update

Creates or replaces a document identified by key with the given value.

PUT /kv/${realm}/${collection/key/path}
200
PUT /kv/${realm}/${collection/prefix}?batch=1
200

The realm is the realm which defines kv storage behavior. Rest of the URL is the key where the first part becomes the collection.

If the batch parameter is given, the kvs accepts a JSON array as input data similar to what is returned by Get in recurse=1 mode. All keys of the entries will have the collection and prefix used from the URL. Batch mode is transactional and will only complete if all entries have been written using ReadCommited transaction isolation. Entry values must be Base64 encoded if the entry content_type attribute is not application/json.

Get

Retrieves the item identified by realm and key path.

GET /kv/${realm}/${collection/key}
200
{
	"key": "${collection/key}"
	"value": "whatever was stored, Base64 encoded if not JSON",
	"content_type": "application/json"

}
GET /kv/${realm}/${collection}?recurse=1
200
[
	{
		"key": "${collection}/first-doc",
		"value": {},
		"content_type": "application/json"
	},
	{
		"key": "${collection}/other-doc",
		"value": {},
		"content_type": "application/json"
	}
]
GET /kv/${realm}/${collection/key}?raw=1
200
{}
GET /kv/${realm}/${collection/non/existing/key}
404
Supported parameters for Get requests

?raw=1 returns the raw document as stored instead of the JSON envelope. ?recurse=1 returns a JSON array returning all keys which have the same prefix as the given key. For efficient use, limit this to collections. You can use more specific prefixes for nested keys but keep in mind that the result set is filtered after it was received from storage.

Delete

Removes the item from storage so it can no longer be retrieved.

DELETE /kv/${realm}/${collection/key}
200
DELETE /kv/${realm}/${collection/key}
404
Usage examples

This assumes you have curl in your path.

$ export KVS_HOST=https://localhost:8428
$ export TOKEN_VALUE=<access_token>
$ curl -s -k -XPUT -H "Authorization: Bearer $TOKEN_VALUE" "$KVS_HOST/api/kvs/v1/kv/user/test1/doc1" -H "Content-Type: application/json" --data '{}'
$ curl -s -k -H "Authorization: Bearer $TOKEN_VALUE" "$KVS_HOST/api/kvs/v1/kv/user/test1/doc1"
{
  "key": "test1/doc1",
  "value": {}
}
$ curl -s -k -XDELETE -H "Authorization: Bearer $TOKEN_VALUE" "$KVS_HOST/api/kvs/v1/kv/user/test1/doc1"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Register plugins.RegisterPluginV1 = func() plugins.PluginV1 {
	return &KVSPlugin{
		quit: make(chan struct{}),
	}
}

Register is the exported registration entry point as loaded by Kopano API to register plugins.

Functions

This section is empty.

Types

type KVSPlugin

type KVSPlugin struct {
	// contains filtered or unexported fields
}

KVSPlugin implements a key value store for Kopano API.

func (*KVSPlugin) Close

func (p *KVSPlugin) Close() error

Close closes the accociated plugin.

func (*KVSPlugin) Info

func (p *KVSPlugin) Info() *plugins.InfoV1

Info returns the accociated plugins plugin.Info.

func (*KVSPlugin) Initialize

func (p *KVSPlugin) Initialize(ctx context.Context, errCh chan<- error, srv plugins.ServerV1) error

Initialize initizalizes the accociated plugin.

func (*KVSPlugin) MakeHTTPUserKVHandler

func (p *KVSPlugin) MakeHTTPUserKVHandler(router *mux.Router) http.Handler

MakeHTTPUserKVHandler creates the HTTP handler for the per user kv store.

func (*KVSPlugin) ServeHTTP

func (p *KVSPlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) (bool, error)

ServeHTTP serves HTTP requests.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL