web: github.com/hoisie/web Index | Files | Directories

package web

import "github.com/hoisie/web"

Package web is a lightweight web framework for Go. It's ideal for writing simple, performant backend web services.

Index

Package Files

fcgi.go helpers.go scgi.go secure_cookie.go server.go ttycolors.go web.go

Variables

var (
    ErrMissingCookieSecret = errors.New("Secret Key for secure cookies has not been set. Assign one to web.Config.CookieSecret.")
    ErrInvalidKey          = errors.New("The keys for secure cookies have not been initialized. Ensure that a Run* method is being called")
)
var Config = &ServerConfig{
    RecoverPanic: true,
    ColorOutput:  true,
}

Config is the configuration of the main server.

func Close Uses

func Close()

Close stops the main server.

func Delete Uses

func Delete(route string, handler interface{})

Delete adds a handler for the 'DELETE' http method in the main server.

func Get Uses

func Get(route string, handler interface{})

Get adds a handler for the 'GET' http method in the main server.

func Handle Uses

func Handle(route string, method string, httpHandler http.Handler)

Add a custom http.Handler. Will have no effect when running as FCGI or SCGI.

func Match Uses

func Match(method string, route string, handler interface{})

Match adds a handler for an arbitrary http method in the main server.

func NewCookie Uses

func NewCookie(name string, value string, age int64) *http.Cookie

NewCookie is a helper method that returns a new http.Cookie object. Duration is specified in seconds. If the duration is zero, the cookie is permanent. This can be used in conjunction with ctx.SetCookie.

func Post Uses

func Post(route string, handler interface{})

Post adds a handler for the 'POST' http method in the main server.

func Process Uses

func Process(c http.ResponseWriter, req *http.Request)

Process invokes the main server's routing system.

func Put Uses

func Put(route string, handler interface{})

Put adds a handler for the 'PUT' http method in the main server.

func Run Uses

func Run(addr string)

Run starts the web application and serves HTTP requests for the main server.

func RunFcgi Uses

func RunFcgi(addr string)

RunFcgi starts the web application and serves FastCGI requests for the main server.

func RunScgi Uses

func RunScgi(addr string)

RunScgi starts the web application and serves SCGI requests for the main server.

func RunTLS Uses

func RunTLS(addr string, config *tls.Config)

RunTLS starts the web application and serves HTTPS requests for the main server.

func SetLogger Uses

func SetLogger(logger *log.Logger)

SetLogger sets the logger for the main server.

func Slug Uses

func Slug(s string, sep string) string

Slug is a helper function that returns the URL slug for string s. It's used to return clean, URL-friendly strings that can be used in routing.

func Urlencode Uses

func Urlencode(data map[string]string) string

Urlencode is a helper method that converts a map into URL-encoded form data. It is a useful when constructing HTTP POST requests.

func Websocket Uses

func Websocket(route string, httpHandler websocket.Handler)

Adds a handler for websockets. Only for webserver mode. Will have no effect when running as FCGI or SCGI.

type Context Uses

type Context struct {
    Request *http.Request
    Params  map[string]string
    Server  *Server
    http.ResponseWriter
}

A Context object is created for every incoming HTTP request, and is passed to handlers as an optional first argument. It provides information about the request, including the http.Request object, the GET and POST params, and acts as a Writer for the response.

func (*Context) Abort Uses

func (ctx *Context) Abort(status int, body string)

Abort is a helper method that sends an HTTP header and an optional body. It is useful for returning 4xx or 5xx errors. Once it has been called, any return value from the handler will not be written to the response.

func (*Context) BadRequest Uses

func (ctx *Context) BadRequest()

BadRequest writes a 400 HTTP response

func (*Context) ContentType Uses

func (ctx *Context) ContentType(val string) string

ContentType sets the Content-Type header for an HTTP response. For example, ctx.ContentType("json") sets the content-type to "application/json" If the supplied value contains a slash (/) it is set as the Content-Type verbatim. The return value is the content type as it was set, or an empty string if none was found.

func (*Context) Forbidden Uses

func (ctx *Context) Forbidden()

Forbidden writes a 403 HTTP response

func (*Context) GetBasicAuth Uses

