Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extracting some example middleware into seperate files

  • Loading branch information...
commit 7f43e5ed06919f057433c488d1908d08dab02f0f 1 parent 215ed5a
@paulbellamy authored
Showing with 51 additions and 51 deletions.
  1. +2 −0  Makefile
  2. +12 −0 logger.go
  3. +1 −51 mango.go
  4. +36 −0 show_errors.go
View
2  Makefile
@@ -5,6 +5,8 @@ GOFMT=gofmt
GOFILES=\
mango.go\
+ logger.go\
+ show_errors.go\
include $(GOROOT)/src/Make.pkg
View
12 logger.go
@@ -0,0 +1,12 @@
+package mango
+
+import (
+ "log"
+)
+
+func Logger(logger *log.Logger) Middleware {
+ return func(env Env, app App) (Status, Headers, Body) {
+ env["mango.logger"] = logger
+ return app(env)
+ }
+}
View
52 mango.go
@@ -1,13 +1,9 @@
package mango
import (
- "bytes"
"fmt"
"http"
- "log"
"os"
- "strings"
- "template"
)
type Env map[string]interface{}
@@ -95,53 +91,7 @@ func (this *Stack) Run(app App) os.Error {
if this.Address == "" {
this.Address = "0.0.0.0:8000"
}
- log.Println("Starting Mango Stack On:", this.Address)
+ fmt.Println("Starting Mango Stack On: %s", this.Address)
http.HandleFunc("/", this.buildStack())
return http.ListenAndServe(this.Address, nil)
}
-
-
-/*************************************
- * End Mango Source
- * Begin Example Usage
- ************************************/
-
-// An example of how to pass runtime config to Middleware
-func Logger(prefix string) Middleware {
- return func(env Env, app App) (Status, Headers, Body) {
- status, headers, body := app(env)
- log.Println(prefix, env["mango.request"].(*http.Request).Method, env["mango.request"].(*http.Request).RawURL, status)
- return status, headers, body
- }
-}
-
-func ShowErrors(templateString string) Middleware {
- if templateString == "" {
- templateString = `
- <html>
- <body>
- <p>
- {Error|html}
- </p>
- </body>
- </html>
- `
- }
-
- errorTemplate := template.MustParse(templateString, nil)
-
- return func(env Env, app App) (status Status, headers Headers, body Body) {
- defer func() {
- if err := recover(); err != nil {
- log.Println("Error: ", err)
- buffer := bytes.NewBufferString("")
- errorTemplate.Execute(buffer, struct{ Error string }{err.(string)})
- status = 500
- headers = make(map[string]string)
- body = Body(buffer.String())
- }
- }()
-
- return app(env)
- }
-}
View
36 show_errors.go
@@ -0,0 +1,36 @@
+package mango
+
+import (
+ "bytes"
+ "template"
+)
+
+func ShowErrors(templateString string) Middleware {
+ if templateString == "" {
+ templateString = `
+ <html>
+ <body>
+ <p>
+ {Error|html}
+ </p>
+ </body>
+ </html>
+ `
+ }
+
+ errorTemplate := template.MustParse(templateString, nil)
+
+ return func(env Env, app App) (status Status, headers Headers, body Body) {
+ defer func() {
+ if err := recover(); err != nil {
+ buffer := bytes.NewBufferString("")
+ errorTemplate.Execute(buffer, struct{ Error string }{err.(string)})
+ status = 500
+ headers = make(map[string]string)
+ body = Body(buffer.String())
+ }
+ }()
+
+ return app(env)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.