From ed2238370c37c45abd9b847c709f91285e95cb7f Mon Sep 17 00:00:00 2001 From: Arne Bahlo Date: Fri, 1 Aug 2014 18:19:01 +0200 Subject: [PATCH] Add more examples --- README.md | 94 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index afd6693..ebca3fc 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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("/login", "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("/login", "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("/login", "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