A micro web library for Go.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
demo
router
session
view
.gitignore
.travis.yml
LICENSE
README.md
context.go
error.go
helper.go
pprof.go
router.go
router_test.go
server.go
session.go

README.md

Possum

Build Status GoDoc Coverage Status

Possum is a micro web library for Go.

It has following modules:

  • Routers
  • Views
  • Session
  • Helpers

Install

Install the package:

go get github.com/mikespook/possum

Usage

Importing the package and sub-packages:

import (
	"github.com/mikespook/possum"
	"github.com/mikespook/possum/router"
	"github.com/mikespook/possum/view"
)

Possum uses Context for passing data, handling request and rendering response.

This is how to create a new server mux for Possum:

mux := possum.NewServerMux()

And assign a customized error handler:

mux.ErrorHandle = func(err error) {
	fmt.Println(err)
}

PreRequest and PostResponse are useful for pre-checking or customizing logs:

mux.PreRequest = func(ctx *possum.Context) error {
	host, port, err := net.SplitHostPort(ctx.Request.RemoteAddr)
	if err != nil {
		return err
	}
	if host != "127.0.0.1" {
		return possum.NewError(http.StatusForbidden, "Localhost only")
	}
	return nil
}

mux.PostResponse = func(ctx *possum.Context) error {
	fmt.Printf("[%d] %s:%s \"%s\"", ctx.Response.Status,
		ctx.Request.RemoteAddr,	ctx.Request.Method,
		ctx.Request.URL.String())		
}

A specific path can bind to a different combination of routers, handlers and views:

f := session.NewFactory(session.CookieStorage('session-id', nil))

func helloword(ctx *Context) error {
	ctx.StartSession(f)
	return nil
}

mux.HandlerFunc(router.Simple("/json"), helloword, view.Json(view.CharSetUTF8))

if err := view.InitHtmlTemplates("*.html"); err != nil {
	return
}
mux.HandleFunc(router.Wildcard("/html/*/*"),
	helloworld, view.Html("base.html", "utf-8"))

if err := view.InitWatcher("*.html", view.InitTextTemplates, nil);
	err != nil {
	return
}
mux.HandleFunc(router.RegEx("/html/(.*)/[a-z]"),
	helloworld, view.Text("base.html", "utf-8"))

mux.HandleFunc(router.Colon("/:img/:id"), 
	nil, view.File("img.jpg", "image/jpeg"))

Also, a PProf methods can be initialized by mux.InitPProf:

mux.InitPProf("/_pprof")

It will serve profiles and debug informations through http://ip:port/_pprof.

E.g.:

And finally, it is a standard way for listening and serving:

http.ListenAndServe(":8080", mux)

For more details, please see the demo.

Contributors

(Alphabetic order)

Open Source - MIT Software License

See LICENSE.