forest

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

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

Go to latest
Published: May 29, 2016 License: MIT Imports: 7 Imported by: 1

README

forest

Coverage Status

API documentation

forest is a micro-framework for building REST services that talk JSON. Its core unit is a forest.App that is built upon a bear multiplexer for URL routing. It outputs responses using forest.Response and provides utility methods for many common tasks required by web services.

Install

go get github.com/ursiform/forest

Test

go test -cover github.com/ursiform/forest

API

API documentation

License

MIT License

Documentation

Overview

Package forest provides a minimalist framework for writing REST services that speak JSON.

Index

Constants

View Source
const (
	Body                  = "forestbody"
	DurationCookie        = 26 * time.Hour
	DurationSession       = 25 * time.Hour
	ErrorBadCredentials   = "bad credentials"
	ErrorCSRF             = SessionID + " required"
	ErrorGeneric          = "something went wrong"
	ErrorMethodNotAllowed = "method not allowed"
	ErrorNotFound         = "not found"
	ErrorParse            = "json parse error"
	ErrorUnauthorized     = "unauthorized access"
	Failure               = false
	Success               = true
	NoMessage             = ""
	SessionID             = "sessionid"
	SessionRefresh        = "sessionrefresh"
	SessionUser           = "sessionuser"
	SessionUserID         = "sessionuserid"
	Error                 = "foresterror"
	SafeError             = "forestsafeerror"
	UnknownIP             = "unknown-ip"
	UnknownAgent          = "unknown user agent"
	UnknownSession        = "unknown-session"
	WareInstalled         = "forest middleware"
)
View Source
const ConfigFile = "forest.json"

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	*bear.Mux
	Config *Config

	Log *logger.Logger

	SafeErrorFilter func(error) error
	// contains filtered or unexported fields
}

func New

func New(configFile string) *App

func (*App) Duration

func (app *App) Duration(key string) time.Duration

Duration gets the duration for a specific key, e.g. "Cookie" expiration.

func (*App) Error

func (app *App) Error(key string) string

Error gets the error for a specific key, e.g. "Unauthorized".

func (*App) InstallWare

func (app *App) InstallWare(
	key string, handler func(ctx *bear.Context), message string) error

func (*App) ListenAndServe

func (app *App) ListenAndServe() error

func (*App) ListenAndServeTLS

func (app *App) ListenAndServeTLS(certFile, keyFile string) error

func (*App) Message

func (app *App) Message(key string) string

Message gets the app message for a specific key, e.g. "AlreadyLoggedIn".

func (*App) On

func (app *App) On(verb string, pattern string, handlers ...interface{}) error

func (*App) RegisterRoute

func (app *App) RegisterRoute(path string, sub SubRouter)

func (*App) Response

func (app *App) Response(ctx *bear.Context,
	code int, success bool, message string) *Response

func (*App) SetCookie

func (app *App) SetCookie(
	ctx *bear.Context, path, key, value string, duration time.Duration)

func (*App) SetDuration

func (app *App) SetDuration(key string, value time.Duration)

SetDuration sets the duration for a specific key, e.g. "Cookie" expiration.

func (*App) SetError

func (app *App) SetError(key string, value string)

SetError sets the error for a specific key, e.g. "Unauthorized".

func (*App) SetMessage

func (app *App) SetMessage(key string, value string)

SetMessage sets the app message for a specific key, e.g. "AlreadyLoggedIn".

func (*App) Ware

func (app *App) Ware(key string) func(ctx *bear.Context)

type Config

type Config struct {
	Address      string `json:"address,omitempty"`
	CookiePath   string
	File         string
	LogLevel     int
	LogLevelName string `json:"loglevel,omitempty"`
	LogRequests  bool   `json:"logrequests,omitempty"`
	Name         string `json:"name,omitempty"`
	PoweredBy    string
	Debug        bool   `json:"debug,omitempty"`
	Version      string `json:"version,omitempty"`
}

type Response

type Response struct {
	Data    interface{} `json:"data,omitempty"`
	Success bool        `json:"success"`
	Message string      `json:"message,omitempty"`
	// contains filtered or unexported fields
}

func (*Response) SetCookie

func (res *Response) SetCookie(
	path, key, value string, duration time.Duration)

func (*Response) Write

func (res *Response) Write(data interface{}) (bytes int, err error)

type SubRouter

type SubRouter interface {
	Route(path string)
}

SubRouter is the basic building block of forest applications. In the main.go file where a service is initialized, app.RegisterRoute is called with a path string and a SubRouter instance in order to logically group all of the endpoints a particular service will answer. Since each App inherits from bear.Mux , it is not strictly necessary to use SubRouter at all, it simply exists to provide a convenient way to instantiate many endpoints in one place.

Jump to

Keyboard shortcuts

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