Possum is a micro web library for Go.
It has following modules:
- Routers
- Views
- Session
- Helpers
Install the package:
go get github.com/mikespook/possum
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.
(Alphabetic order)
See LICENSE.