diff --git a/README.md b/README.md index 7d6629d..6b42cd1 100644 --- a/README.md +++ b/README.md @@ -3,26 +3,26 @@ Goji Goji is a minimalistic web framework inspired by Sinatra. +Example +------- -Philosophy ----------- +```go +package main -Most of the design decisions in Goji can be traced back to the fundamental -philosopy that the Go standard library got things Mostly Right, and if it -didn't, it at least is good enough that it's not worth fighting. +import ( + "fmt" + "net/http" -Therefore, Goji leans heavily on the standard library, and in particular its -interfaces and idioms. You can expect to be able to use most of Goji in exactly -the manner you would use a comparable standard library function, and have it -function in exactly the way you would expect. + "github.com/zenazn/goji" + "github.com/zenazn/goji/web" +) -Also in this vein, Goji makes use of Go's `flag` package, and in particular the -default global flag set. Third party packages that have global state and squat -on global namespaces is something to be suspicious of, but the `flag` package is -also the closest thing Go has to a unified configuration API, and when used -tastefully it can make everyone's lives a bit easier. Wherever possible, the use -of these flags is opt-out, at the cost of additional complexity for the user. +func hello(c web.C, w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %s!", c.UrlParams["name"]) +} -Goji also makes an attempt to not be magical -- explicit is better than -implicit. Goji does make use of reflection and `interface{}`, but only when an -API would be impossible or cumbersome without it. +func main() { + goji.Get("/hello/:name", hello) + goji.Serve() +} +``` diff --git a/default.go b/default.go new file mode 100644 index 0000000..c43cccf --- /dev/null +++ b/default.go @@ -0,0 +1,108 @@ +package goji + +import ( + "github.com/zenazn/goji/web" +) + +// The default web.Mux. +var DefaultMux *web.Mux + +func init() { + DefaultMux = web.New() +} + +// Append the given middleware to the default Mux's middleware stack. See the +// documentation for web.Mux.Use for more informatino. +func Use(name string, middleware interface{}) { + DefaultMux.Use(name, middleware) +} + +// Insert the given middleware into the default Mux's middleware stack. See the +// documentation for web.Mux.Insert for more informatino. +func Insert(name string, middleware interface{}, before string) error { + return DefaultMux.Insert(name, middleware, before) +} + +// Remove the given middleware from the default Mux's middleware stack. See the +// documentation for web.Mux.Abandon for more informatino. +func Abandon(name string) error { + return DefaultMux.Abandon(name) +} + +// Retrieve the list of middleware from the default Mux's middleware stack. See +// the documentation for web.Mux.Middleware() for more information. +func Middleware() []string { + return DefaultMux.Middleware() +} + +// Add a route to the default Mux. See the documentation for web.Mux for more +// information about what types this function accepts. +func Handle(pattern interface{}, handler interface{}) { + DefaultMux.Handle(pattern, handler) +} + +// Add a CONNECT route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Connect(pattern interface{}, handler interface{}) { + DefaultMux.Connect(pattern, handler) +} + +// Add a DELETE route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Delete(pattern interface{}, handler interface{}) { + DefaultMux.Delete(pattern, handler) +} + +// Add a GET route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Get(pattern interface{}, handler interface{}) { + DefaultMux.Get(pattern, handler) +} + +// Add a HEAD route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Head(pattern interface{}, handler interface{}) { + DefaultMux.Head(pattern, handler) +} + +// Add a OPTIONS route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Options(pattern interface{}, handler interface{}) { + DefaultMux.Options(pattern, handler) +} + +// Add a PATCH route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Patch(pattern interface{}, handler interface{}) { + DefaultMux.Patch(pattern, handler) +} + +// Add a POST route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Post(pattern interface{}, handler interface{}) { + DefaultMux.Post(pattern, handler) +} + +// Add a PUT route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Put(pattern interface{}, handler interface{}) { + DefaultMux.Put(pattern, handler) +} + +// Add a TRACE route to the default Mux. See the documentation for web.Mux for +// more information about what types this function accepts. +func Trace(pattern interface{}, handler interface{}) { + DefaultMux.Trace(pattern, handler) +} + +// Add a sub-route to the default Mux. See the documentation for web.Mux.Sub +// for more information. +func Sub(pattern string, handler interface{}) { + DefaultMux.Sub(pattern, handler) +} + +// Set the NotFound handler for the default Mux. See the documentation for +// web.Mux.NotFound for more information. +func NotFound(handler interface{}) { + DefaultMux.NotFound(handler) +} diff --git a/goji.go b/goji.go new file mode 100644 index 0000000..e973f7c --- /dev/null +++ b/goji.go @@ -0,0 +1,67 @@ +/* +Package goji provides an out-of-box web server with reasonable defaults. + +Example: + package main + + import ( + "fmt" + "net/http" + + "github.com/zenazn/goji" + "github.com/zenazn/goji/web" + ) + + func hello(c web.C, w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %s!", c.UrlParams["name"]) + } + + func main() { + goji.Get("/hello/:name", hello) + goji.Serve() + } + +This package exists purely as a convenience to programmers who want to get +started as quickly as possible. It draws almost all of its code from goji's +subpackages, the most interesting of which is goji/web, and where most of the +documentation for the web framework lives. + +A side effect of this package's ease-of-use is the fact that it is opinionated. +If you don't like (or have outgrown) its opinions, it should be straightforward +to use the APIs of goji's subpackages to reimplement things to your liking. Both +methods of using this library are equally well supported. +*/ +package goji + +import ( + "flag" + "log" + "net/http" + + "github.com/zenazn/goji/bind" + "github.com/zenazn/goji/graceful" +) + +// Start Goji using reasonable defaults. +func Serve() { + if !flag.Parsed() { + flag.Parse() + } + + // Install our handler at the root of the standard net/http default mux. + // This allows packages like expvar to continue working as expected. + http.Handle("/", DefaultMux) + + listener := bind.Default() + log.Println("Starting Goji on", listener.Addr()) + + bind.Ready() + + err := graceful.Serve(listener, http.DefaultServeMux) + + if err != nil { + log.Fatal(err) + } + + graceful.Wait() +}