Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

urandom/webfw

Repository files navigation

webfw

A simple collection of things for writing web stuff.

Docs and examples are avaiable at godoc

A quick example, straight from the docs (the later is always up-to-date):

  1. The code

    package main
    
    import (
    	"net/http"
    
    	"github.com/urandom/webfw"
    	"github.com/urandom/webfw/context"
    )
    
    type Hello struct {
            webfw.BasePatternController
    }
    
    func NewHello(pattern string) Hello {
            return Hello{webfw.NewBasePatternController(pattern, MethodAll, "")}
    }
    
    func (con Hello) Handler(c *context.Context) http.HandlerFunc {
            return func(w http.ResponseWriter, r *http.Request) {
                    params := webfw.GetParams(c, r)
                    d := renderer.RenderData{"name": params["name"]}
    
                    err := webfw.GetRenderCtx(c, r)(w, d, "hello.tmpl")
                    if err != nil {
                            webfw.GetLogger(c, r).Print(err)
                    }
            }
    }
    
    
    func Example() {
    	s := webfw.NewServer()
    
    	dispatcher := s.Dispatcher("/")
    
    	dispatcher.Handle(NewHello("/hello/:name"))
    	if err := s.ListenAndServe(); err != nil {
    		panic(err)
    	}
    }
    
    func main() {
    	Example()
    }
  2. The templates, in a directory "templates"

    2.1 "base.tmpl":

    <!doctype html>
    <html>
        <body>{{ template "content" . }}</body>
    </html>
    {{ define "content" }}{{ end }}

    2.2 "hello.tmpl"

    {{ define "content" }}
    <h1>Hello {{ .name }}</h1>
    {{ end }}

About

A simple web "framework" written in go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages