karigo

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

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

Go to latest
Published: Jan 15, 2019 License: GPL-3.0 Imports: 16 Imported by: 0

README

karigo

karigo is an API framework based on jsonapi.

Here's a list of features supported by the framework:

  • Accept JSON API requests and return JSON API responses (jsonapi.org/format)
  • Manipulate resources, collections, and relationships
  • The Store interface can be implemented to use any DBMS (implementation for PostgreSQL already provided)
  • Automatically synchronize the schema of your database so that it matches the types you define
  • Utilities to make your app a command line application
  • If you make an app called myapp, you can run it with the command myapp run, not karigo run myapp
  • You also get other tools like myapp sync to update your database's schema
  • Those commands are meant to be run in a directory that contains a file with the necessary configuration
  • Use JWT for sessions (jwt.io)

State

The framework has no release nor documentation yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GateLoggedIn

func GateLoggedIn(ctx *Ctx) bool

GateLoggedIn ...

func GatePublic

func GatePublic(*Ctx) bool

GatePublic ...

func HashPassword

func HashPassword(pw string) string

HashPassword ...

func KernelDeleteRelationships

func KernelDeleteRelationships(ctx *Ctx) error

KernelDeleteRelationships ...

func KernelDeleteResource

func KernelDeleteResource(ctx *Ctx) error

KernelDeleteResource ...

func KernelGetCollection

func KernelGetCollection(ctx *Ctx) error

KernelGetCollection ...

func KernelGetRelationship

func KernelGetRelationship(ctx *Ctx) error

KernelGetRelationship ...

func KernelGetRelationships

func KernelGetRelationships(ctx *Ctx) error

KernelGetRelationships ...

func KernelGetResource

func KernelGetResource(ctx *Ctx) error

KernelGetResource ...

func KernelInsertRelationships

func KernelInsertRelationships(ctx *Ctx) error

KernelInsertRelationships ...

func KernelInsertResource

func KernelInsertResource(ctx *Ctx) error

KernelInsertResource ...

func KernelUpdateRelationship

func KernelUpdateRelationship(ctx *Ctx) error

KernelUpdateRelationship ...

func KernelUpdateRelationships

func KernelUpdateRelationships(ctx *Ctx) error

KernelUpdateRelationships ...

func KernelUpdateResource

func KernelUpdateResource(ctx *Ctx) error

KernelUpdateResource ...

func RandomID

func RandomID(length int) string

RandomID ...

func TableName

func TableName(rel jsonapi.Rel) string

TableName ...

func TerminateCmd

func TerminateCmd(app *App) error

TerminateCmd ...

Types

type App

type App struct {
	sync.Mutex

	Config Config
	Store  Store       `json:"-"`
	CLI    *cli.App    `json:"-"`
	Server http.Server `json:"-"`

	*jsonapi.Registry

	Kernels map[string]Kernel `json:"-"`
	Gates   map[string][]Gate `json:"-"`
}

An App represents the instance of an application that listens and responds to HTTP requests.

func NewApp

func NewApp(store Store) *App

NewApp creates and returns an App object.

func PrepareCmd

func PrepareCmd(c *cli.Context) (*App, error)

PrepareCmd ...

func (*App) Debug

func (a *App) Debug(msg string, args ...interface{})

Debug ...

func (*App) Delete

func (a *App) Delete(route string, k Kernel, g ...Gate)

Delete adds a kernel for handling DELETE requestsn.

func (*App) Get

func (a *App) Get(route string, k Kernel, g ...Gate)

Get adds a kernel for handling GET requests.

func (*App) Info

func (a *App) Info(msg string, args ...interface{})

Info ...

func (*App) Post

func (a *App) Post(route string, k Kernel, g ...Gate)

Post adds a kernel for handling POST requests.

func (*App) Put

func (a *App) Put(route string, k Kernel, g ...Gate)

Put adds a kernel for handling PUT requests.

func (*App) ReadConfig

func (a *App) ReadConfig(data []byte) error

ReadConfig ...

func (*App) Run

func (a *App) Run() error

Run ...

func (*App) RunCLI

func (a *App) RunCLI()

RunCLI ...

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*App) Shutdown

func (a *App) Shutdown() error

Shutdown ...

type Config

type Config struct {
	Name     string
	PrePath  string
	Port     uint16
	Debug    bool
	Info     bool
	Minimize bool
	Store    struct {
		Driver   string
		Host     string
		Database string
		User     string
		Password string
		Options  map[string]string
	}
}

Config ...

type Ctx

type Ctx struct {
	sync.Mutex `json:"-"`

	App *App `json:"-"`
	Tx  Tx

	// Log
	Log []string

	// Request
	W      ResponseWriter `json:"-"`
	Method string
	URL    *jsonapi.URL

	// User
	JWT    *jwt.Token `json:"-"`
	ID     string
	Groups []string

	// Payloads
	In  *jsonapi.Payload
	Out *jsonapi.Document
}

