Inspired by Plug and Rack this package provides simple router which allows you to aggregate functions into pipelines for each endpoint. Pipelines are built with:
type Conn struct {
Writer http.ResponseWriter
Request *http.Request
Params url.Values
}
Contains all request related data through plugs in pipeline.
type Plug func(Conn) Conn
Should modify Conn
or Halt()
pipeline.
go get -u github.com/droptheplot/glug
package main
import (
"github.com/droptheplot/glug"
"fmt"
"net/http"
)
func Root(conn glug.Conn) glug.Conn {
fmt.Fprintf(conn.Writer, "Everyone can access this page.")
return conn
}
func BlogIndex(conn glug.Conn) glug.Conn {
fmt.Fprintf(conn.Writer, "Nothing to see here!")
return conn
}
func Auth(conn glug.Conn) glug.Conn {
http.Redirect(conn.Writer, conn.Request, "/", http.StatusFound)
return conn.Halt()
}
func main() {
r := glug.New()
r.HandleFunc("GET", "/", Root)
r.HandleFunc("GET", "/blog", Auth, BlogIndex)
http.ListenAndServe(":3000", r)
}