middleware

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2020 License: MIT Imports: 5 Imported by: 0

README

Resgate logo

Middleware for Go RES Service
Synchronize Your Clients

License Reference Status


This package is deprecated, please use the store package instead.

The store interface provides superior structure for building services that scales well.

Documentation

Overview

Package middleware provides middleware for the res package:

https://github.com/jirenius/go-res

Middleware can be used for adding handler functions to a res.Handler, to perform tasks such as:

* storing, loading and updating persisted data * synchronize changes between multiple service instances * add additional logging * provide helpers for complex live queries

Currently, only the BadgerDB middleware is created, to demonstrate database persistence.

Usage

Add middleware to a resource:

s.Handle("user.$id",
	middlware.BadgerDB{DB: db},
)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadgerDB

type BadgerDB struct {
	// BadgerDB database
	DB *badger.DB
	// Default resource value if not found in database. Will return res.ErrNotFound if not set.
	Default interface{}
	// Type used to marshal into when calling r.Value() or r.RequireValue().
	Type interface{}
}

BadgerDB provides persistence to BadgerDB for the res Handlers.

It will set the GetResource and Apply* handlers to load, store, and update the resources in the database, using the resource ID as key value.

Example
package main

import (
	"github.com/dgraph-io/badger"
	res "github.com/jirenius/go-res"
	"github.com/jirenius/go-res/middleware"
)

func main() {
	db := &badger.DB{} // Dummy. Use badger.Open

	s := res.NewService("directory")
	s.Handle("user.$id",
		res.Model,
		middleware.BadgerDB{DB: db},
		/* ... */
	)
}
Output:

func (BadgerDB) SetOption

func (o BadgerDB) SetOption(hs *res.Handler)

SetOption is to implement the res.Option interface

func (BadgerDB) WithDB

func (o BadgerDB) WithDB(db *badger.DB) BadgerDB

WithDB returns a new BadgerDB value with the DB set to db.

func (BadgerDB) WithDefault

func (o BadgerDB) WithDefault(i interface{}) BadgerDB

WithDefault returns a new BadgerDB value with the Default resource value set to i.

Example
package main

import (
	"github.com/dgraph-io/badger"
	res "github.com/jirenius/go-res"
	"github.com/jirenius/go-res/middleware"
)

func main() {
	db := &badger.DB{} // Dummy. Use badger.Open

	s := res.NewService("directory")
	badgerDB := middleware.BadgerDB{DB: db}
	s.Handle("users",
		res.Collection,
		// Default to an empty slice of references
		badgerDB.WithType([]res.Ref{}).WithDefault([]res.Ref{}),
		/* ... */
	)
}
Output:

func (BadgerDB) WithType

func (o BadgerDB) WithType(v interface{}) BadgerDB

WithType returns a new BadgerDB value with the Type value set to v.

Example
package main

import (
	"github.com/dgraph-io/badger"
	res "github.com/jirenius/go-res"
	"github.com/jirenius/go-res/middleware"
)

func main() {
	db := &badger.DB{} // Dummy. Use badger.Open

	type User struct {
		ID   int    `json:"id"`
		Name string `json:"name"`
	}

	s := res.NewService("directory")
	badgerDB := middleware.BadgerDB{DB: db}
	s.Handle("user.$id",
		res.Model,
		badgerDB.WithType(User{}),
		res.Set(func(r res.CallRequest) {
			_ = r.RequireValue().(User)
			/* ... */
			r.OK(nil)
		}),
	)
}
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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