Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit f7e26f6ac149dec44eb3828080879c13d8e0d6d5 1 parent 2ef973d
Paul Bellamy authored
19 examples/Makefile
View
@@ -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)
35 examples/cats.go → examples/cats/cats.go
View
@@ -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
30 examples/cats_middleware.go
View
@@ -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
- }
-}
2  examples/hello.go → examples/hello/hello.go
View
@@ -1,7 +1,7 @@
package main
import (
- "mango"
+ "../../" // Point this to mango
)
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
3  examples/logger.go → examples/logger/logger.go
View
@@ -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!")
}
2  examples/routing.go → examples/routing/routing.go
View
@@ -1,7 +1,7 @@
package main
import (
- "mango"
+ "../../" // Point this to mango
)
// Our default handler
2  examples/session.go → examples/session/session.go
View
@@ -1,7 +1,7 @@
package main
import (
- "mango"
+ "../../" // Point this to mango
)
func Hello(env mango.Env) (mango.Status, mango.Headers, mango.Body) {
21 examples/silence.go
View
@@ -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)
-}
37 examples/silence/silence.go
View
@@ -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)
+}
21 examples/silence_middleware.go
View
@@ -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
-}
Please sign in to comment.
Something went wrong with that request. Please try again.