Browse Source

New and improved toplevel package

The idea is to make goji super easy to use out of the box.
Carl Jackson 12 years ago
parent
commit
b0feb9b8e2
3 changed files with 193 additions and 18 deletions
  1. +18
    -18
      README.md
  2. +108
    -0
      default.go
  3. +67
    -0
      goji.go

+ 18
- 18
README.md View File

@ -3,26 +3,26 @@ Goji
Goji is a minimalistic web framework inspired by Sinatra. 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()
}
```

+ 108
- 0
default.go View File

@ -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)
}

+ 67
- 0
goji.go View File

@ -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()
}

Loading…
Cancel
Save