karigo

package module
v0.0.0-...-be7478f Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2020 License: GPL-3.0 Imports: 19 Imported by: 0

README

karigo

karigo is an API engine that follows the JSON:API specification.

State

This is a work in progress.

Documentation

Documentation will be provided when the API is more stable.

Contributing

Contributions are not accepted at the moment.

Documentation

Overview

Package karigo provides an HTTP API framework where the business logic is executed through transactions appended to an ordered log. The storage is seen as a list of keys and values and the transactions are simply a list of updates to apply to the keys.

An ordered log is an easy data structure to implement and mutate. In karigo, the log is meant to be exposed and handled directly. Components like search indexes and caches can simply read the log to stay up-to-date. The log can also be easily replicated through a consencus algorithm like Raft to improve avalaibility.

Index

Constants

View Source
const (
	GET    = "GET"
	POST   = "POST"
	PATCH  = "PATCH"
	DELETE = "DELETE"
)

Methods

Variables

View Source
var (
	// ErrCantDo should be returned by a source that is given a valid query
	// that it is unable to do.
	ErrCantDo = errors.New("can't do the given query")

	// ErrNotFound is returned when an endpoint does not exist.
	ErrNotFound = errors.New("resource not found")

	// ErrUnexpected is returned when an unexpected error occurs.
	ErrUnexpected = errors.New("unexpected error")

	// ErrNotImplemented is returned when an endpoint exists but is not
	// implemented.
	ErrNotImplemented = errors.New("not implemented")
)

Functions

func ActionDefault

func ActionDefault(cp *Checkpoint)

ActionDefault ...

func FirstSchema

func FirstSchema() *jsonapi.Schema

FirstSchema ...

Types

type Action

type Action func(*Checkpoint)

Action ...

type Checkpoint

type Checkpoint struct {
	Res jsonapi.Resource
	// contains filtered or unexported fields
}

Checkpoint ...

func (*Checkpoint) Apply

func (c *Checkpoint) Apply(ops []query.Op)

Apply ...

func (*Checkpoint) Check

func (c *Checkpoint) Check(err error)

Check ...

func (*Checkpoint) Collection

func (c *Checkpoint) Collection(qry query.Col) jsonapi.Collection

Collection ...

func (*Checkpoint) Fail

func (c *Checkpoint) Fail(err error)

Fail ...

func (*Checkpoint) Resource

func (c *Checkpoint) Resource(qry query.Res) jsonapi.Resource

Resource ...

type Config

type Config struct {
	// Server
	Port uint

	// Node
	Hosts   []string
	Journal map[string]string
	Sources map[string]map[string]string
}

Config ...

type Journal

type Journal interface {
	Service

	// Reset empties the whole journal. The next append
	// will add an entry add index 0.
	Reset() error

	// Append appends an entry to the journal.
	Append([]byte) error

	// Oldest returns the oldest known entry.
	Oldest() (uint, []byte, error)

	// Newest returns the newest entry.
	Newest() (uint, []byte, error)

	// At returns the entry indexed at i, or none if it
	// does not exist.
	At(i uint) ([]byte, error)

	// Cut removes all entries from the oldest one to
	// the one at i minus one.
	//
	// If i is lower than the oldest known index,
	// nothing gets cut. If i is greater than the newest
	// index, it will be interpreted as the newest index,
	// and therefore everything will be cut except i,
	// leaving a journal of length one.
	Cut(i uint) error

	// Range returns a slice of entries from indexes f
	// to t (inclusively).
	//
	// It returns an error if it can't return the range,
	// whether it is because the journal's history starts
	// after f or t is greater than the newest index.
	Range(f uint, t uint) ([][]byte, error)
}

Journal ...

type Node

type Node struct {
	Name string

	Hosts   []string
	Journal map[string]string
	Sources map[string]map[string]string

	sync.Mutex
	// contains filtered or unexported fields
}

Node ...

func NewNode

func NewNode(config Config) *Node

NewNode ...

func (*Node) Handle

func (n *Node) Handle(r *Request) *jsonapi.Document

Handle ...

type Request

type Request struct {
	ID string

	Method string
	URL    *jsonapi.URL

	Body []byte
	Doc  *jsonapi.Document

	Logger zerolog.Logger
}

Request ...

type Server

type Server struct {
	Config

	Nodes map[string]*Node
	// contains filtered or unexported fields
}

Server ...

func NewServer

func NewServer() *Server

func (*Server) DisableLogger

func (s *Server) DisableLogger()

func (*Server) Run

func (s *Server) Run()

Run ...

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP ...

func (*Server) SetOutput

func (s *Server) SetOutput(w io.Writer)

type Service

type Service interface {
	// Connect instantiates a connection to the service behind
	// the source.
	//
	// Implementations should document the elements they expect
	// in the map.
	Connect(map[string]string) error

	// Ping reports whether there is an active connection or not.
	Ping() bool
}

A Service is the interface to handle external services, whether they are on the same host or another one.

This is used for managing journals and sources. If an implementation does not represent an external service, the methods can pretend every is fine. For example, Connect could always return nil.

type Source

type Source interface {
	Service

	// Reset wipes all data and brings the underlying database
	// to a clean state.
	//
	// A clean state represents the given schema.
	Reset(*jsonapi.Schema) error

	// NewTx returns a new Tx object.
	NewTx() (query.Tx, error)
}

A Source is the interface used by karigo to query data from a database and apply operations to mutate the data.

Each of the interface's methods have comments to explain how to implement a Source. See the current implementations for more details.

Directories

Path Synopsis
cmd
drivers
internal

Jump to

Keyboard shortcuts

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