web

package
v0.0.0-...-704c7b2 Latest Latest
Warning

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

Go to latest
Published: May 16, 2013 License: MIT, MIT Imports: 26 Imported by: 0

README

web.go

web.go is the simplest way to write web applications in the Go programming language. It's ideal for writing simple, performant backend web services.

Overview

web.go should be familiar to people who've developed websites with higher-level web frameworks like sinatra or web.py. It is designed to be a lightweight web framework that doesn't impose any scaffolding on the user. Some features include:

  • Routing to url handlers based on regular expressions
  • Secure cookies
  • Support for fastcgi and scgi
  • Web applications are compiled to native code. This means very fast execution and page render speed
  • Efficiently serving static files

Installation

Make sure you have the a working Go environment. See the install instructions. web.go targets the Go release branch.

To install web.go, simply run:

go get github.com/hoisie/web

To compile it from source:

git clone git://github.com/hoisie/web.git
cd web && go build

Example

package main
    
import (
    "github.com/hoisie/web"
)
    
func hello(val string) string { return "hello " + val } 
    
func main() {
    web.Get("/(.*)", hello)
    web.Run("0.0.0.0:9999")
}

To run the application, put the code in a file called hello.go and run:

go run hello.go

You can point your browser to http://localhost:9999/world .

Getting parameters

Route handlers may contain a pointer to web.Context as their first parameter. This variable serves many purposes -- it contains information about the request, and it provides methods to control the http connection. For instance, to iterate over the web parameters, either from the URL of a GET request, or the form data of a POST request, you can access ctx.Params, which is a map[string]string:

package main

import (
    "github.com/hoisie/web"
)
    
func hello(ctx *web.Context, val string) { 
    for k,v := range ctx.Params {
		println(k, v)
	}
}   
    
func main() {
    web.Get("/(.*)", hello)
    web.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

Documentation

API docs are hosted at http://webgo.io

If you use web.go, I'd greatly appreciate a quick message about what you're building with it. This will help me get a sense of usage patterns, and helps me focus development efforts on features that people will actually use.

About

web.go was written by Michael Hoisie.

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var Config = &ServerConfig{
	RecoverPanic: true,
}

Config is the configuration of the main server.

Functions

func Close

func Close()

Close stops the main server.

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 Match

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

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

func NewCookie

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

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 web application and serves HTTP requests for the main server.

func RunFcgi

func RunFcgi(addr string)

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

func RunScgi

func RunScgi(addr string)

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

func RunTLS

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

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

func SetLogger

func SetLogger(logger *log.Logger)

SetLogger sets the logger for the main server.

func Slug

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

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.

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

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) ContentType

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) GetSecureCookie

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

func (*Context) NotFound

func (ctx *Context) NotFound(message string)

NotFound writes a 404 HTTP response

func (*Context) NotModified

func (ctx *Context) NotModified()

Notmodified writes a 304 HTTP response

func (*Context) Redirect

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

Redirect is a helper method for 3xx redirects.

func (*Context) SetCookie

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

SetCookie adds a cookie header to the response.

func (*Context) SetHeader

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

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

func (*Context) WriteString

func (ctx *Context) WriteString(content string)

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 web.go server.

func NewServer

func NewServer() *Server

func (*Server) Close

func (s *Server) Close()

Close stops server s.

func (*Server) Delete

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

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

func (*Server) Get

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

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

func (*Server) Match

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

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

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

func (*Server) Process

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

Process invokes the routing system for server s

func (*Server) Put

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

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

func (*Server) Run

func (s *Server) Run(addr string)

Run starts the web application and serves HTTP requests for s

func (*Server) RunFcgi

func (s *Server) RunFcgi(addr string)

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

func (*Server) RunScgi

func (s *Server) RunScgi(addr string)

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

func (*Server) RunTLS

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

RunTLS starts the web application and serves HTTPS requests for 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

func (*Server) SetLogger

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

SetLogger sets the logger for server s

type ServerConfig

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

ServerConfig is configuration for server objects.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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