Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b60169
commit 771d38f
Showing
2 changed files
with
5 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,6 @@ | ||
/* | ||
Package traffic - a Sinatra inspired regexp/pattern mux for Go. | ||
Simple example: | ||
package main | ||
import ( | ||
"net/http" | ||
"github.com/pilu/traffic" | ||
"fmt" | ||
) | ||
func rootHandler(w traffic.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "Hello World\n") | ||
} | ||
func pageHandler(w traffic.ResponseWriter, r *http.Request) { | ||
params := r.URL.Query() | ||
fmt.Fprintf(w, "Category ID: %s\n", params.Get("category_id")) | ||
fmt.Fprintf(w, "Page ID: %s\n", params.Get("id")) | ||
} | ||
func main() { | ||
router := traffic.New() | ||
// Routes | ||
router.Get("/", rootHandler) | ||
router.Get("/categories/:category_id/pages/:id", pageHandler) | ||
http.Handle("/", router) | ||
http.ListenAndServe(":7000", nil) | ||
} | ||
You can also add "before filters" to all your routes or just to some of them: | ||
router := traffic.New() | ||
// Executed before all handlers | ||
router.AddBeforeFilter(checkApiKey). | ||
AddBeforeFilter(addAppNameHeader). | ||
AddBeforeFilter(addTimeHeader) | ||
// Routes | ||
router.Get("/", rootHandler) | ||
router.Get("/categories/:category_id/pages/:id", pageHandler) | ||
// "/private" has one more before filter that checks for a second api key (private_api_key) | ||
router.Get("/private", privatePageHandler). | ||
AddBeforeFilter(checkPrivatePageApiKey) | ||
Complete example: | ||
func rootHandler(w traffic.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "Hello World\n") | ||
} | ||
func privatePageHandler(w traffic.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "Hello Private Page\n") | ||
} | ||
func pageHandler(w traffic.ResponseWriter, r *http.Request) { | ||
params := r.URL.Query() | ||
fmt.Fprintf(w, "Category ID: %s\n", params.Get("category_id")) | ||
fmt.Fprintf(w, "Page ID: %s\n", params.Get("id")) | ||
} | ||
func checkApiKey(w traffic.ResponseWriter, r *http.Request) bool { | ||
params := r.URL.Query() | ||
if params.Get("api_key") != "foo" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return false | ||
} | ||
return true | ||
} | ||
func checkPrivatePageApiKey(w traffic.ResponseWriter, r *http.Request) bool { | ||
params := r.URL.Query() | ||
if params.Get("private_api_key") != "bar" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return false | ||
} | ||
return true | ||
} | ||
func addAppNameHeader(w traffic.ResponseWriter, r *http.Request) bool { | ||
w.Header().Add("X-APP-NAME", "My App") | ||
return true | ||
} | ||
func addTimeHeader(w traffic.ResponseWriter, r *http.Request) bool { | ||
t := fmt.Sprintf("%s", time.Now()) | ||
w.Header().Add("X-APP-TIME", t) | ||
return true | ||
} | ||
func main() { | ||
router := traffic.New() | ||
// Routes | ||
router.Get("/", rootHandler) | ||
router.Get("/categories/:category_id/pages/:id", pageHandler) | ||
// "/private" has one more before filter that checks for a second api key (private_api_key) | ||
router.Get("/private", privatePageHandler). | ||
AddBeforeFilter(checkPrivatePageApiKey) | ||
// Executed before all handlers | ||
router.AddBeforeFilter(checkApiKey). | ||
AddBeforeFilter(addAppNameHeader). | ||
AddBeforeFilter(addTimeHeader) | ||
http.Handle("/", router) | ||
http.ListenAndServe(":7000", nil) | ||
} | ||
Docs and examples are on github: https://github.com/pilu/traffic/ | ||
*/ | ||
package traffic |