-
Notifications
You must be signed in to change notification settings - Fork 8
/
singlepath.go
39 lines (30 loc) · 1.17 KB
/
singlepath.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"fmt"
"net/http"
"github.com/liveplant/liveplant-server/Godeps/_workspace/src/github.com/carbocation/interpose"
"github.com/liveplant/liveplant-server/Godeps/_workspace/src/github.com/carbocation/interpose/middleware"
"github.com/liveplant/liveplant-server/Godeps/_workspace/src/github.com/gorilla/mux"
)
func main() {
middle := interpose.New()
// Invoke the Gorilla framework's combined logger
middle.Use(middleware.GorillaLog())
// Create a router to serve HTTP content at two paths
// and tell our middleware about the router
router := mux.NewRouter()
middle.UseHandler(router)
router.PathPrefix("/green").Subrouter().Handle("/{name}", Green(http.HandlerFunc(welcomeHandler)))
router.Handle("/{name}", http.HandlerFunc(welcomeHandler))
http.ListenAndServe(":3001", middle)
}
func welcomeHandler(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, "Welcome to the home page, %s", mux.Vars(req)["name"])
}
func Green(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("X-Favorite-Color", "green")
next.ServeHTTP(rw, req)
fmt.Fprint(rw, " who likes green")
})
}