web

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2016 License: MIT Imports: 25 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. If you use the weekly branch you may have difficulty compiling web.go. There's an alternative web.go branch, weekly, that attempts to keep up with the weekly 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 build 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 do the following:

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

For a quickstart guide, check out web.go's home page

There is also a tutorial

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.

Follow me on Twitter!

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func Close added in v1.0.0

func Close()

Stops the web server

func Delete

func Delete(route string, handler interface{})

Adds a handler for the 'DELETE' http method.

func Get

func Get(route string, handler interface{})

Adds a handler for the 'GET' http method.

func HandlerFunc added in v1.0.0

func HandlerFunc(route string, handler http.Handler)

Adds a generic handler function

func Options added in v1.0.0

func Options(route string, handler interface{})

Adds a handler for the 'OPTIONS' http method.

func Post

func Post(route string, handler interface{})

Adds a handler for the 'POST' http method.

func Put

func Put(route string, handler interface{})

Adds a handler for the 'PUT' http method.

func Run

func Run(addr string)

Runs the web application and serves http requests

func RunFcgi

func RunFcgi(addr string)

Runs the web application by serving fastcgi requests

func RunScgi

func RunScgi(addr string)

Runs the web application and serves scgi requests

func RunTLS added in v1.0.0

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

func Urlencode

func Urlencode(data map[string]string) string

Types

type Context

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

func (*Context) Abort

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

func (*Context) ContentType added in v1.0.0

func (ctx *Context) ContentType(ext string)

Sets the content type by extension, as defined in the mime package. For example, ctx.ContentType("json") sets the content-type to "application/json"

func (*Context) GetSecureCookie

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

func (*Context) NotFound

func (ctx *Context) NotFound(message string)

func (*Context) NotModified added in v1.0.0

func (ctx *Context) NotModified()

func (*Context) Redirect

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

func (*Context) SetCookie

func (ctx *Context) SetCookie(name string, value string, age int64)

Sets a cookie -- duration is the amount of time in seconds. 0 = forever

func (*Context) SetHeader added in v1.0.0

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

func (*Context) SetSecureCookie

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

func (*Context) WriteString

func (ctx *Context) WriteString(content string)

type ResponseWriter added in v1.0.0

type ResponseWriter interface {
	Header() http.Header
	WriteHeader(status int)
	Write(data []byte) (n int, err error)
	Close()
}

type Server added in v1.0.0

type Server struct {
	Config *ServerConfig

	Env map[string]interface{}
	// contains filtered or unexported fields
}

func NewServer added in v1.0.0

func NewServer() *Server

func (*Server) Close added in v1.0.0

func (s *Server) Close()

Stops the web server

func (*Server) Delete added in v1.0.0

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

Adds a handler for the 'DELETE' http method.

func (*Server) Get added in v1.0.0

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

Adds a handler for the 'GET' http method.

func (*Server) Options added in v1.0.0

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

Adds a handler for the 'OPTIONS' http method.

func (*Server) Post added in v1.0.0

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

Adds a handler for the 'POST' http method.

func (*Server) Put added in v1.0.0

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

Adds a handler for the 'PUT' http method.

func (*Server) Run added in v1.0.0

func (s *Server) Run(addr string)

Runs the web application and serves http requests

func (*Server) RunFcgi added in v1.0.0

func (s *Server) RunFcgi(addr string)

Runs the web application and serves scgi requests for this Server object.

func (*Server) RunScgi added in v1.0.0

func (s *Server) RunScgi(addr string)

func (*Server) RunTLS added in v1.0.0

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

func (*Server) ServeHTTP added in v1.0.0

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

type ServerConfig added in v1.0.0

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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