Skip to content

Commit f7e26f6

Browse files
committed
Updating examples for 'go build', run it in each example dir
1 parent 2ef973d commit f7e26f6

File tree

10 files changed

+72
-100
lines changed

10 files changed

+72
-100
lines changed

examples/Makefile

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/cats.go renamed to examples/cats/cats.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
package main
22

33
import (
4-
"mango"
5-
"cats_middleware"
4+
"../../" // Point this to mango
65
"net/http"
76
"os"
87
"io/ioutil"
8+
"math/rand"
9+
"regexp"
910
)
1011

12+
// Our custom middleware
13+
func Cats(cat_images []string) mango.Middleware {
14+
// Initial setup stuff here
15+
// Done on application setup
16+
17+
// Initialize our regex for finding image links
18+
regex := regexp.MustCompile("[^\"']+(.jpg|.png|.gif)")
19+
20+
// This is our middleware's request handler
21+
return func(env mango.Env, app mango.App) (mango.Status, mango.Headers, mango.Body) {
22+
// Call the upstream application
23+
status, headers, body := app(env)
24+
25+
// Pick a random cat image
26+
image_url := cat_images[rand.Int()%len(cat_images)]
27+
28+
// Substitute in our cat picture
29+
body = mango.Body(regex.ReplaceAllString(string(body), image_url))
30+
31+
// Send the modified response onwards
32+
return status, headers, body
33+
}
34+
}
35+
1136
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
12-
env.Logger().Println("Got a", env.Request().Method, "request for", env.Request().RawURL)
37+
env.Logger().Println("Got a", env.Request().Method, "request for", env.Request().URL)
1338

14-
response, err := http.Get("http://www.weddinggalleryweb.com/")
39+
response, err := http.Get("http://www.example.com/")
1540
if err != nil {
1641
env.Logger().Printf("%s", err)
1742
os.Exit(1)
@@ -34,7 +59,7 @@ func main() {
3459

3560
// Initialize our cats middleware with our list of cat_images
3661
cat_images := []string{"http://images.cheezburger.com/completestore/2010/7/4/9440dc57-52a6-4122-9ab3-efd4daa0ff60.jpg", "http://images.icanhascheezburger.com/completestore/2008/12/10/128733944185267668.jpg"}
37-
cats_middleware := cats.Cats(cat_images)
62+
cats_middleware := Cats(cat_images)
3863

3964
stack.Middleware(cats_middleware) // Include the Cats middleware in our stack
4065

examples/cats_middleware.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/hello.go renamed to examples/hello/hello.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"mango"
4+
"../../" // Point this to mango
55
)
66

77
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {

examples/logger.go renamed to examples/logger/logger.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package main
22

33
import (
4-
"mango"
4+
"../../" // Point this at mango
55
"log"
66
"os"
77
)
88

99
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
10+
env.Logger().Println("Got a", env.Request().Method, "request for", env.Request().URL)
1011
return 200, mango.Headers{}, mango.Body("Hello World!")
1112
}
1213

examples/routing.go renamed to examples/routing/routing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"mango"
4+
"../../" // Point this to mango
55
)
66

77
// Our default handler

examples/session.go renamed to examples/session/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"mango"
4+
"../../" // Point this to mango
55
)
66

77
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {

examples/silence.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/silence/silence.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"../../" // Point this to mango
5+
)
6+
7+
// Our custom middleware
8+
func SilenceErrors(env mango.Env, app mango.App) (mango.Status, mango.Headers, mango.Body) {
9+
// Call our upstream app
10+
status, headers, body := app(env)
11+
12+
// If we got an error
13+
if status == 500 {
14+
// Silence it!
15+
status = 200
16+
headers = mango.Headers{}
17+
body = "Silence is golden!"
18+
}
19+
20+
// Pass the response back to the client
21+
return status, headers, body
22+
}
23+
24+
// Our default handler
25+
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
26+
//Return 500 to trigger the silence
27+
return 500, mango.Headers{}, mango.Body("Hello World!")
28+
}
29+
30+
func main() {
31+
stack := new(mango.Stack)
32+
stack.Address = ":3000"
33+
34+
stack.Middleware(SilenceErrors) // Include our custom middleware
35+
36+
stack.Run(Hello)
37+
}

examples/silence_middleware.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)