happyngine

package module
v0.0.0-...-9be4a76 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2016 License: GPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Hostname = ""

Functions

func NewCookie

func NewCookie(name, value string, options *SessionOptions) *http.Cookie

NewCookie returns an http.Cookie with the options set. It also sets the Expires field calculated based on the MaxAge value, for Internet Explorer compatibility.

Types

type API

type API struct {
	Middlewares     []MiddlewareHandler
	Resources       map[string]interface{}
	Error404Handler ErrorHandler
	Router          Router
	Headers         map[string]string
}

func NewAPI

func NewAPI() *API

func (*API) AddMiddleware

func (this *API) AddMiddleware(middlewareHandler MiddlewareHandler)

func (*API) AddResource

func (this *API) AddResource(name string, resource interface{})

func (*API) AddRoute

func (this *API) AddRoute(method string, path string, actionHandler ActionHandler, middlewares ...MiddlewareHandler)

func (*API) GetResource

func (this *API) GetResource(name string) interface{}

func (*API) Run

func (this *API) Run(host string) error

func (*API) ServeHTTP

func (this *API) ServeHTTP(resp http.ResponseWriter, req *http.Request)

type Action

type Action struct {
	Context    *Context
	Form       *Form
	Parameters []*Parameter
}

func (*Action) AddError

func (this *Action) AddError(code int, text string)

func (*Action) AddParameter

func (this *Action) AddParameter(name string, required bool, validators ...*validator.Validator)

func (*Action) GetErrors

func (this *Action) GetErrors() ([]string, int)

func (*Action) GetInt64Param

func (this *Action) GetInt64Param(key string) int64

func (*Action) GetIntParam

func (this *Action) GetIntParam(key string) int

func (*Action) GetParam

func (this *Action) GetParam(key string) string

func (*Action) GetURLInt64Param

func (this *Action) GetURLInt64Param(key string) int64

func (*Action) GetURLIntParam

func (this *Action) GetURLIntParam(key string) int

func (*Action) GetURLParam

func (this *Action) GetURLParam(key string) string

func (*Action) HasErrors

func (this *Action) HasErrors() bool

func (*Action) IsValid

func (this *Action) IsValid() bool

func (*Action) JSON

func (this *Action) JSON(code int, obj interface{}, headers ...string)

func (*Action) Send

func (this *Action) Send(code int, text string, headers ...string)

func (*Action) SendByte

func (this *Action) SendByte(code int, data []byte, headers ...string)

type ActionHandler

type ActionHandler func(*Context) ActionInterface

type ActionInterface

type ActionInterface interface {
	Run()
	IsValid() bool
	Send(int, string, ...string)
}

type Context

type Context struct {
	Request            *http.Request       `json:"-"`
	Response           http.ResponseWriter `json:"-"`
	API                *API                `json:"-"`
	Session            *Session            `json:"-"`
	ResponseStatusCode int                 `json:"-"` // Because we can't retrieve the status from http.ResponseWriter
	ResponseLength     int                 `json:"-"`
	Errors             map[string]string   `json:"-"`
	ErrorCode          int                 `json:"-"`
	// contains filtered or unexported fields
}

func NewContext

func NewContext(req *http.Request, resp http.ResponseWriter, api *API) *Context

func (*Context) AddError

func (c *Context) AddError(code int, text string)

func (*Context) Criticalln

func (c *Context) Criticalln(args ...interface{})

func (*Context) Debugln

func (c *Context) Debugln(args ...interface{})

func (*Context) Errorln

func (c *Context) Errorln(args ...interface{})

func (*Context) FetchSession

func (c *Context) FetchSession(name string) *Session

Session may be nil

func (*Context) GetErrors

func (c *Context) GetErrors() ([]string, int)

func (*Context) GetInt64Param

func (c *Context) GetInt64Param(key string) int64

func (*Context) GetIntParam

func (c *Context) GetIntParam(key string) int

func (*Context) GetParam