func (ctx *Context) GetBasicAuth() (string, string, error)

GetBasicAuth returns the decoded user and password from the context's 'Authorization' header.

func (*Context) GetSecureCookie Uses

func (ctx *Context) GetSecureCookie(name string) (string, bool)

func (*Context) NotFound Uses

func (ctx *Context) NotFound(message string)

NotFound writes a 404 HTTP response

func (*Context) NotModified Uses

func (ctx *Context) NotModified()

Notmodified writes a 304 HTTP response

func (*Context) Redirect Uses

func (ctx *Context) Redirect(status int, url_ string)

Redirect is a helper method for 3xx redirects.

func (*Context) SetCookie Uses

func (ctx *Context) SetCookie(cookie *http.Cookie)

SetCookie adds a cookie header to the response.

func (*Context) SetHeader Uses

func (ctx *Context) SetHeader(hdr string, val string, unique bool)

SetHeader sets a response header. If `unique` is true, the current value of that header will be overwritten . If false, it will be appended.

func (*Context) SetSecureCookie Uses

func (ctx *Context) SetSecureCookie(name string, val string, age int64) error

func (*Context) Unauthorized Uses

func (ctx *Context) Unauthorized()

Unauthorized writes a 401 HTTP response

func (*Context) WriteString Uses

func (ctx *Context) WriteString(content string)

WriteString writes string data into the response object.

type Server Uses

type Server struct {
    Config *ServerConfig

    Logger *log.Logger
    Env    map[string]interface{}
    // contains filtered or unexported fields
}

Server represents a web.go server.

func NewServer Uses

func NewServer() *Server

func (*Server) Close Uses

func (s *Server) Close()

Close stops server s.

func (*Server) Delete Uses

func (s *Server) Delete(route string, handler interface{})

Delete adds a handler for the 'DELETE' http method for server s.

func (*Server) Get Uses

func (s *Server) Get(route string, handler interface{})

Get adds a handler for the 'GET' http method for server s.

func (*Server) Handle Uses

func (s *Server) Handle(route string, method string, httpHandler http.Handler)

Add a custom http.Handler. Will have no effect when running as FCGI or SCGI.

func (*Server) Match Uses

func (s *Server) Match(method string, route string, handler interface{})

Match adds a handler for an arbitrary http method for server s.

func (*Server) Post Uses

func (s *Server) Post(route string, handler interface{})

Post adds a handler for the 'POST' http method for server s.

func (*Server) Process Uses

func (s *Server) Process(c http.ResponseWriter, req *http.Request)

Process invokes the routing system for server s

func (*Server) Put Uses

func (s *Server) Put(route string, handler interface{})

Put adds a handler for the 'PUT' http method for server s.

func (*Server) Run Uses

func (s *Server) Run(addr string)

Run starts the web application and serves HTTP requests for s

func (*Server) RunFcgi Uses

func (s *Server) RunFcgi(addr string)

RunFcgi starts the web application and serves FastCGI requests for s.

func (*Server) RunScgi Uses

func (s *Server) RunScgi(addr string)

RunScgi starts the web application and serves SCGI requests for s.

func (*Server) RunTLS Uses

func (s *Server) RunTLS(addr string, config *tls.Config) error

RunTLS starts the web application and serves HTTPS requests for s.

func (*Server) ServeHTTP Uses

func (s *Server) ServeHTTP(c http.ResponseWriter, req *http.Request)

ServeHTTP is the interface method for Go's http server package

func (*Server) SetLogger Uses

func (s *Server) SetLogger(logger *log.Logger)

SetLogger sets the logger for server s

func (*Server) Websocket Uses

func (s *Server) Websocket(route string, httpHandler websocket.Handler)

Adds a handler for websockets. Only for webserver mode. Will have no effect when running as FCGI or SCGI.

type ServerConfig Uses

type ServerConfig struct {
    StaticDir    string
    Addr         string
    Port         int
    CookieSecret string
    RecoverPanic bool
    Profiler     bool
    ColorOutput  bool
}

ServerConfig is configuration for server objects.

Directories

PathSynopsis
examples

Package web imports 32 packages (graph) and is imported by 166 packages. Updated 2016-08-09. Refresh now. Tools for package owners.