Created
October 8, 2015 15:47
-
-
Save PhilmacFLy/e8d61e1d4cf040241cb0 to your computer and use it in GitHub Desktop.
This file contains 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 api100 | |
import ( | |
"fmt" | |
"net/http" | |
"github.com/carbocation/interpose" | |
"github.com/carbocation/interpose/middleware" | |
"github.com/gorilla/mux" | |
) | |
func GetSubrouter(prefix string) *interpose.Middleware { | |
middle := interpose.New() | |
middle.Use(middleware.GorillaLog()) | |
r := mux.NewRouter().PathPrefix(prefix).Subrouter() | |
r.HandleFunc("/{name}/hallo", halloHandler) | |
r.HandleFunc("/{name}/tschau", tschauHandler) | |
middle.UseHandler(r) | |
return middle | |
} | |
func halloHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
n := vars["name"] | |
fmt.Fprintf(w, "Hi there, I love %s!", n) | |
} | |
func tschauHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
n := vars["name"] | |
fmt.Fprintf(w, "Bis bald %s!", n) | |
} |
This file contains 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 api200 | |
import ( | |
"fmt" | |
"net/http" | |
"github.com/carbocation/interpose" | |
"github.com/carbocation/interpose/middleware" | |
"github.com/gorilla/mux" | |
) | |
func GetSubrouter(prefix string) *interpose.Middleware { | |
middle := interpose.New() | |
middle.Use(middleware.GorillaLog()) | |
r := mux.NewRouter().PathPrefix(prefix).Subrouter() | |
r.HandleFunc("/{name}/hallo", halloHandler) | |
r.HandleFunc("/{name}/tschau", tschauHandler) | |
r.HandleFunc("/{name}/servus", servusHandler) | |
r.HandleFunc("/{name}/zeit", zeitHandler) | |
middle.UseHandler(r) | |
return middle | |
} | |
func halloHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
n := vars["name"] | |
fmt.Fprintf(w, "Hi there, I love %s!", n) | |
} | |
func tschauHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
n := vars["name"] | |
fmt.Fprintf(w, "Bis bald %s!", n) | |
} | |
func servusHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
n := vars["name"] | |
fmt.Fprintf(w, "Servus %s!", n) | |
} | |
func zeitHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
n := vars["name"] | |
fmt.Fprintf(w, "Es tut mir leid %s leider hab ich keine Uhr", n) | |
} |
This file contains 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 ( | |
"log" | |
"net/http" | |
"subbsp/api/v100" | |
"subbsp/api/v200" | |
"github.com/gorilla/mux" | |
) | |
func main() { | |
r := mux.NewRouter() | |
a100 := api100.GetSubrouter("/api/v100") | |
r.PathPrefix("/api/v100").Handler(a100) | |
a200 := api200.GetSubrouter("/api/v200") | |
r.PathPrefix("/api/v200").Handler(a200) | |
log.Println("Server Running") | |
log.Fatal(http.ListenAndServe(":4242", r)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment