Documentation ¶
Overview ¶
Package webgo is a lightweight webgo framework for Go.
It's ideal for writing simple, performant backend webgo services.
Example
package main import ( "dev.visiontek.wh/webgo" ) func hello(val string) string { return "hello " + val } func main() { webgo.Get("/(.*)", hello) webgo.Run("0.0.0.0:9999") }
Getting parameters
package main import ( "dev.visiontek.wh/webgo" ) func hello(ctx *webgo.Context, val string) { for k,v := range ctx.Params { println(k, v) } } func main() { webgo.Get("/(.*)", hello) webgo.Run("0.0.0.0:9999") }
In this example, if you visit `http://localhost:9999/?a=1&b=2`, you'll see the following printed out in the terminal:
a 1 b 2
Index ¶
- Variables
- func Close()
- func Delete(route string, handler interface{})
- func Get(route string, handler interface{})
- func Handler(route string, method string, httpHandler http.Handler)
- func Match(method string, route string, handler interface{})
- func NewCookie(name string, value string, age int64) *http.Cookie
- func Post(route string, handler interface{})
- func Process(c http.ResponseWriter, req *http.Request)
- func Put(route string, handler interface{})
- func Run(addr string)
- func RunFcgi(addr string)
- func RunScgi(addr string)
- func RunTLS(addr string, config *tls.Config)
- func SetLogger(logger *log.Logger)
- func Slug(s string, sep string) string
- func Urlencode(data map[string]string) string
- func Websocket(route string, httpHandler websocket.Handler)
- type Context
- func (ctx *Context) Abort(status int, body string)
- func (ctx *Context) ContentType(val string) string
- func (ctx *Context) Forbidden()
- func (ctx *Context) GetBasicAuth() (string, string, error)
- func (ctx *Context) GetSecureCookie(name string) (string, bool)
- func (ctx *Context) NotFound(message string)
- func (ctx *Context) NotModified()
- func (ctx *Context) Redirect(status int, url_ string)
- func (ctx *Context) SetCookie(cookie *http.Cookie)
- func (ctx *Context) SetHeader(hdr string, val string, unique bool)
- func (ctx *Context) SetSecureCookie(name string, val string, age int64)
- func (ctx *Context) Unauthorized()
- func (ctx *Context) WriteString(content string)
- type Server
- func (s *Server) Close()
- func (s *Server) Delete(route string, handler interface{})
- func (s *Server) Get(route string, handler interface{})
- func (s *Server) Handler(route string, method string, httpHandler http.Handler)
- func (s *Server) Match(method string, route string, handler interface{})
- func (s *Server) Post(route string, handler interface{})
- func (s *Server) Process(c http.ResponseWriter, req *http.Request)
- func (s *Server) Put(route string, handler interface{})
- func (s *Server) Run(addr string)
- func (s *Server) RunFcgi(addr string)
- func (s *Server) RunScgi(addr string)
- func (s *Server) RunTLS(addr string, config *tls.Config) error
- func (s *Server) ServeHTTP(c http.ResponseWriter, req *http.Request)
- func (s *Server) SetLogger(logger *log.Logger)
- func (s *Server) Websocket(route string, httpHandler websocket.Handler)
- type ServerConfig
- type Session
- type SessionManager
- func (p *SessionManager) Abandon()
- func (p *SessionManager) GetPath() string
- func (p *SessionManager) GetSession(res http.ResponseWriter, req *http.Request) (session *Session)
- func (p *SessionManager) GetSessionById(id string) (session *Session)
- func (p *SessionManager) GetTimeout() uint
- func (p *SessionManager) Has(id string) (found bool)
- func (p *SessionManager) OnEnd(f func(*Session))
- func (p *SessionManager) OnStart(f func(*Session))
- func (p *SessionManager) OnTouch(f func(*Session))
- func (p *SessionManager) SetPath(t string)
- func (p *SessionManager) SetTimeout(t uint)
Constants ¶
This section is empty.
Variables ¶
var Config = &ServerConfig{ RecoverPanic: true, }
Config is the configuration of the main server.
Functions ¶
func Delete ¶
func Delete(route string, handler interface{})
Delete adds a handler for the 'DELETE' http method in the main server.
func Get ¶
func Get(route string, handler interface{})
Get adds a handler for the 'GET' http method in the main server.
func Handler ¶
Adds a custom handler. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
func NewCookie ¶
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 ¶
func Post(route string, handler interface{})
Post adds a handler for the 'POST' http method in the main server.
func Process ¶
func Process(c http.ResponseWriter, req *http.Request)
Process invokes the main server's routing system.
func Put ¶
func Put(route string, handler interface{})
Put adds a handler for the 'PUT' http method in the main server.
func Run ¶
func Run(addr string)
Run starts the webgo application and serves HTTP requests for the main server.
func RunFcgi ¶
func RunFcgi(addr string)
RunFcgi starts the webgo application and serves FastCGI requests for the main server.
func RunScgi ¶
func RunScgi(addr string)
RunScgi starts the webgo application and serves SCGI requests for the main server.
func Slug ¶
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.
Types ¶
type Context ¶
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 ¶
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) ContentType ¶
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) GetBasicAuth ¶
GetBasicAuth is a helper method of *Context that returns the decoded user and password from the *Context's authorization header
func (*Context) NotModified ¶
func (ctx *Context) NotModified()
Notmodified writes a 304 HTTP response
func (*Context) SetHeader ¶
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 ¶
func (*Context) Unauthorized ¶
func (ctx *Context) Unauthorized()
Unauthorized writes a 401 HTTP response
func (*Context) WriteString ¶
WriteString writes string data into the response object.
type Server ¶
type Server struct { Config *ServerConfig Logger *log.Logger Env map[string]interface{} // contains filtered or unexported fields }
Server represents a webgo.go server.
func (*Server) Handler ¶
Adds a custom handler. Only for webserver mode. Will have no effect when running as FCGI or SCGI.
func (*Server) Process ¶
func (s *Server) Process(c http.ResponseWriter, req *http.Request)
Process invokes the routing system for server s
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(c http.ResponseWriter, req *http.Request)
ServeHTTP is the interface method for Go's http server package
type ServerConfig ¶
type ServerConfig struct { StaticDir string StaticFilesServer bool // FilesServerRoot: StaticDir + "/files" Addr string Port int CookieSecret string RecoverPanic bool Profiler bool }
ServerConfig is configuration for server objects.
type Session ¶
type Session struct { Id string Value interface{} // contains filtered or unexported fields }
Session stores the id and value for a session.
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager manager sessions.
func NewSessionManager ¶
func NewSessionManager(logger *log.Logger) *SessionManager
func (*SessionManager) Abandon ¶
func (p *SessionManager) Abandon()
func (*SessionManager) GetPath ¶
func (p *SessionManager) GetPath() string
func (*SessionManager) GetSession ¶
func (p *SessionManager) GetSession(res http.ResponseWriter, req *http.Request) (session *Session)
func (*SessionManager) GetSessionById ¶
func (p *SessionManager) GetSessionById(id string) (session *Session)
func (*SessionManager) GetTimeout ¶
func (p *SessionManager) GetTimeout() uint
func (*SessionManager) Has ¶
func (p *SessionManager) Has(id string) (found bool)
func (*SessionManager) OnEnd ¶
func (p *SessionManager) OnEnd(f func(*Session))
func (*SessionManager) OnStart ¶
func (p *SessionManager) OnStart(f func(*Session))
func (*SessionManager) OnTouch ¶
func (p *SessionManager) OnTouch(f func(*Session))
func (*SessionManager) SetPath ¶
func (p *SessionManager) SetPath(t string)
func (*SessionManager) SetTimeout ¶
func (p *SessionManager) SetTimeout(t uint)