A Ctx is passed around during a request for storing temporary data related to the request.

func NewCtx

func NewCtx(a *App, w ResponseWriter, r *http.Request) *Ctx

NewCtx creates a Ctx and returns it.

func (*Ctx) AddToLog

func (ctx *Ctx) AddToLog(l string)

AddToLog adds a record to the context's log.

func (*Ctx) SaveLog

func (ctx *Ctx) SaveLog()

SaveLog ...

type Gate

type Gate func(*Ctx) bool

A Gate decides whether a request is allowed to continue or not.

type Kernel

type Kernel func(ctx *Ctx) error

Kernel ...

type Key

type Key struct {
	Type  string
	ID    string
	Field string
}

Key ...

func (*Key) String

func (k *Key) String() string

String ... TODO Implement Key.String

type Query

type Query struct {
	Type            string
	ID              string
	Fields          []string
	BelongsToFilter jsonapi.BelongsToFilter
	Filter          *jsonapi.Condition
	Sort            []string
	PageSize        int
	PageNumber      int
}

Query represents a resource or a collection.

To get a resource, only Type and ID are used. To get a collection, all fields except ID are considered.

Filter is not necessary. Fields will default to a slice containing the string "id" if nil or empty.

func NewQuery

func NewQuery(url *jsonapi.URL) *Query

NewQuery creates a new *Query object from a *jsonapi.URL object.

func (*Query) String

func (k *Query) String() string

String ... TODO Implement Query.String

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter

	// Status returns the HTTP status of the response, or 0 if it hasn't been
	// set yet.
	Status() int

	// Written returns wether a status code has been written or not.
	Written() bool

	// Unwrap returns the original proxied target.
	Unwrap() http.ResponseWriter
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra methods about the response.

func WrapResponseWriter

func WrapResponseWriter(w http.ResponseWriter) ResponseWriter

WrapResponseWriter wraps an http.ResponseWriter and returns a ResponseWriter.

type Store

type Store interface {
	// Connection management
	Open(driver, host, db, user, pw string, opts map[string]string) error
	URL() string
	Close()

	// Transaction
	Begin() (Tx, error)

	// Resource manipulation
	SelectCollection(tx Tx, resType string, from jsonapi.BelongsToFilter, params *jsonapi.Params, c jsonapi.Collection) error
	SelectResource(tx Tx, resType, resID string, from jsonapi.BelongsToFilter, params *jsonapi.Params, r jsonapi.Resource) error
	SelectInclusions(tx Tx, originType, originID string, from jsonapi.BelongsToFilter, params *jsonapi.Params) ([]jsonapi.Resource, error)
	InsertResource(tx Tx, r jsonapi.Resource) error
	UpdateResource(tx Tx, resType, resID string, updates map[string]interface{}) error
	DeleteResource(tx Tx, resType, resID string) error
	ResourceExists(tx Tx, resType, resID string) (bool, error)

	// Relationship manipulation
	SelectRelationship(tx Tx, resType, resID, relName string) (string, error)
	SelectRelationships(tx Tx, resType, resID, relName string) ([]string, error)
	UpdateRelationship(tx Tx, resType, resID, relName, relID string) error
	UpdateRelationships(tx Tx, resType, resID, relName string, relIDs []string) error
	InsertRelationships(tx Tx, resType, resID, relName string, relIDs []string) error
	DeleteRelationship(tx Tx, resType, resID, relID, relName string) error
	DeleteRelationships(tx Tx, resType, resID, relName string, relIDs []string) error
	DeleteAllRelationships(tx Tx, resType, resID, relName string) error

	// Other
	CountCollectionSize(tx Tx, resType string, from jsonapi.BelongsToFilter, params *jsonapi.Params) (int, error)

	// Database management
	SetRegistry(reg *jsonapi.Registry)
	SelectResourceTables(tx Tx) ([]string, error)
	SelectRelationshipTables(tx Tx) ([]string, error)
	SelectColumns(tx Tx, resType string) ([]map[string]string, error)
	CreateResourceTable(tx Tx, typ jsonapi.Type) error
	CreateRelationshipTable(tx Tx, rel jsonapi.Rel) error
	AddColumn(tx Tx, resType string, attr jsonapi.Attr) error
	DropTable(tx Tx, resType string) error
	DropColumn(tx Tx, resType, colName string) error
	DrainDatabase(tx Tx) error
	SyncDatabase(tx Tx, reg *jsonapi.Registry, verbose, apply bool) error
}

Store ...

type Tx

type Tx interface {
	Commit() error
	Rollback() error
}

Tx ...

Jump to

Keyboard shortcuts

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