Documentation ¶
Index ¶
- func Errorf(format string, parameters ...interface{}) error
- type Application
- func (app *Application) Delete(pattern string, handler HandlerFunc)
- func (app *Application) Error(statusCode int, handler ErrorHandlerFunc)
- func (app *Application) Get(pattern string, handler HandlerFunc)
- func (app *Application) Head(pattern string, handler HandlerFunc)
- func (app *Application) Options(pattern string, handler HandlerFunc)
- func (app *Application) Patch(pattern string, handler HandlerFunc)
- func (app *Application) Post(pattern string, handler HandlerFunc)
- func (app *Application) Put(pattern string, handler HandlerFunc)
- func (app *Application) Run(addr string)
- func (app *Application) RunTLS(addr, certFile, keyFile string)
- func (app *Application) ServeHTTP(res http.ResponseWriter, req *http.Request)
- func (app *Application) Static(url string, path string)
- func (app *Application) Use(m ...MiddlewareHandlerFunc)
- type Chain
- type Config
- func (config *Config) Get(key string, defaultValue interface{}) (interface{}, error)
- func (config *Config) GetBool(key string, defaultValue bool) (bool, error)
- func (config *Config) GetFloat(key string, defaultValue float64) (float64, error)
- func (config *Config) GetInt(key string, defaultValue int) (int, error)
- func (config *Config) GetString(key string, defaultValue string) (string, error)
- func (config *Config) Set(key string, value interface{}) error
- type Context
- func (ctx *Context) Abort(statusCode int, data ...map[string]interface{})
- func (ctx *Context) AddHeader(key, value string)
- func (ctx *Context) ClientIP() string
- func (ctx *Context) Cookie(key string) (string, error)
- func (ctx *Context) Header(key string) string
- func (ctx *Context) JSON(obj interface{})
- func (ctx *Context) JSONIndent(obj interface{}, prefix, indent string)
- func (ctx *Context) Loader(name string) *Context
- func (ctx *Context) Param(key string) string
- func (ctx *Context) Query(key string, index ...int) (string, error)
- func (ctx *Context) Redirect(url string)
- func (ctx *Context) Redirect301(url string)
- func (ctx *Context) Render(file string, data ...map[string]interface{})
- func (ctx *Context) RenderFromString(tplSrc string, data ...map[string]interface{})
- func (ctx *Context) Send(body interface{})
- func (ctx *Context) SendStatus(statusCode int)
- func (ctx *Context) SetCookie(key string, value string, expire int)
- func (ctx *Context) SetHeader(key, value string)
- func (ctx *Context) StatusCode() int
- type Error
- type ErrorHandlerFunc
- type FileSystemLoader
- type Frame
- type HandlerFunc
- type KeyError
- type MapLoader
- type MemorySession
- type MemorySessionManager
- type MiddlewareHandlerFunc
- type Parameter
- type Session
- type SessionManager
- type Template
- type TemplateLoader
- type TemplateManager
- type ValueTypeError
- type View
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Application ¶
type Application struct { // The View model of the application. View handles the templating and page // rendering. View *View // Config provides configuration management. Config *Config SessionManager SessionManager // NotFoundHandler handles requests when no route is matched. NotFoundHandler HandlerFunc // The default error handler, if the corresponding error code is not specified // in the `errorHandler` map, this handler will be called. DefaultErrorHandler ErrorHandlerFunc // contains filtered or unexported fields }
Application is an abstraction of a Golf application, can be used for configuration, etc.
func (*Application) Delete ¶
func (app *Application) Delete(pattern string, handler HandlerFunc)
Delete method is used for registering a Delete method route
func (*Application) Error ¶
func (app *Application) Error(statusCode int, handler ErrorHandlerFunc)
Error method is used for registering an handler for a specified HTTP error code.
func (*Application) Get ¶
func (app *Application) Get(pattern string, handler HandlerFunc)
Get method is used for registering a Get method route
func (*Application) Head ¶ added in v0.2.0
func (app *Application) Head(pattern string, handler HandlerFunc)
Head method is used for registering a Head method route
func (*Application) Options ¶ added in v0.2.0
func (app *Application) Options(pattern string, handler HandlerFunc)
Options method is used for registering a Options method route
func (*Application) Patch ¶ added in v0.2.0
func (app *Application) Patch(pattern string, handler HandlerFunc)
Patch method is used for registering a Patch method route
func (*Application) Post ¶
func (app *Application) Post(pattern string, handler HandlerFunc)
Post method is used for registering a Post method route
func (*Application) Put ¶
func (app *Application) Put(pattern string, handler HandlerFunc)
Put method is used for registering a Put method route
func (*Application) RunTLS ¶
func (app *Application) RunTLS(addr, certFile, keyFile string)
RunTLS runs the app with TLS support.
func (*Application) ServeHTTP ¶
func (app *Application) ServeHTTP(res http.ResponseWriter, req *http.Request)
Basic entrance of an `http.ResponseWriter` and an `http.Request`.
func (*Application) Static ¶
func (app *Application) Static(url string, path string)
Static is used for registering a static folder
func (*Application) Use ¶ added in v0.3.0
func (app *Application) Use(m ...MiddlewareHandlerFunc)
Use appends a middleware to the existing middleware chain.
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain contains a sequence of middlewares.
func NewChain ¶
func NewChain(handlerArray ...MiddlewareHandlerFunc) *Chain
NewChain Creates a new middleware chain.
func (*Chain) Append ¶
func (c *Chain) Append(fn MiddlewareHandlerFunc)
Append a middleware to the middleware chain
func (Chain) Final ¶
func (c Chain) Final(fn HandlerFunc) HandlerFunc
Final indicates a final Handler, chain the multiple middlewares together with the handler, and return them together as a handler.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config control for the application.
func ConfigFromJSON ¶
ConfigFromJSON creates a Config instance from a JSON io.reader.
func (*Config) Get ¶
Get is used to retrieve the value by indicating a key. After calling this method you should indicate the type of the return value. If one of the parent node is not a map, then return a ValueTypeError. If the key is not found, return a KeyError.
func (*Config) GetBool ¶
GetBool fetches the bool value by indicating the key. It returns a ValueTypeError if the value is not a sring.
func (*Config) GetFloat ¶
GetFloat fetches the float value by indicating the key. It returns a ValueTypeError if the value is not a sring.
func (*Config) GetInt ¶
GetInt fetches the int value by indicating the key. It returns a ValueTypeError if the value is not a sring.
func (*Config) GetString ¶
GetString fetches the string value by indicating the key. It returns a ValueTypeError if the value is not a sring.
func (*Config) Set ¶
Set is used to set the value by indicating the key. If you want to set multi-level json, key can be like 'foo/bar'. For instance, `Set("foo/bar", 4)` and `Set("foo/bar2", "foo")`. If the parent key is not a map, then return a KeyError. For instance, can not set ("foo/bar", 4) after setting ("foo", 5).
type Context ¶
type Context struct { // http.Request Request *http.Request // http.ResponseWriter Response http.ResponseWriter // URL Parameter Params Parameter // The application App *Application // Session instance for the current context. Session Session // Indicating if the response is already sent. IsSent bool // contains filtered or unexported fields }
Context is a wrapper of http.Request and http.ResponseWriter.
func NewContext ¶
func NewContext(req *http.Request, res http.ResponseWriter, app *Application) *Context
NewContext creates a Golf.Context instance.
func (*Context) Abort ¶
Abort method returns an HTTP Error by indicating the status code, the corresponding handler inside `App.errorHandler` will be called, if user has not set the corresponding error handler, the defaultErrorHandler will be called.
func (*Context) AddHeader ¶ added in v0.3.0
AddHeader adds the key, value pair to the header. It appends to any existing values associated with key.
func (*Context) ClientIP ¶ added in v0.3.0
ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. This method is taken from https://github.com/gin-gonic/gin
func (*Context) Header ¶
Header gets the first value associated with the given key. If there are no values associated with the key, Get returns "".
func (*Context) JSONIndent ¶ added in v0.3.0
JSONIndent Sends a JSON response, indenting the JSON as desired.
func (*Context) Loader ¶
Loader method sets the template loader for this context. This should be done before calling `ctx.Render`.
func (*Context) Param ¶
Param method retrieves the parameters from url If the url is /:id/, then id can be retrieved by calling `ctx.Param(id)`
func (*Context) Redirect301 ¶ added in v0.3.0
Redirect301 method sets the response as a 301 redirection.
func (*Context) RenderFromString ¶
RenderFromString renders a input string.
func (*Context) Send ¶
func (ctx *Context) Send(body interface{})
Send the response immediately. Set `ctx.IsSent` to `true` to make sure that the response won't be sent twice.
func (*Context) SendStatus ¶ added in v0.3.0
SendStatus takes an integer and sets the response status to the integer given.
func (*Context) SetCookie ¶
SetCookie set cookies for the request. If expire is 0, create a session cookie.
func (*Context) SetHeader ¶ added in v0.3.0
SetHeader sets the header entries associated with key to the single element value. It replaces any existing values associated with key.
func (*Context) StatusCode ¶
StatusCode returns the status code that golf has sent.
type Error ¶ added in v0.1.1
type Error struct { Message string Class string Stack []*Frame // contains filtered or unexported fields }
Error provides more structured information about a Go error.
func NewError ¶ added in v0.1.1
func NewError(msg interface{}) Error
NewError creates a new error instance
func (Error) StackTraceString ¶ added in v0.1.1
StackTraceString returns the stack trace in a string format.
type ErrorHandlerFunc ¶ added in v0.2.0
ErrorHandlerFunc is the type of the function that handles error in Golf.
type FileSystemLoader ¶
type FileSystemLoader struct {
BaseDir string
}
FileSystemLoader is an implementation of TemplateLoader that loads templates from file system.
func (*FileSystemLoader) LoadTemplate ¶
func (loader *FileSystemLoader) LoadTemplate(name string) (string, error)
LoadTemplate loads a template from a file.
type Frame ¶ added in v0.1.1
type Frame struct { Number string `json:"number"` File string `json:"file"` Method string `json:"method"` }
Frame represent a stack frame inside of a Honeybadger backtrace.
type HandlerFunc ¶ added in v0.2.0
type HandlerFunc func(ctx *Context)
HandlerFunc is the type of the handler function that Golf accepts.
func RecoverMiddleware ¶
func RecoverMiddleware(next HandlerFunc) HandlerFunc
RecoverMiddleware is the built-in middleware for recovering from errors.
func SessionMiddleware ¶ added in v0.1.1
func SessionMiddleware(next HandlerFunc) HandlerFunc
SessionMiddleware handles session of the request
func XSRFProtectionMiddleware ¶ added in v0.1.1
func XSRFProtectionMiddleware(next HandlerFunc) HandlerFunc
XSRFProtectionMiddleware is the built-in middleware for XSRF protection.
type KeyError ¶
type KeyError struct {
// contains filtered or unexported fields
}
KeyError is thrown when the specified key is not found in the configuration.
type MapLoader ¶
MapLoader is a implementation of TemplateLoader that loads templates from a map data structure.
type MemorySession ¶
type MemorySession struct {
// contains filtered or unexported fields
}
MemorySession is an memory based implementaion of Session.
func (*MemorySession) Delete ¶
func (s *MemorySession) Delete(key string) error
Delete method deletes the value by given a key in the session.
func (*MemorySession) Get ¶
func (s *MemorySession) Get(key string) (interface{}, error)
Get method gets the value by given a key in the session.
func (*MemorySession) SessionID ¶
func (s *MemorySession) SessionID() string
SessionID returns the current ID of the session.
func (*MemorySession) Set ¶
func (s *MemorySession) Set(key string, value interface{}) error
Set method sets the key value pair in the session.
type MemorySessionManager ¶
type MemorySessionManager struct {
// contains filtered or unexported fields
}
MemorySessionManager is a implementation of Session Manager, which stores data in memory.
func NewMemorySessionManager ¶
func NewMemorySessionManager() *MemorySessionManager
NewMemorySessionManager creates a new session manager.
func (*MemorySessionManager) Count ¶ added in v0.1.1
func (mgr *MemorySessionManager) Count() int
Count returns the number of the current session stored in the session manager.
func (*MemorySessionManager) GarbageCollection ¶ added in v0.1.1
func (mgr *MemorySessionManager) GarbageCollection()
GarbageCollection recycles expired sessions, delete them from the session manager.
func (*MemorySessionManager) NewSession ¶
func (mgr *MemorySessionManager) NewSession() (Session, error)
NewSession creates and returns a new session.
type MiddlewareHandlerFunc ¶ added in v0.3.0
type MiddlewareHandlerFunc func(next HandlerFunc) HandlerFunc
MiddlewareHandlerFunc defines the middleware function type that Golf uses.
func LoggingMiddleware ¶
func LoggingMiddleware(out io.Writer) MiddlewareHandlerFunc
LoggingMiddleware is the built-in middleware for logging. This method is referred from https://github.com/gin-gonic/gin/blob/develop/logger.go#L46
type Parameter ¶ added in v0.2.0
type Parameter struct {
// contains filtered or unexported fields
}
Parameter holds the parameters matched in the route
type Session ¶
type Session interface { Set(key string, value interface{}) error Get(key string) (interface{}, error) Delete(key string) error SessionID() string // contains filtered or unexported methods }
Session is an interface for session instance, a session instance contains data needed.
type SessionManager ¶ added in v0.1.1
type SessionManager interface { NewSession() (Session, error) Session(string) (Session, error) GarbageCollection() Count() int // contains filtered or unexported methods }
SessionManager manages a map of sessions.
type TemplateLoader ¶
TemplateLoader is the loader interface for templates.
type TemplateManager ¶
type TemplateManager struct { Loader TemplateLoader FuncMap map[string]interface{} //template.FuncMap }
TemplateManager handles the template loader and stores the function map.
func (*TemplateManager) Render ¶
func (t *TemplateManager) Render(w io.Writer, name string, data interface{}) error
Render renders a template and writes it to the io.Writer interface.
func (*TemplateManager) RenderFromString ¶
func (t *TemplateManager) RenderFromString(w io.Writer, tplSrc string, data interface{}) error
RenderFromString renders a template from string.
type ValueTypeError ¶
type ValueTypeError struct {
// contains filtered or unexported fields
}
ValueTypeError is thrown when the type of the specified value is not valid.
func (*ValueTypeError) Error ¶
func (err *ValueTypeError) Error() string
Error method implements Error method of Go standard library "error".
type View ¶
View handles templates rendering
func (*View) Render ¶
func (view *View) Render(loaderName string, filePath string, data map[string]interface{}) (string, error)
Render renders a template by indicating the file path and the name of the template loader.
func (*View) RenderFromString ¶
func (view *View) RenderFromString(loaderName string, tplSrc string, data map[string]interface{}) (string, error)
RenderFromString does the same thing as render but renders a template by indicating the template source from tplSrc.
func (*View) SetTemplateLoader ¶
SetTemplateLoader sets the template loader by indicating the name and the path.