Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
Merge branch 'better-readme'
Browse files Browse the repository at this point in the history
* better-readme:
  Add more examples
  • Loading branch information
bahlo committed Aug 1, 2014
2 parents 6a41dac + ed22383 commit 617a9b3
Showing 1 changed file with 69 additions and 25 deletions.
94 changes: 69 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Goat is a Go REST server. You can pronounce it like the _goat_, or
_go-at_. Depends on how you like goats.

## Usage
## Basic Usage
```go
package main

Expand All @@ -13,50 +13,94 @@ import (
"github.com/bahlo/goat"
)

func notImplementedHandler(w http.ResponseWriter, r *http.Request, p goat.Params) {
goat.WriteError(w, 500, "Not implemented")
}

func helloHandler(w http.ResponseWriter, r *http.Request, p goat.Params) {
goat.WriteJSON(w, map[string]string{
"hello": p["name"],
})
goat.WriteJSON(w, map[string]string{
"hello": p["name"],
})
}

func main() {
r := goat.New()
r := goat.New()

r.Get("/", "", r.IndexHandler)
r.Get("/hello/:name", "hello_url", helloHandler)
r.Get("/hello/:name", "hello_url", helloHandler)

sr := r.Subrouter("/user")
sr.Get("/#", "user_login_url", notImplementedHandler)
sr.Get("/logout", "user_logout_url", notImplementedHandler)

http.ListenAndServe(":8080", r)
r.Run(":8080")
}
```

## Features
### Groups
You can group the routes by a prefix. This can have a serious impact on the
readability of your code.
## Subrouters
You can create subrouters to simplify your code
```go
func main() {
r := goat.New()
r.Get("/hello/:name", "hello_url", helloHandler)

sr := r.Subrouter("/user")
{
sr.Post("/#", "user_login_url", loginHandler)
sr.Get("/logout", "user_logout_url", logoutHandler)
}

### Indices
r.Run(":8080")
}
```

## Indices
Every route can have a description (like `user_login_url`). These can be used
to automagically generate an API index (like [this](https://api.github.com)).
If you want to hide specific methods, just provide an empty string.

```go
func main() {
r := goat.New()

r.Get("/", "", r.IndexHandler)
r.Get("/hello/:name", "hello_url", helloHandler)

sr := r.Subrouter("/user")
{
sr.Post("/#", "user_login_url", loginHandler)
sr.Get("/logout", "user_logout_url", logoutHandler)
}

r.Run(":8080")
}
```

The above example would return the following response on `/`:
```json
{
"hello_url": "/hello/:name",
"user_logout_url": "/user/logout"
}
```

**Note:** Indices are only supported for `GET` requests. Open an issue, if you
want them on other methods, too

### Helpers
You can quickly pretty print JSON to a `http.ResponseWriter` using
`goat.WriteJSON` or `goat.WriteError`.
## Middleware
You can easily include any middleware you like. Important is, that it's in the
following format:
```go
func(http.Handler) http.Handler
```

Example:
```go
func main() {
r := goat.New()

r.Get("/hello/:name", "hello_url", helloHandler)

r.Use(myMiddleWare, myOtherMiddleWare)

r.Run(":8080")
}
```

## Roadmap
* [x] Subrouters or Grouping
* [ ] Middleware
* [x] Middleware
* [ ] Continous integration

## Credits
Expand Down

0 comments on commit 617a9b3

Please # to comment.