func (c *Context) GetParam(key string) string

func (*Context) GetURLInt64Param

func (c *Context) GetURLInt64Param(key string) int64

func (*Context) GetURLIntParam

func (c *Context) GetURLIntParam(key string) int

func (*Context) GetURLParam

func (c *Context) GetURLParam(key string) string

func (*Context) HasErrors

func (c *Context) HasErrors() bool

func (*Context) JSON

func (c *Context) JSON(code int, obj interface{}, headers ...string)

func (*Context) NewSession

func (c *Context) NewSession(name string) *Session

func (*Context) Next

func (c *Context) Next()

func (*Context) RemoteIP

func (c *Context) RemoteIP() string

func (*Context) Send

func (c *Context) Send(code int, text string, headers ...string)

func (*Context) SendByte

func (c *Context) SendByte(code int, data []byte, headers ...string)

func (*Context) Warningln

func (c *Context) Warningln(args ...interface{})

type CookieStore

type CookieStore struct {
	Codecs  []securecookie.Codec
	Options *SessionOptions // default configuration
}

CookieStore stores sessions using secure cookies.

func NewCookieStore

func NewCookieStore(keyPairs ...[]byte) *CookieStore

NewCookieStore returns a new CookieStore.

Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.

The first key in a pair is used for authentication and the second for encryption. The encryption key can be set to nil or omitted in the last pair, but the authentication key is required in all pairs.

It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.

Use the convenience function securecookie.GenerateRandomKey() to create strong keys.

func (*CookieStore) Get

func (s *CookieStore) Get(r *http.Request, name string) (*Session, error)

Get returns a session for the given name after adding it to the registry.

It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.

It returns a new session and an error if the session exists but could not be decoded.

func (*CookieStore) MaxAge

func (s *CookieStore) MaxAge(age int)

MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.

func (*CookieStore) New

func (s *CookieStore) New(r *http.Request, name string) (*Session, error)

New returns a session for the given name without adding it to the registry.

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call.

func (*CookieStore) Save

func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter,
	session *Session) error

Save adds a single session to the response.

type ErrorHandler

type ErrorHandler func(*Context, interface{})

type Form

type Form struct {
	Context  *Context
	Elements map[string]FormElementInterface
}

func NewForm

func NewForm(c *Context, elems ...FormElementInterface) *Form

func (*Form) AddElement

func (f *Form) AddElement(e FormElementInterface) *Form

func (*Form) Elem

func (f *Form) Elem(name string) FormElementInterface

func (*Form) IsValid

func (f *Form) IsValid() bool

type FormElement

type FormElement struct {
	// contains filtered or unexported fields
}

func NewFormElement

func NewFormElement(name, errStr string) *FormElement

func (*FormElement) AddValidator

func (e *FormElement) AddValidator(h FormValidatorHandler) *FormElement

func (*FormElement) Error

func (e *FormElement) Error() string

func (*FormElement) FormValue

func (e *FormElement) FormValue() string

func (*FormElement) Found

func (e *FormElement) Found() bool

func (*FormElement) Name

func (e *FormElement) Name() string

func (*FormElement) Required

func (e *FormElement) Required() bool

func (*FormElement) SetFormValue

func (e *FormElement) SetFormValue(v string)

func (*FormElement) SetRequired

func (e *FormElement) SetRequired(r bool) *FormElement

func (*FormElement) SetValue

func (e *FormElement) SetValue(i interface{})

func (*FormElement) Validate

func (e *FormElement) Validate(c *Context)

func (*FormElement) Value

func (e *FormElement) Value() interface{}

type FormElementInterface

type FormElementInterface interface {
	Name() string
	Validate(*Context)
	SetFormValue(string)
	FormValue() string
	SetValue(interface{})
	Value() interface{}
	Error() string
	Required() bool
	Found() bool
}

type FormValidatorHandler

type FormValidatorHandler func(*Context, FormElementInterface)

func IsDate

func IsDate() FormValidatorHandler

func IsEmail

func IsEmail() FormValidatorHandler

func IsEqual

func IsEqual(refs ...string) FormValidatorHandler

func IsFloat

func IsFloat() FormValidatorHandler

func IsInteger

func IsInteger() FormValidatorHandler

func IsUInteger

func IsUInteger() FormValidatorHandler

func IsUUID

func IsUUID() FormValidatorHandler

func RegexpFormValidator

func RegexpFormValidator(pattern string) FormValidatorHandler

type M

type M map[string]interface{}

type MiddlewareHandler

type MiddlewareHandler func(*Context)

type MultiError

type MultiError []error

MultiError stores multiple errors.

Borrowed from the App Engine SDK.

func (MultiError) Error

func (m MultiError) Error() string

type Parameter

type Parameter struct {
	Name       string
	Validators []*validator.Validator
	Required   bool
}

func NewParameter

func NewParameter(name string, required bool, validators ...*validator.Validator) *Parameter

func (*Parameter) IsValid

func (this *Parameter) IsValid(data string) error

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry stores sessions used during a request.

func GetRegistry

func GetRegistry(r *http.Request) *Registry

GetRegistry returns a registry instance for the current request.

func (*Registry) Get

func (s *Registry) Get(store Store, name string) (session *Session, err error)

Get registers and returns a session for the given name and session store.

It returns a new session if there are no sessions registered for the name.

func (*Registry) Save

func (s *Registry) Save(w http.ResponseWriter) error

Save saves all sessions registered for the current request.

type Route

type Route struct {
	Method        string
	Path          *regexp.Regexp
	ActionHandler ActionHandler
	Middlewares   []MiddlewareHandler
}

func NewRoute

func NewRoute(method string, path string, actionHandler ActionHandler, middlewares ...MiddlewareHandler) *Route

type Router

type Router struct {
	Routes []*Route
}

func (*Router) AddRoute

func (this *Router) AddRoute(route *Route)

func (*Router) FindRoute

func (this *Router) FindRoute(req *http.Request) (*Route, error)

type Session

type Session struct {
	ID      string
	Values  map[string]interface{}
	Options *SessionOptions
	IsNew   bool
	// contains filtered or unexported fields
}

func GetSession

func GetSession(r *http.Request, name string) *Session

func NewSession

func NewSession(name string, options *SessionOptions) *Session

func (*Session) Changed

func (s *Session) Changed() bool

func (*Session) Del

func (s *Session) Del(name string)

func (*Session) Destroy

func (s *Session) Destroy()

func (*Session) Get

func (s *Session) Get(name string) interface{}

func (*Session) Name

func (s *Session) Name() string

Name returns the name used to register the session.

func (*Session) Save

func (s *Session) Save(r *http.Request, w http.ResponseWriter) error

Save is a convenience method to save this session. It is the same as calling store.Save(request, response, session). You should call Save before writing to the response or returning from the handler.

func (*Session) Set

func (s *Session) Set(name string, value interface{})

func (*Session) SetMap

func (s *Session) SetMap(m map[string]interface{})

type SessionOptions

type SessionOptions struct {
	Path   string
	Domain string
	// MaxAge=0 means no 'Max-Age' attribute specified.
	// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
	// MaxAge>0 means Max-Age attribute present and given in seconds.
	MaxAge   int
	Secure   bool
	HttpOnly bool
}

Options stores configuration for a session or session store.

Fields are a subset of http.Cookie fields.

type Store

type Store interface {
	// Get should return a cached session.
	Get(r *http.Request, name string) (*Session, error)

	// New should create and return a new session.
	//
	// Note that New should never return a nil session, even in the case of
	// an error if using the Registry infrastructure to cache the session.
	New(r *http.Request, name string) (*Session, error)

	// Save should persist session to the underlying store implementation.
	Save(r *http.Request, w http.ResponseWriter, s *Session) error
}

Store is an interface for custom session stores.

See CookieStore and FilesystemStore for examples.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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