Last active
August 29, 2015 14:15
-
-
Save mpobrien/d8fc938f7df721355691 to your computer and use it in GitHub Desktop.
mux routing mount experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "net/http" | |
import "fmt" | |
import "github.com/gorilla/mux" | |
func HomeHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("home!")) | |
} | |
func ProductsHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("products!")) | |
} | |
func ArticlesHandler(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("articles!")) | |
} | |
func Plugin() http.Handler { | |
r := mux.NewRouter() | |
r.HandleFunc("/func1", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Println(mux.Vars(r)) | |
w.Write([]byte("func1!")) | |
}) | |
r.HandleFunc("/func2", func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("func2!")) | |
}) | |
/* | |
r.HandleFunc("/cmd1", func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("func1!")) | |
}) | |
r.HandleFunc("/cmd2", func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("func2!")) | |
}) | |
*/ | |
return r | |
} | |
func flattenVars(v map[string]string) []string { | |
r := make([]string, 0) | |
for k, v := range v { | |
r = append(r, k) | |
r = append(r, v) | |
} | |
return r | |
} | |
func MountHandler(r *mux.Router, prefix string, h http.Handler) http.Handler { | |
root := r.PathPrefix(prefix) | |
root.Handler( | |
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Println("processing handlerf unc") | |
url, err := root.URL(flattenVars(mux.Vars(r))...) | |
if err != nil { | |
fmt.Println("got error doing url", err) | |
http.NotFound(w, r) | |
return | |
} | |
fmt.Println("calling ", url.String()) | |
http.StripPrefix(url.String(), h).ServeHTTP(w, r) | |
})) | |
return r | |
} | |
func pluginStripPrefix(prefix string, h http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Println("vars is", mux.Vars(r)) | |
}) | |
/* | |
mux.Vars | |
if prefix == "" { | |
return h | |
} | |
return HandlerFunc(func(w ResponseWriter, r *Request) { | |
if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) { | |
r.URL.Path = p | |
h.ServeHTTP(w, r) | |
} else { | |
NotFound(w, r) | |
} | |
}) | |
*/ | |
} | |
func main() { | |
r := mux.NewRouter() | |
r.HandleFunc("/", HomeHandler) | |
r.HandleFunc("/products", ProductsHandler) | |
r.HandleFunc("/articles", ArticlesHandler) | |
pluginHandler := Plugin() | |
MountHandler(r, "/pluginname/{task_id}", pluginHandler) | |
//r.PathPrefix("/pluginname/{task_id}/").Handler(pluginStripPrefix("blah", pluginHandler)) | |
http.Handle("/", r) | |
http.ListenAndServe(":3000", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment