lite

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2020 License: MIT Imports: 13 Imported by: 0

README

lite

GoDoc License Build Status Report Card GoCover

Simple tool for building RESTful APIs

Installation
go get -u github.com/tiny-go/lite
Modules

You can use BaseModule that provides basic module functionality (such as register/unregister/list) or write your own implementation. BaseModule already contains Register, Unregister and Controllers methods and implements Module interface.

Controllers

Any golang func, struct or custom type can be used as a controller provided that it implements Controller interface and has some action methods, such as Get/GetAll/Post/PostAll/... (check the entire list in interfaces.go).

Dependencies

If you need to pass some dependencies (like config, database connection etc) to your module/controller use handler.Map(dep), it will be passed to the module/controller (use struct tag inject:"true" in front of the struct fields that should be injected). Take a look at example folder for more information (for instance example/auth/user/controller.go).

Usage
package main

import (
	"log"
	"net/http"

	"github.com/tiny-go/lite"
	// register codecs
	"github.com/tiny-go/codec/driver"
	_ "github.com/tiny-go/codec/driver/json"
	_ "github.com/tiny-go/codec/driver/xml"
)

func main() {
	// set default codec
	driver.Default("application/json")
	// create new handler
	handler := lite.NewHandler()
	// map dependencies
	handler.Map(depA)
	handler.Map(depB)
	handler.Map(depC)
	// register modules
	handler.Use(aliasOne, moduleA)
	handler.Use(aliasTwo, moduleB)
	// start HTTP server
	log.Fatal(http.ListenAndServe(":8080", handler))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GorillaParams

func GorillaParams(next http.Handler) http.Handler

GorillaParams extracts URI params from the request (when using gorolla/mux).

func Modules

func Modules(f func(alias string, module Module) bool)

Modules iterates all registered modules applying provided func.

func Register

func Register(alias string, module Module)

Register makes module available with provided alias.

Types

type BaseModule

type BaseModule struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

BaseModule contains a basic set of logic and provides basic operations on resources (like "Register", "Unregister" etc).

func NewBaseModule

func NewBaseModule() *BaseModule

NewBaseModule is a constructor func for BaseModule.

func (*BaseModule) Controllers

func (m *BaseModule) Controllers(f func(string, Controller) bool)

Controllers calls the provided func sequentially for each available resource. If func returns false the "for" loop will be stopped.

func (*BaseModule) Register

func (m *BaseModule) Register(name string, resource Controller) error

Register makes resource available by provided alias.

func (*BaseModule) Unregister

func (m *BaseModule) Unregister(name string) error

Unregister removes resource from the list by alias.

type Controller

type Controller interface {
	mw.Controller
	Init() error
}

Controller interface is a bare minimal controller.

type Handler

type Handler interface {
	http.Handler
	Use(string, Module) error
	Map(interface{}) inject.TypeMapper
}

Handler interface describes HTTP API handler.

func NewHandler

func NewHandler() Handler

NewHandler creates new HTTP handler.

type Methods

type Methods []string

Methods is a collection of HTTP methods.

func (*Methods) Add

func (ms *Methods) Add(method string)

Add method to the list of supported actions (ignoring duplicates).

func (*Methods) Empty

func (ms *Methods) Empty() bool

Empty returns true if list of methods is empty.

func (*Methods) Join

func (ms *Methods) Join() string

Join converts list of methods/actions to a string (separated by comma).

type Module

type Module interface {
	// Register should add Controller to module resources.
	Register(alias string, controller Controller) error
	// Unregister should remove the Controller from module resources.
	Unregister(alias string) error
	// Controllers should call the provided func sequentially for each available
	// resource. If func returns false the "for" loop should be interrupted.
	Controllers(func(alias string, controller Controller) bool)
}

Module represents single module with lite API (it means that all its routes can be generated only once at startup).

type Params

type Params map[string]string

Params is a key/value map containing the request parameters from the URI.

func ParamsFromContext

func ParamsFromContext(ctx context.Context) Params

ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.

type PluralDeleter

type PluralDeleter interface {
	Controller
	DeleteAll(ctx context.Context, params url.Values) (interface{}, error)
}

PluralDeleter should be able to delete a list of models.

type PluralGetter

type PluralGetter interface {
	Controller
	GetAll(ctx context.Context, params url.Values) (interface{}, error)
}

PluralGetter should be able to provide a list of available models.

type PluralPatcher

type PluralPatcher interface {
	Controller
	PatchAll(ctx context.Context, params url.Values, f func(v interface{}) error) (interface{}, error)
}

PluralPatcher should be able to patch a list of models.

type PluralPoster

type PluralPoster interface {
	Controller
	PostAll(ctx context.Context, f func(v interface{}) error) (interface{}, error)
}

PluralPoster should be able to store a list of model to the storage.

type PluralPutter

type PluralPutter interface {
	Controller
	PutAll(ctx context.Context, params url.Values, f func(v interface{}) error) (interface{}, error)
}

PluralPutter should be able to update a list of models.

type SingleDeleter

type SingleDeleter interface {
	Controller
	Delete(ctx context.Context, pk string) (interface{}, error)
}

SingleDeleter should be able to delete a single model by primary key(s).

type SingleGetter

type SingleGetter interface {
	Controller
	Get(ctx context.Context, pk string) (interface{}, error)
}

SingleGetter should be able to provide a single model by primary key(s)

type SinglePatcher

type SinglePatcher interface {
	Controller
	Patch(ctx context.Context, pk string, f func(v interface{}) error) (interface{}, error)
}

SinglePatcher should be able to patch a single model by primary key(s).

type SinglePoster

type SinglePoster interface {
	Controller
	Post(ctx context.Context, f func(v interface{}) error) (interface{}, error)
}

SinglePoster should be able to store a single model to the storage.

type SinglePutter

type SinglePutter interface {
	Controller
	Put(ctx context.Context, pk string, f func(v interface{}) error) (interface{}, error)
}

SinglePutter should be able to update a single model by primary key(s).

Directories

Path Synopsis
examples
os
ums

Jump to

Keyboard shortcuts

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