Permalink
Browse files

Updating examples for 'go build', run it in each example dir

  • Loading branch information...
1 parent 2ef973d commit f7e26f6ac149dec44eb3828080879c13d8e0d6d5 @paulbellamy committed Mar 10, 2012
View
19 examples/Makefile
@@ -1,19 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-BINS=hello logger session routing cats silence
-LIBS=cats_middleware.a silence_middleware.a
-
-all: $(LIBS) $(BINS)
-
-clean:
- rm -rf *.[68] $(BINS)
-
-%: %.go
- $(GC) -I _obj $*.go
- $(LD) -L _obj -o $@ $*.$O
-
-%.a: %.go
- $(GC) $*.go
- @mkdir -p _obj
- rm -f _obj/$@ $(OFILES)
- gopack grc _obj/$@ $*.$O $(OFILES)
View
35 examples/cats.go → examples/cats/cats.go
@@ -1,17 +1,42 @@
package main
import (
- "mango"
- "cats_middleware"
+ "../../" // Point this to mango
"net/http"
"os"
"io/ioutil"
+ "math/rand"
+ "regexp"
)
+// Our custom middleware
+func Cats(cat_images []string) mango.Middleware {
+ // Initial setup stuff here
+ // Done on application setup
+
+ // Initialize our regex for finding image links
+ regex := regexp.MustCompile("[^\"']+(.jpg|.png|.gif)")
+
+ // This is our middleware's request handler
+ return func(env mango.Env, app mango.App) (mango.Status, mango.Headers, mango.Body) {
+ // Call the upstream application
+ status, headers, body := app(env)
+
+ // Pick a random cat image
+ image_url := cat_images[rand.Int()%len(cat_images)]
+
+ // Substitute in our cat picture
+ body = mango.Body(regex.ReplaceAllString(string(body), image_url))
+
+ // Send the modified response onwards
+ return status, headers, body
+ }
+}
+
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
- env.Logger().Println("Got a", env.Request().Method, "request for", env.Request().RawURL)
+ env.Logger().Println("Got a", env.Request().Method, "request for", env.Request().URL)
- response, err := http.Get("http://www.weddinggalleryweb.com/")
+ response, err := http.Get("http://www.example.com/")
if err != nil {
env.Logger().Printf("%s", err)
os.Exit(1)
@@ -34,7 +59,7 @@ func main() {
// Initialize our cats middleware with our list of cat_images
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"}
- cats_middleware := cats.Cats(cat_images)
+ cats_middleware := Cats(cat_images)
stack.Middleware(cats_middleware) // Include the Cats middleware in our stack
View
30 examples/cats_middleware.go
@@ -1,30 +0,0 @@
-package cats
-
-import (
- "mango"
- "rand"
- "regexp"
-)
-
-func Cats(cat_images []string) mango.Middleware {
- // Initial setup stuff here
- // Done on application setup
-
- // Initialize our regex for finding image links
- regex := regexp.MustCompile("[^\"']+(.jpg|.png|.gif)")
-
- // This is our middleware's request handler
- return func(env mango.Env, app mango.App) (mango.Status, mango.Headers, mango.Body) {
- // Call the upstream application
- status, headers, body := app(env)
-
- // Pick a random cat image
- image_url := cat_images[rand.Int()%len(cat_images)]
-
- // Substitute in our cat picture
- body = mango.Body(regex.ReplaceAllString(string(body), image_url))
-
- // Send the modified response onwards
- return status, headers, body
- }
-}
View
2 examples/hello.go → examples/hello/hello.go
@@ -1,7 +1,7 @@
package main
import (
- "mango"
+ "../../" // Point this to mango
)
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
View
3 examples/logger.go → examples/logger/logger.go
@@ -1,12 +1,13 @@
package main
import (
- "mango"
+ "../../" // Point this at mango
"log"
"os"
)
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
+ env.Logger().Println("Got a", env.Request().Method, "request for", env.Request().URL)
return 200, mango.Headers{}, mango.Body("Hello World!")
}
View
2 examples/routing.go → examples/routing/routing.go
@@ -1,7 +1,7 @@
package main
import (
- "mango"
+ "../../" // Point this to mango
)
// Our default handler
View
2 examples/session.go → examples/session/session.go
@@ -1,7 +1,7 @@
package main
import (
- "mango"
+ "../../" // Point this to mango
)
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
View
21 examples/silence.go
@@ -1,21 +0,0 @@
-package main
-
-import (
- "mango"
- "silence_middleware"
-)
-
-// Our default handler
-func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
- //Return 500 to trigger the silence
- return 500, mango.Headers{}, mango.Body("Hello World!")
-}
-
-func main() {
- stack := new(mango.Stack)
- stack.Address = ":3000"
-
- stack.Middleware(silence.SilenceErrors) // Include our custom middleware
-
- stack.Run(Hello)
-}
View
37 examples/silence/silence.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "../../" // Point this to mango
+)
+
+// Our custom middleware
+func SilenceErrors(env mango.Env, app mango.App) (mango.Status, mango.Headers, mango.Body) {
+ // Call our upstream app
+ status, headers, body := app(env)
+
+ // If we got an error
+ if status == 500 {
+ // Silence it!
+ status = 200
+ headers = mango.Headers{}
+ body = "Silence is golden!"
+ }
+
+ // Pass the response back to the client
+ return status, headers, body
+}
+
+// Our default handler
+func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
+ //Return 500 to trigger the silence
+ return 500, mango.Headers{}, mango.Body("Hello World!")
+}
+
+func main() {
+ stack := new(mango.Stack)
+ stack.Address = ":3000"
+
+ stack.Middleware(SilenceErrors) // Include our custom middleware
+
+ stack.Run(Hello)
+}
View
21 examples/silence_middleware.go
@@ -1,21 +0,0 @@
-package silence
-
-import (
- "mango"
-)
-
-func SilenceErrors(env mango.Env, app mango.App) (mango.Status, mango.Headers, mango.Body) {
- // Call our upstream app
- status, headers, body := app(env)
-
- // If we got an error
- if status == 500 {
- // Silence it!
- status = 200
- headers = mango.Headers{}
- body = "Silence is golden!"
- }
-
- // Pass the response back to the client
- return status, headers, body
-}

0 comments on commit f7e26f6

Please sign in to comment.